{"id":5801,"library":"pyobjc-framework-cfnetwork","title":"PyObjC CFNetwork Framework Bindings","description":"PyObjC provides Python bindings for Apple's macOS frameworks, allowing Python applications to interact with Objective-C APIs. The `pyobjc-framework-cfnetwork` package specifically provides wrappers for the CFNetwork framework, which offers a C-based API for low-level network services. It is actively maintained as part of the broader PyObjC project, with releases often coinciding with new macOS SDK updates and Python version support. The current version is 12.1.","status":"active","version":"12.1","language":"en","source_language":"en","source_url":"https://github.com/ronaldoussoren/pyobjc","tags":["macos","objective-c","cocoa","network","cfnetwork"],"install":[{"cmd":"pip install pyobjc-framework-cfnetwork","lang":"bash","label":"Install pyobjc-framework-cfnetwork"}],"dependencies":[{"reason":"Provides the core bridge between Python and Objective-C, essential for all PyObjC framework bindings.","package":"pyobjc-core"}],"imports":[{"symbol":"CFNetwork","correct":"import CFNetwork"}],"quickstart":{"code":"import CFNetwork\n\n# Create a CFHost object for a given hostname\nhostname = \"www.apple.com\"\nhost = CFNetwork.CFHostCreateWithName(None, hostname)\n\nif host:\n    # Start info resolution (blocking for simplicity; real apps might use async)\n    # kCFHostAddresses resolves IP addresses\n    success = CFNetwork.CFHostStartInfoResolution(\n        host, CFNetwork.kCFHostAddresses, None\n    )\n    if success:\n        # Retrieve the resolved addresses\n        # PyObjC automatically converts CFArrayRef to Python list\n        addresses = CFNetwork.CFHostGetAddressing(host, None)\n        if addresses:\n            print(f\"Resolved addresses for {hostname}:\")\n            for address in addresses:\n                # address is a CFDataRef containing sockaddr_storage\n                print(f\"  {address}\") \n        else:\n            print(f\"No addresses found for {hostname}\")\n    else:\n        print(f\"Failed to resolve host {hostname}\")\n    \n    # Stop resolution and release resources (PyObjC handles Python object lifecycle)\n    CFNetwork.CFHostStopInfoResolution(host)\nelse:\n    print(f\"Failed to create CFHost object for {hostname}\")","lang":"python","description":"This example demonstrates how to use `CFNetwork.CFHostCreateWithName` to create a host object and then resolve its IP addresses. It showcases basic interaction with a C-based macOS networking API through PyObjC."},"warnings":[{"fix":"Ensure your project targets Python 3.10 or later when using PyObjC 12.x, and Python 3.9 or later when using PyObjC 11.x.","message":"Python 3.9 support was dropped in PyObjC 12.0. Python 3.8 support was dropped in PyObjC 11.0.","severity":"breaking","affected_versions":"12.0+"},{"fix":"Review code that interacts with `alloc`/`init` patterns for potential memory management issues, although Python's GC often mitigates direct impact unless complex manual reference counting is involved.","message":"PyObjC 11.1 introduced changes to correctly model Automatic Reference Counting (ARC) for initializer methods (e.g., methods in the 'init' family). This aligns with `clang`'s documentation, where initializers steal a reference to `self` and return a new one.","severity":"breaking","affected_versions":"11.1+"},{"fix":"Avoid using free-threaded Python builds (e.g., `python --enable-thread-gil=0`) with PyObjC until full, stable support is officially announced to prevent crashes or incorrect behavior.","message":"Experimental support for free-threading (PEP 703) in Python 3.13 and later is *not yet fully supported* or stable in PyObjC, despite PyObjC 11.0 introducing *experimental* changes related to it.","severity":"gotcha","affected_versions":"11.0+"},{"fix":"Carefully review classes that override `__new__` and use `__init__` in conjunction with PyObjC, especially for subclasses of Objective-C classes. You may need to adapt to PyObjC's object construction patterns, typically favoring `init` methods.","message":"The behavior of `__init__` when a custom `__new__` is implemented changed in PyObjC 10.3, potentially breaking existing code. While 10.3.1 partially re-introduced `__init__` support for classes with user-implemented `__new__`, those relying on PyObjC's `__new__` still cannot use `__init__`.","severity":"gotcha","affected_versions":"10.3 - 10.3.1"},{"fix":"If your application relied on `IMServicePlugIn` bindings, you must either target an older macOS and PyObjC version or migrate to alternative APIs if available.","message":"PyObjC 10.0 removed bindings for the `IMServicePlugIn` framework, as it was deprecated in macOS 10.13 and removed in macOS 14.","severity":"breaking","affected_versions":"10.0+"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}