1__all__ = ['inject', 'signature']
2
3import os
4import sys
5
6def _ensure_path(p):
7    p = os.path.realpath(p)
8    if isinstance(p, unicode):
9        p = p.encode(sys.getfilesystemencoding())
10    return p
11
12def inject(pid, bundle, useMainThread=True):
13    """Loads the given MH_BUNDLE in the target process identified by pid"""
14    try:
15        from _objc import _inject
16        from _dyld import dyld_find
17    except ImportError:
18        raise NotImplementedError("objc.inject is only supported on Mac OS X 10.3 and later")
19    bundlePath = bundle
20    systemPath = dyld_find('/usr/lib/libSystem.dylib')
21    carbonPath = dyld_find('/System/Library/Frameworks/Carbon.framework/Carbon')
22    paths = map(_ensure_path, (bundlePath, systemPath, carbonPath))
23    return _inject(
24        pid,
25        useMainThread,
26        *paths
27    )
28
29def signature(signature, **kw):
30    """
31    A Python method decorator that allows easy specification
32    of Objective-C selectors.
33
34    Usage::
35
36        @objc.signature('i@:if')
37        def methodWithX_andY_(self, x, y):
38            return 0
39    """
40    from _objc import selector
41    kw['signature'] = signature
42    def makeSignature(func):
43        return selector(func, **kw)
44    return makeSignature
45