在python中调用secitemcopymatching

时间:2015-11-06 01:43:37

标签: python pyobjc

使用python(Mac python上的新手)从Keychain读取项目

这就是我在googlepedia中发现的几件事情。

from ctypes import CDll, byref, Structure, POINTER
from Foundation import NSDictionary
class OpaqueObject:
   pass
OpaquePtr = POINTER(OpaqueObject)
Security = CDLL('/System/Library......../Security')
query = NSDictionary.dictionaryWithDictionary({<still working on this part>})
items = OpauePtr()
Security.SecItemCopyMatching(query, byref(items))

{仍在使用此部分},目前正在读取{“foo”:“bar”},这当然是一个无效的查询,但至少应该运行

无论如何,它在SecItemCopyMatching的调用上失败,说它知道如何转换param1。我知道该函数被定义为采用CFDictionary但我期望免费桥接接受NSDictionary

无论如何,我怀疑这是混合2个mac python机制ctypes和PyObjc的坏代码。

1 个答案:

答案 0 :(得分:0)

免费桥接不适用于Python ctypes。请考虑使用CFDictionary代替:

CoreFoundation = CDLL('/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation')

CoreFoundation.CFDictionaryCreateMutable.restype = OpaquePtr
CoreFoundation.CFStringCreateWithBytes.restype = OpaquePtr
def CFDictionaryAddStringKeyValue(d, k, v):
    ck = CoreFoundation.CFStringCreateWithBytes(None, k, len(k), 0, 0)
    cv = CoreFoundation.CFStringCreateWithBytes(None, v, len(v), 0, 0)
    CoreFoundation.CFDictionaryAddValue(d, ck, cv)
    CoreFoundation.CFRelease(ck)
    CoreFoundation.CFRelease(cv)

query = CoreFoundation.CFDictionaryCreateMutable(None, 0, None, None)
CFDictionaryAddStringKeyValue(query, "foo", "bar")

然后,您可以将query传递给SecItemCopyMatching