1 import os 2 import sys 3 import unittest 4 5 # Bob Ippolito: 6 """ 7 Ok.. the code to find the filename for __getattr__ should look 8 something like: 9 10 import os 11 from macholib.dyld import dyld_find 12 13 def find_lib(name): 14 possible = ['lib'+name+'.dylib', name+'.dylib', 15 name+'.framework/'+name] 16 for dylib in possible: 17 try: 18 return os.path.realpath(dyld_find(dylib)) 19 except ValueError: 20 pass 21 raise ValueError, "%s not found" % (name,) 22 23 It'll have output like this: 24 25 >>> find_lib('pthread') 26 '/usr/lib/libSystem.B.dylib' 27 >>> find_lib('z') 28 '/usr/lib/libz.1.dylib' 29 >>> find_lib('IOKit') 30 '/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit' 31 32 -bob 33 34 """ 35 36 from ctypes.macholib.dyld import dyld_find 37 38 def find_lib(name): 39 possible = ['lib'+name+'.dylib', name+'.dylib', name+'.framework/'+name] 40 for dylib in possible: 41 try: 42 return os.path.realpath(dyld_find(dylib)) 43 except ValueError: 44 pass 45 raise ValueError("%s not found" % (name,)) 46 47 class MachOTest(unittest.TestCase): 48 @unittest.skipUnless(sys.platform == "darwin", 'OSX-specific test') 49 def test_find(self): 50 51 self.assertEqual(find_lib('pthread'), 52 '/usr/lib/libSystem.B.dylib') 53 54 result = find_lib('z') 55 # Issue #21093: dyld default search path includes $HOME/lib and 56 # /usr/local/lib before /usr/lib, which caused test failures if 57 # a local copy of libz exists in one of them. Now ignore the head 58 # of the path. 59 self.assertRegexpMatches(result, r".*/lib/libz\..*.*\.dylib") 60 61 self.assertEqual(find_lib('IOKit'), 62 '/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit') 63 64 if __name__ == "__main__": 65 unittest.main() 66