MCPcopy Create free account
hub / github.com/numpy/numpy / build_module_distutils

Function build_module_distutils

numpy/f2py/tests/util.py:285–345  ·  view source on GitHub ↗

Build a module via distutils and import it.

(source_files, config_code, module_name, **kw)

Source from the content-addressed store, hash-verified

283
284@_memoize
285def build_module_distutils(source_files, config_code, module_name, **kw):
286 """
287 Build a module via distutils and import it.
288
289 """
290 d = get_module_dir()
291
292 # Copy files
293 dst_sources = []
294 for fn in source_files:
295 if not os.path.isfile(fn):
296 raise RuntimeError("%s is not a file" % fn)
297 dst = os.path.join(d, os.path.basename(fn))
298 shutil.copyfile(fn, dst)
299 dst_sources.append(dst)
300
301 # Build script
302 config_code = textwrap.dedent(config_code).replace("\n", "\n ")
303
304 code = fr"""
305import os
306import sys
307sys.path = {repr(sys.path)}
308
309def configuration(parent_name='',top_path=None):
310 from numpy.distutils.misc_util import Configuration
311 config = Configuration('', parent_name, top_path)
312 {config_code}
313 return config
314
315if __name__ == "__main__":
316 from numpy.distutils.core import setup
317 setup(configuration=configuration)
318 """
319 script = os.path.join(d, get_temp_module_name() + ".py")
320 dst_sources.append(script)
321 with open(script, "wb") as f:
322 f.write(code.encode('latin1'))
323
324 # Build
325 cwd = os.getcwd()
326 try:
327 os.chdir(d)
328 cmd = [sys.executable, script, "build_ext", "-i"]
329 p = subprocess.Popen(cmd,
330 stdout=subprocess.PIPE,
331 stderr=subprocess.STDOUT)
332 out, err = p.communicate()
333 if p.returncode != 0:
334 raise RuntimeError("Running distutils build failed: %s\n%s" %
335 (cmd[4:], asstr(out)))
336 finally:
337 os.chdir(cwd)
338
339 # Partial cleanup
340 for fn in dst_sources:
341 os.unlink(fn)
342

Callers

nothing calls this directly

Calls 8

asstrFunction · 0.90
get_module_dirFunction · 0.85
get_temp_module_nameFunction · 0.85
openFunction · 0.85
replaceMethod · 0.80
encodeMethod · 0.80
joinMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected