def scanfuncs(filename, prefixes, cython=False): """ Return list of function names from ``filename`` that begin with prefix. This *does not* import the Python file, so this is safe to use, but functionality is limited to retrieving names of basic functions defined within global scope of the file. This *does*, however, import Cython files (if applicable). """ # Used by: findarenas, findbenchmarks path, name = os.path.split(filename) name, ext = os.path.splitext(name) # Should `cython` be a keyword argument, or should we just infer it? cython = cython or ext == '.pyx' if not cython: funcs = pyclbr.readmodule_ex(name, [path]) funcnames = [] for key, val in funcs.items(): if (any(key.startswith(prefix) for prefix in prefixes) and val.file == filename): funcnames.append(key) return funcnames # Scan Cython file. We need to import it. import pyximport pyximport.install() sys.dont_write_bytecode = True # Make sure local imports work for the given file sys.path.insert(0, path) pyximport.build_module(name, filename) try: mod = pyximport.load_module(name, filename) except ImportError: # There is most likely a '*.py' file that shares the same # base name as the '*.pyx' file we are trying to import. # Removing the directory from sys.path should fix this, # but will disable local importing. sys.path.pop(0) mod = pyximport.load_module(name, filename) # Undo making local imports work if sys.path[0] == path: sys.path.pop(0) funcnames = [] for funcname in mod.__dict__: if any(funcname.startswith(prefix) for prefix in prefixes): funcnames.append(funcname) return funcnames
# 3. load_dynamic will load the module but not extend the globald # directory. We rely on the fact that the loading has been already # performed and call the import * explicitely # # Since the .so is compiled outside of the PYTHON_PATH, there is # no ambiguity when importing the parser: the only way to load the # cython version is by the so_path that targets .pyxbld . # import imp pyx = pyximport.install() pyximport.uninstall(*pyx) build_dir = os.path.join(os.path.expanduser('~'), '.pyxbld') path = os.path.join(os.path.dirname(__file__), "parser.py") name="pysmt.smtlib.parser.parser" so_path = pyximport.build_module(name, path, pyxbuild_dir=build_dir) mod = imp.load_dynamic(name, so_path) assert mod.__file__ == so_path, (mod.__file__, so_path) # print(so_path) from pysmt.smtlib.parser.parser import * # End of preamble # # # if __name__ == "__main__": import sys def main():
# 3. load_dynamic will load the module but not extend the globald # directory. We rely on the fact that the loading has been already # performed and call the import * explicitely # # Since the .so is compiled outside of the PYTHON_PATH, there is # no ambiguity when importing the parser: the only way to load the # cython version is by the so_path that targets .pyxbld . # import imp pyx = pyximport.install() pyximport.uninstall(*pyx) build_dir = os.path.join(os.path.expanduser('~'), '.pyxbld') path = os.path.join(os.path.dirname(__file__), "parser.py") name = "pysmt.smtlib.parser.parser" so_path = pyximport.build_module(name, path, pyxbuild_dir=build_dir) mod = imp.load_dynamic(name, so_path) assert mod.__file__ == so_path, (mod.__file__, so_path) # print(so_path) from pysmt.smtlib.parser.parser import * # End of preamble # # # if __name__ == "__main__": import sys def main(): """Simple testing script"""