{"id":5410,"library":"pyobjc-framework-security","title":"PyObjC Framework Security","description":"PyObjC is a bridge between Python and Objective-C, enabling Python scripts to use and extend existing Objective-C class libraries on macOS. The `pyobjc-framework-security` package provides Python wrappers for Apple's Security framework, allowing developers to access macOS security services like authentication, authorization, and secure data handling from Python. 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","objc","security","framework","apple"],"install":[{"cmd":"pip install pyobjc-framework-security","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Provides the core Python to Objective-C bridge functionality required by all PyObjC framework wrappers.","package":"pyobjc-core","optional":false}],"imports":[{"note":"The bindings for the Security framework are accessed directly through the 'Security' package name.","symbol":"Security","correct":"import Security"}],"quickstart":{"code":"import Security\nimport objc\n\ndef generate_random_bytes(length):\n    \"\"\"Generates cryptographically secure random bytes using Security.framework.\"\"\"\n    # SecRandomCopyBytes is a C function in the Security framework.\n    # We need to load it explicitly if not already exposed as a Python function.\n    # It's usually exposed automatically, but this demonstrates explicit loading for C functions.\n    # The 'b' format indicates a C array of bytes.\n    _functions = [\n        ('SecRandomCopyBytes', 'iI^v'), # OSStatus SecRandomCopyBytes(SecRandomGeneratorRef, size_t, void *)\n    ]\n\n    # Load the function from the Security framework bundle\n    # Security.bundle() is the NSBundle for the Security framework\n    # Use globals() to make the loaded function available directly\n    objc.loadBundleFunctions(Security.bundle(), globals(), _functions)\n\n    buffer = bytearray(length)\n    # kSecRandomDefault is typically passed as the first argument (generatorRef)\n    # Its value is usually 0 for the default generator, or None in PyObjC often works for NULL\n    # The correct value for kSecRandomDefault is implicitly handled by PyObjC for simple cases.\n    # If the function signature is 'iI^v', the 'I' is size_t (length), '^v' is void* (buffer).\n    # PyObjC often allows passing bytearray directly for buffer pointers.\n\n    # SecRandomCopyBytes(generator, count, bytes)\n    # We use 0 for kSecRandomDefault, and 'buffer' for the output bytes.\n    status = SecRandomCopyBytes(0, length, buffer)\n\n    if status == 0: # noErr\n        return bytes(buffer)\n    else:\n        raise RuntimeError(f\"Failed to generate random bytes: {status}\")\n\nif __name__ == \"__main__\":\n    try:\n        random_data = generate_random_bytes(16)\n        print(f\"Generated 16 random bytes: {random_data.hex()}\")\n    except Exception as e:\n        print(f\"Error: {e}\")\n","lang":"python","description":"This quickstart demonstrates how to use `pyobjc-framework-security` to generate cryptographically secure random bytes using the `SecRandomCopyBytes` C function from the macOS Security framework. It showcases loading a C function and handling byte buffers."},"warnings":[{"fix":"Upgrade to Python 3.10 or later. PyObjC 12.1 requires Python >=3.10.","message":"Support for Python 3.9 was dropped in PyObjC version 12.0.","severity":"breaking","affected_versions":">=12.0"},{"fix":"Upgrade to Python 3.9 or later (preferably 3.10+ for current versions).","message":"Support for Python 3.8 was dropped in PyObjC version 11.0.","severity":"breaking","affected_versions":">=11.0"},{"fix":"Migrate away from `IMServicePlugIn` to modern macOS APIs, as the framework is no longer supported by Apple or PyObjC.","message":"The `IMServicePlugIn` framework bindings were removed in PyObjC 10.0 as the entire framework was deprecated in macOS 10.13 and removed in macOS 14.","severity":"breaking","affected_versions":">=10.0"},{"fix":"If implementing custom object creation, ensure `__new__` is handled carefully. Avoid using `__init__` if relying on PyObjC's default `__new__` implementation for Objective-C classes, or upgrade to 10.3.1+ if a user-defined `__new__` requires `__init__`.","message":"PyObjC 10.3 initially broke the ability to use `__init__` when a class defined `__new__`. While partially re-enabled in 10.3.1 for user-defined `__new__` methods, `__init__` cannot be used with `__new__` provided by PyObjC itself.","severity":"gotcha","affected_versions":"10.3 - 10.3.x"},{"fix":"Developers working with custom `init` methods in Objective-C classes wrapped by PyObjC should be aware of this change in reference counting behavior to prevent memory leaks or premature deallocation. Consult Apple's ARC documentation for details.","message":"PyObjC 11.1 aligned the core bridge's behavior with `clang's` documentation for Automatic Reference Counting (ARC) regarding initializer methods. Methods in the 'init' family now correctly steal a reference to `self` and return a new reference.","severity":"gotcha","affected_versions":">=11.1"},{"fix":"Consult the PyObjC API notes for the Security framework and Apple's documentation for modern alternatives. File an issue with PyObjC if a critical API is missing.","message":"Several legacy Security.framework APIs, such as `SecKeychainItemCopyAttributesAndData` and `SecIdentityCopyPreference`, are not available from Python as they are soft-deprecated or explicitly not exposed by PyObjC.","severity":"deprecated","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}