PyObjC SecurityFoundation Framework
PyObjC SecurityFoundation provides Python wrappers for the macOS SecurityFoundation framework, enabling Python scripts to interact with authorization services and other security-related APIs on macOS. It is part of the larger PyObjC project, which regularly releases updates, with the current version being 12.1.
Warnings
- breaking Python Version Support Drops: PyObjC has a policy of dropping support for older Python versions. PyObjC 12.0 dropped support for Python 3.9, and PyObjC 11.0 dropped support for Python 3.8. Always consult the release notes to ensure compatibility with your target Python and macOS versions.
- breaking Changes in `__init__` and `__new__` behavior: In PyObjC 10.3, calling `__init__` on subclasses that rely on PyObjC's `__new__` was disabled. While partially reintroduced in 10.3.1 for user-implemented `__new__`, code relying on the PyObjC-provided `__new__` cannot use `__init__`. This can break existing subclassing patterns.
- breaking Removal of `IMServicePlugIn` Bindings: The `IMServicePlugIn` framework bindings were entirely removed in PyObjC 10.0, as the underlying framework was deprecated by Apple in macOS 10.13 and removed in macOS 14.
- gotcha KVO (Key-Value Observing) for `NSProxy` Subclasses: As of PyObjC 12.1, KVO usage is automatically disabled for Python subclasses of `NSProxy`. Attempting to use KVO with these subclasses can lead to `SystemError`.
- gotcha `os.fspath` with Cocoa URLs: Prior to PyObjC 10.1, `os.fspath()` would raise a `TypeError` when used with Cocoa URL objects (`NSURL`, `CFURLRef`) that referred to local filesystem paths. This was fixed in 10.1.
- gotcha `bytearray` as C `char*` argument: In versions prior to 12.1, passing a `bytearray` directly to an Objective-C function or selector expecting a null-terminated C char array might have required manual conversion. As of 12.1, `bytearray` instances can be used directly.
Install
-
pip install pyobjc-framework-securityfoundation
Imports
- SFAuthorization
from SecurityFoundation import SFAuthorization
Quickstart
import SecurityFoundation
import objc
# Get a default SFAuthorization object
auth = SecurityFoundation.SFAuthorization.authorization()
if auth:
print(f"Successfully obtained SFAuthorization object: {auth}")
# Further operations would involve using methods on the 'auth' object
# to perform authorization checks or privilege escalation.
else:
print("Failed to obtain SFAuthorization object.")