Debian Codegraph @

lib_pypy/_pypy_testcapi.py

raw symbols SCIP
1 import os, sys
2
3 import tempfile, binascii
4 import importlib.machinery
5 from importlib.util import spec_from_file_location, module_from_spec
6
7
8 def _get_hashed_filename(cfile):
9 with open(cfile,'r') as fid:
10 content = fid.read()
11 # from cffi's Verifier()
12 key = '\x00'.join([sys.version[:3], content])
13 # change the key to force recompilation
14 key += '2017-11-21'
15 if sys.version_info >= (3,):
16 key = key.encode('utf-8')
17 k1 = hex(binascii.crc32(key[0::2]) & 0xffffffff)
18 k1 = k1.lstrip('0x').rstrip('L')
19 k2 = hex(binascii.crc32(key[1::2]) & 0xffffffff)
20 k2 = k2.lstrip('0').rstrip('L')
21 try:
22 username = os.environ['USER'] #linux, et al
23 except KeyError:
24 try:
25 username = os.environ['USERNAME'] #windows
26 except KeyError:
27 username = os.getuid()
28 return tempfile.gettempdir() + os.path.sep + 'testcapi_%s_%s%s' % (
29 username, k1, k2)
30
31 def get_hashed_dir(cfile):
32 hashed_fn = _get_hashed_filename(cfile)
33 try:
34 with open(hashed_fn) as f:
35 dirname = f.read(1024)
36 except IOError:
37 dirname = ''
38 tmpdir = tempfile.gettempdir()
39 if (not dirname or '/' in dirname or '\\' in dirname or '\x00' in dirname
40 or not os.path.isdir(os.path.join(tmpdir, dirname))):
41 dirname = binascii.hexlify(os.urandom(8))
42 if not isinstance(dirname, str): # Python 3
43 dirname = dirname.decode('ascii')
44 dirname = 'testcapi_' + dirname
45 output_dir = os.path.join(tmpdir, dirname)
46 try:
47 os.mkdir(output_dir)
48 except OSError:
49 pass
50 return output_dir
51
52
53 def _get_c_extension_suffix():
54 suffixes = importlib.machinery.EXTENSION_SUFFIXES
55 return suffixes[0] if suffixes else None
56
57
58 def compile_shared(csource, modulename, output_dir):
59 """Compile '_testcapi.c' or '_ctypes_test.c' into an extension module,
60 and import it.
61 """
62 thisdir = os.path.dirname(__file__)
63 assert output_dir is not None
64
65 from distutils.ccompiler import new_compiler
66 from distutils import log, sysconfig
67 log.set_verbosity(3)
68
69 compiler = new_compiler()
70 compiler.output_dir = output_dir
71 # Compile .c file
72 include_dir = sysconfig.get_config_var('INCLUDEPY')
73 if sys.platform == 'win32':
74 ccflags = ['-D_CRT_SECURE_NO_WARNINGS']
75 else:
76 ccflags = ['-fPIC', '-Wimplicit-function-declaration', '-O0', '-g3']
77 sysconfig.customize_compiler(compiler)
78 res = compiler.compile([os.path.join(thisdir, csource)],
79 include_dirs=[include_dir],
80 extra_preargs=ccflags,
81 )
82 object_filename = res[0]
83
84 # set link options
85 output_filename = modulename + _get_c_extension_suffix()
86 if sys.platform == 'win32':
87 libname = 'python{0[0]}{0[1]}'.format(sys.version_info)
88 library = os.path.join(thisdir, '..', 'libs', libname)
89 if not os.path.exists(library + '.lib'):
90 # For a local translation or nightly build
91 library = os.path.join(thisdir, '..', 'pypy', 'goal', libname)
92 assert os.path.exists(library + '.lib'), 'Could not find import library "%s"' % library
93 libraries = [library, 'oleaut32']
94 extra_ldargs = ['/MANIFEST', # needed for VC10
95 '/EXPORT:PyInit_' + modulename]
96 else:
97 libraries = []
98 extra_ldargs = []
99
100 # link the dynamic library
101 compiler.link_shared_object(
102 [object_filename],
103 output_filename,
104 libraries=libraries,
105 extra_preargs=extra_ldargs)
106
107 # Now import the newly created library, it will replace the original
108 # module in sys.modules
109 spec = spec_from_file_location(modulename,
110 os.path.join(output_dir, output_filename))
111 mod = module_from_spec(spec)
112
113 # If everything went fine up to now, write the name of this new
114 # directory to 'hashed_fn', for future processes (and to avoid a
115 # growing number of temporary directories that are not completely
116 # obvious to clean up on Windows)
117 hashed_fn = _get_hashed_filename(os.path.join(thisdir, csource))
118 try:
119 with open(hashed_fn, 'w') as f:
120 f.write(os.path.basename(output_dir))
121 except IOError:
122 pass
123 return mod