Issue #23 resolved

Add ctypes support

Ronald Oussoren avatarRonald Oussoren created an issue

from an e-mail on pyobjc-dev:

I'm getting an error when calling SMJobBless - "ValueError: depythonifying 'pointer', got 'LP_c_void_p'". I've tried a few things without luck. Any help will be appreciated.

Here's my code -

from ServiceManagement import (
    kSMDomainSystemLaunchd,
    SMJobBless,
)

security = ctypes.cdll.LoadLibrary(ctypes.util.find_library('Security'))

class AuthorizationItem(ctypes.Structure):
    _fields_ = [
        ('name', ctypes.c_char_p),
        ('valueLength', ctypes.c_uint32),
        ('value', ctypes.c_void_p),
        ('flags', ctypes.c_uint32),
    ]

class AuthorizationRights(ctypes.Structure):
    _fields_ = [
        ('count', ctypes.c_uint32),
        ('items', ctypes.POINTER(AuthorizationItem)),
    ]

authorization_item = AuthorizationItem(
    name='com.apple.ServiceManagement.blesshelper',
    valueLength=0,
    value=None,
    flags=0,
)
authorization_rights = AuthorizationRights(
    count=1,
    items=ctypes.pointer(authorization_item),
)
authorization_flags = 19
authorization_ref = ctypes.POINTER(ctypes.c_void_p)()
status = security.AuthorizationCreate(
    ctypes.pointer(authorization_rights),
    None,
    authorization_flags,
    ctypes.pointer(authorization_ref),
)
SMJobBless(
    kSMDomainSystemLaunchd,
    'app.helper',
    authorization_ref,
    None,
)

PyObjC should recognize the ctypes pointer types and should assume that the user knows what he's doing and pass the C pointer wrapped by ctypes to the C function.

Comments (3)

  1. Ronald Oussoren

    I intend to add functions (or method) for converting PyObjC objects to/from ctypes (void) pointers.

    This will likely have a similar interface as the already existing support for PyCapsule objects, something like:

    o1 = NSObject.alloc().init()
    c_ptr = o1.__ctypes_ptr__()
    
    o2 = NSObject(ctypes_ptr=c_ptr)
    assert o1 is o2
    
  2. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.