Commit eaf2ce5d authored by Stefan Behnel's avatar Stefan Behnel

clean up some code

parent 4c21a8f1
......@@ -48,6 +48,7 @@ class UnboundSymbols(EnvTransform, SkipDeclarations):
super(UnboundSymbols, self).__call__(node)
return self.unbound
@cached_function
def unbound_symbols(code, context=None):
code = to_unicode(code)
......@@ -67,6 +68,7 @@ def unbound_symbols(code, context=None):
import __builtin__ as builtins
return UnboundSymbols()(tree) - set(dir(builtins))
def unsafe_type(arg, context=None):
py_type = type(arg)
if py_type is int:
......@@ -74,6 +76,7 @@ def unsafe_type(arg, context=None):
else:
return safe_type(arg, context)
def safe_type(arg, context=None):
py_type = type(arg)
if py_type in [list, tuple, dict, str]:
......@@ -97,6 +100,7 @@ def safe_type(arg, context=None):
return '%s.%s' % (base_type.__module__, base_type.__name__)
return 'object'
def _get_build_extension():
dist = Distribution()
# Ensure the build respects distutils configuration by parsing
......@@ -107,6 +111,7 @@ def _get_build_extension():
build_extension.finalize_options()
return build_extension
@cached_function
def _create_context(cython_include_dirs):
return Context(list(cython_include_dirs), default_options)
......@@ -263,7 +268,6 @@ def extract_func_code(code):
return '\n'.join(module), ' ' + '\n '.join(function)
try:
from inspect import getcallargs
except ImportError:
......@@ -294,6 +298,7 @@ except ImportError:
raise TypeError("Missing argument: %s" % name)
return all
def get_body(source):
ix = source.index(':')
if source[:5] == 'lambda':
......@@ -301,6 +306,7 @@ def get_body(source):
else:
return source[ix+1:]
# Lots to be done here... It would be especially cool if compiled functions
# could invoke each other quickly.
class RuntimeCompiledFunction(object):
......
......@@ -86,6 +86,7 @@ def parse_from_strings(name, code, pxds={}, level=None, initial_pos=None,
tree.scope = scope
return tree
class TreeCopier(VisitorTransform):
def visit_Node(self, node):
if node is None:
......@@ -95,6 +96,7 @@ class TreeCopier(VisitorTransform):
self.visitchildren(c)
return c
class ApplyPositionAndCopy(TreeCopier):
def __init__(self, pos):
super(ApplyPositionAndCopy, self).__init__()
......@@ -105,6 +107,7 @@ class ApplyPositionAndCopy(TreeCopier):
copy.pos = self.pos
return copy
class TemplateTransform(VisitorTransform):
"""
Makes a copy of a template tree while doing substitutions.
......@@ -247,6 +250,7 @@ class TreeFragment(object):
substitutions = nodes,
temps = self.temps + temps, pos = pos)
class SetPosTransform(VisitorTransform):
def __init__(self, pos):
super(SetPosTransform, self).__init__()
......
......@@ -62,19 +62,23 @@ PYXBLD_EXT = ".pyxbld"
DEBUG_IMPORT = False
def _print(message, args):
if args:
message = message % args
print(message)
def _debug(message, *args):
if DEBUG_IMPORT:
_print(message, args)
def _info(message, *args):
_print(message, args)
# Performance problem: for every PYX file that is imported, we will
# Performance problem: for every PYX file that is imported, we will
# invoke the whole distutils infrastructure even if the module is
# already built. It might be more efficient to only do it when the
# mod time of the .pyx is newer than the mod time of the .so but
......@@ -84,6 +88,7 @@ def _info(message, *args):
def _load_pyrex(name, filename):
"Load a pyrex file given a name and filename."
def get_distutils_extension(modname, pyxfilename, language_level=None):
# try:
# import hashlib
......@@ -103,6 +108,7 @@ def get_distutils_extension(modname, pyxfilename, language_level=None):
extension_mod.cython_directives = {'language_level': language_level}
return extension_mod,setup_args
def handle_special_build(modname, pyxfilename):
special_build = os.path.splitext(pyxfilename)[0] + PYXBLD_EXT
ext = None
......@@ -129,6 +135,7 @@ def handle_special_build(modname, pyxfilename):
for source in ext.sources]
return ext, setup_args
def handle_dependencies(pyxfilename):
testing = '_test_files' in globals()
dependfile = os.path.splitext(pyxfilename)[0] + PYXDEP_EXT
......@@ -166,6 +173,7 @@ def handle_dependencies(pyxfilename):
if testing:
_test_files.append(file)
def build_module(name, pyxfilename, pyxbuild_dir=None, inplace=False, language_level=None):
assert os.path.exists(pyxfilename), (
"Path does not exist: %s" % pyxfilename)
......@@ -197,6 +205,7 @@ def build_module(name, pyxfilename, pyxbuild_dir=None, inplace=False, language_l
return so_path
def load_module(name, pyxfilename, pyxbuild_dir=None, is_package=False,
build_inplace=False, language_level=None, so_path=None):
try:
......@@ -314,6 +323,7 @@ class PyxImporter(object):
_debug("%s not found" % fullname)
return None
class PyImporter(PyxImporter):
"""A meta-path importer for normal .py files.
"""
......@@ -384,6 +394,7 @@ class PyImporter(PyxImporter):
self.blocked_modules.pop()
return importer
class LibLoader(object):
def __init__(self):
self._libs = {}
......@@ -404,6 +415,7 @@ class LibLoader(object):
_lib_loader = LibLoader()
class PyxLoader(object):
def __init__(self, fullname, path, init_path=None, pyxbuild_dir=None,
inplace=False, language_level=None):
......@@ -442,7 +454,8 @@ class PyxArgs(object):
build_in_temp=True
setup_args={} #None
##pyxargs=None
##pyxargs=None
def _have_importers():
has_py_importer = False
......@@ -456,6 +469,7 @@ def _have_importers():
return has_py_importer, has_pyx_importer
def install(pyximport=True, pyimport=False, build_dir=None, build_in_temp=True,
setup_args={}, reload_support=False,
load_py_module_on_import_failure=False, inplace=False,
......@@ -532,6 +546,7 @@ def install(pyximport=True, pyimport=False, build_dir=None, build_in_temp=True,
return py_importer, pyx_importer
def uninstall(py_importer, pyx_importer):
"""
Uninstall an import hook.
......@@ -546,6 +561,7 @@ def uninstall(py_importer, pyx_importer):
except ValueError:
pass
# MAIN
def show_docs():
......@@ -559,5 +575,6 @@ def show_docs():
pass
help(__main__)
if __name__ == '__main__':
show_docs()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment