[pypy-svn] r70064 - pypy/branch/import-builtin/pypy/module/imp

afa at codespeak.net afa at codespeak.net
Fri Dec 11 01:02:25 CET 2009


Author: afa
Date: Fri Dec 11 01:02:24 2009
New Revision: 70064

Modified:
   pypy/branch/import-builtin/pypy/module/imp/__init__.py
   pypy/branch/import-builtin/pypy/module/imp/importing.py
   pypy/branch/import-builtin/pypy/module/imp/interp_imp.py
Log:
Translation fix and some clean-ups

Modified: pypy/branch/import-builtin/pypy/module/imp/__init__.py
==============================================================================
--- pypy/branch/import-builtin/pypy/module/imp/__init__.py	(original)
+++ pypy/branch/import-builtin/pypy/module/imp/__init__.py	Fri Dec 11 01:02:24 2009
@@ -1,13 +1,5 @@
 from pypy.interpreter.mixedmodule import MixedModule
 
-names = ['C_BUILTIN', 'C_EXTENSION', 'IMP_HOOK', 'NullImporter', 'PKG_DIRECTORY',
- 'PY_CODERESOURCE', 'PY_COMPILED', 'PY_FROZEN', 'PY_RESOURCE', 'PY_SOURCE',
- 'SEARCH_ERROR', '__doc__', '__name__', '__package__', 'acquire_lock', 'find_module',
- 'get_frozen_object', 'get_magic', 'get_suffixes', 'init_builtin', 'init_frozen',
- 'is_builtin', 'is_frozen', 'load_compiled', 'load_dynamic', 'load_module',
- 'load_package', 'load_source', 'lock_held', 'new_module', 'release_lock', 'reload']
-
-
 class Module(MixedModule):
     """
     This module provides the components needed to build your own
@@ -20,7 +12,7 @@
         'C_BUILTIN':       'space.wrap(importing.C_BUILTIN)',
         'get_suffixes':    'interp_imp.get_suffixes',
 
-        'get_magic':       'interp_imp.get_magic',
+        #'get_magic':       'interp_imp.get_magic',
         'find_module':     'interp_imp.find_module',
         'load_module':     'interp_imp.load_module',
         'load_source':     'interp_imp.load_source',
@@ -35,151 +27,3 @@
 
     appleveldefs = {
         }
-
-# PyPy-specific interface
-try:
-    import __pypy__
-    def get_magic():
-        """Return the magic number for .pyc or .pyo files."""
-        import struct
-        return struct.pack('<i', __pypy__.PYC_MAGIC)
-except ImportError:
-    # XXX CPython testing hack: delegate to the real imp.get_magic
-    get_magic = __import__('imp').get_magic
-
-def get_suffixes():
-    """Return a list of (suffix, mode, type) tuples describing the files
-    that find_module() looks for."""
-    return [('.py', 'U', PY_SOURCE),
-            ('.pyc', 'rb', PY_COMPILED)]
-
-
-def find_module(name, path=None):
-    """find_module(name, [path]) -> (file, filename, (suffix, mode, type))
-    Search for a module.  If path is omitted or None, search for a
-    built-in, frozen or special module and continue search in sys.path.
-    The module name cannot contain '.'; to search for a submodule of a
-    package, pass the submodule name and the package's __path__.
-    """
-    if path is None:
-        if name in sys.builtin_module_names:
-            return (None, name, ('', '', C_BUILTIN))
-        path = sys.path
-    for base in path:
-        filename = os.path.join(base, name)
-        if os.path.isdir(filename):
-            return (None, filename, ('', '', PKG_DIRECTORY))
-        for ext, mode, kind in get_suffixes():
-            if os.path.exists(filename+ext):
-                return (file(filename+ext, mode), filename+ext, (ext, mode, kind))
-    raise ImportError, 'No module named %s' % (name,)
-
-
-def load_module(name, file, filename, description):
-    """Load a module, given information returned by find_module().
-    The module name must include the full package name, if any.
-    """
-    suffix, mode, type = description
-
-    if type == PY_SOURCE:
-        return load_source(name, filename, file)
-
-    if type == PKG_DIRECTORY:
-        initfilename = os.path.join(filename, '__init__.py')
-        module = sys.modules.setdefault(name, new_module(name))
-        module.__name__ = name
-        module.__doc__ = None
-        module.__file__ = initfilename
-        module.__path__ = [filename]
-        execfile(initfilename, module.__dict__)
-        return module
-
-    if type == C_BUILTIN:
-        module = __import__(name, {}, {}, None)
-        return module
-    if type == PY_COMPILED:
-       return  load_compiled(name, filename, file)
-    raise ValueError, 'invalid description argument: %r' % (description,)
-
-def load_dynamic(name, *args, **kwds):
-    raise ImportError(name)
-
-def load_source(name, pathname, file=None):
-    autoopen = file is None
-    if autoopen:
-        file = open(pathname, 'U')
-    source = file.read()
-    if autoopen:
-        file.close()
-    co = compile(source, pathname, 'exec')
-    return run_module(name, pathname, co)
-
-def load_compiled(name, pathname, file=None):
-    import marshal
-    autoopen = file is None
-    if autoopen:
-        file = open(pathname, 'rb')
-    magic = file.read(4)
-    if magic != get_magic():
-        raise ImportError("Bad magic number in %s" % pathname)
-    file.read(4)    # skip timestamp
-    co = marshal.load(file)
-    if autoopen:
-        file.close()
-    return run_module(name, pathname, co)
-
-def run_module(name, pathname, co):
-    module = sys.modules.setdefault(name, new_module(name))
-    module.__name__ = name
-    module.__doc__ = None
-    module.__file__ = pathname
-    try:
-        exec co in module.__dict__
-    except :
-        sys.modules.pop(name,None)
-        raise
-    return module
- 
-
-def new_module(name):
-    """Create a new module.  Do not enter it in sys.modules.
-    The module name must include the full package name, if any.
-    """
-    return new.module(name)
-
-
-def init_builtin(name):
-    if name not in sys.builtin_module_names:
-        return None
-    if name in sys.modules:
-        raise ImportError("cannot initialize a built-in module twice "
-                          "in PyPy")
-    return __import__(name)
-
-def init_frozen(name):
-    return None
-
-def is_builtin(name):
-    if name in sys.builtin_module_names:
-        return -1   # cannot be initialized again
-    else:
-        return 0
-
-def is_frozen(name):
-    return False
-
-# ____________________________________________________________
-
-try:
-    # PyPy-specific interface
-    from thread import _importlock_held    as lock_held
-    from thread import _importlock_acquire as acquire_lock
-    from thread import _importlock_release as release_lock
-except ImportError:
-    def lock_held():
-        """On platforms without threads, return False."""
-        return False
-    def acquire_lock():
-        """On platforms without threads, this function does nothing."""
-    def release_lock():
-        """On platforms without threads, this function does nothing."""

Modified: pypy/branch/import-builtin/pypy/module/imp/importing.py
==============================================================================
--- pypy/branch/import-builtin/pypy/module/imp/importing.py	(original)
+++ pypy/branch/import-builtin/pypy/module/imp/importing.py	Fri Dec 11 01:02:24 2009
@@ -19,6 +19,7 @@
 PY_COMPILED = 2
 PKG_DIRECTORY = 3
 C_BUILTIN = 4
+IMP_HOOK = 5
 
 def find_modtype(space, filepart):
     """Check which kind of module to import for the given filepart,
@@ -247,24 +248,33 @@
         if space.is_true(w_loader):
             return w_loader
 
-def ImportInfo(type, name, stream, suffix="", filemode=""):
-    return type, name, stream, suffix, filemode
+class FindInfo:
+    def __init__(self, modtype, filename, stream,
+                 suffix="", filemode="", w_loader=None):
+        self.modtype = modtype
+        self.filename = filename
+        self.stream = stream
+        self.suffix = suffix
+        self.filemode = filemode
+        self.w_loader = w_loader
+
+    @staticmethod
+    def fromLoader(w_loader):
+        return FindInfo(IMP_HOOK, '', None, w_loader=w_loader)
 
 def find_module(space, modulename, w_modulename, partname, w_path, use_loader=True):
-    """If use_loader is False, returns (import_info, None)
-    if use_loader is True, may return (None, w_loader)"""
     # Examin importhooks (PEP302) before doing the import
     if use_loader:
         w_loader  = find_in_meta_path(space, w_modulename, w_path)
         if w_loader:
-            return None, w_loader
+            return FindInfo.fromLoader(w_loader)
 
     # XXX Check for frozen modules?
     #     when w_path is a string
 
     # check the builtin modules
     if modulename in space.builtin_modules:
-        return ImportInfo(C_BUILTIN, modulename, None), None
+        return FindInfo(C_BUILTIN, modulename, None)
 
     # XXX check frozen modules?
     #     when w_path is null
@@ -275,7 +285,7 @@
             if use_loader:
                 w_loader = find_in_path_hooks(space, w_modulename, w_pathitem)
                 if w_loader:
-                    return None, w_loader
+                    return FindInfo.fromLoader(w_loader)
 
             path = space.str_w(w_pathitem)
             filepart = os.path.join(path, partname)
@@ -283,7 +293,7 @@
                 initfile = os.path.join(filepart, '__init__')
                 modtype, _, _ = find_modtype(space, initfile)
                 if modtype in (PY_SOURCE, PY_COMPILED):
-                    return ImportInfo(PKG_DIRECTORY, filepart, None), None
+                    return FindInfo(PKG_DIRECTORY, filepart, None)
                 else:
                     msg = "Not importing directory " +\
                             "'%s' missing __init__.py" % (filepart,)
@@ -294,57 +304,60 @@
                     filename = filepart + suffix
                     stream = streamio.open_file_as_stream(filename, filemode)
                     try:
-                        return ImportInfo(modtype, filename, stream, suffix, filemode), None
+                        return FindInfo(modtype, filename, stream, suffix, filemode)
                     except:
                         stream.close()
                         raise
             except StreamErrors:
                 pass
-    return None, None
 
-def load_module(space, w_modulename, import_info, w_loader, ispkg=False):
-    if w_loader:
-        return space.call_method(w_loader, "load_module", w_modulename)
-    if import_info is None:
+    # not found
+    return None
+
+def load_module(space, w_modulename, find_info, ispkg=False):
+    if find_info is None:
         return
-    modtype, filename, stream, _, _ = import_info
-    if modtype == C_BUILTIN:
-        return space.getbuiltinmodule(filename)
+    if find_info.w_loader:
+        return space.call_method(find_info.w_loader, "load_module", w_modulename)
 
-    if modtype in (PY_SOURCE, PY_COMPILED, PKG_DIRECTORY):
+    if find_info.modtype == C_BUILTIN:
+        return space.getbuiltinmodule(find_info.filename)
+
+    if find_info.modtype in (PY_SOURCE, PY_COMPILED, PKG_DIRECTORY):
         if ispkg:
             w_mod = space.getitem(space.sys.get('modules'), w_modulename)
         else:
             w_mod = space.wrap(Module(space, w_modulename))
         space.sys.setmodule(w_mod)
-        space.setattr(w_mod, space.wrap('__file__'), space.wrap(filename))
+        space.setattr(w_mod, space.wrap('__file__'), space.wrap(find_info.filename))
         space.setattr(w_mod, space.wrap('__doc__'), space.w_None)
 
-    try:
-        if modtype == PY_SOURCE:
-            load_source_module(space, w_modulename, w_mod, filename,
-                               stream.readall())
-            return w_mod
-        elif modtype == PY_COMPILED:
-            magic = _r_long(stream)
-            timestamp = _r_long(stream)
-            load_compiled_module(space, w_modulename, w_mod, filename,
-                                 magic, timestamp, stream.readall())
-            return w_mod
-        elif modtype == PKG_DIRECTORY:
-            w_path = space.newlist([space.wrap(filename)])
-            space.setattr(w_mod, space.wrap('__path__'), w_path)
-            import_info, _ = find_module(space, "__init__", None, "__init__",
-                                         w_path, use_loader=False)
-            if import_info is None:
+        try:
+            if find_info.modtype == PY_SOURCE:
+                load_source_module(space, w_modulename, w_mod, find_info.filename,
+                                   find_info.stream.readall())
                 return w_mod
-            load_module(space, w_modulename, import_info, None, ispkg=True)
-            w_mod = check_sys_modules(space, w_modulename) # in case of "substitution"
-            return w_mod
-    except OperationError:
-        w_mods = space.sys.get('modules')
-        space.call_method(w_mods, 'pop', w_modulename, space.w_None)
-        raise
+            elif find_info.modtype == PY_COMPILED:
+                magic = _r_long(find_info.stream)
+                timestamp = _r_long(find_info.stream)
+                load_compiled_module(space, w_modulename, w_mod, find_info.filename,
+                                     magic, timestamp, find_info.stream.readall())
+                return w_mod
+            elif find_info.modtype == PKG_DIRECTORY:
+                w_path = space.newlist([space.wrap(find_info.filename)])
+                space.setattr(w_mod, space.wrap('__path__'), w_path)
+                find_info = find_module(space, "__init__", None, "__init__",
+                                        w_path, use_loader=False)
+                if find_info is None:
+                    return w_mod
+                load_module(space, w_modulename, find_info, ispkg=True)
+                # fetch the module again, in case of "substitution"
+                w_mod = check_sys_modules(space, w_modulename)
+                return w_mod
+        except OperationError:
+            w_mods = space.sys.get('modules')
+            space.call_method(w_mods, 'pop', w_modulename, space.w_None)
+            raise
 
 def load_part(space, w_path, prefix, partname, w_parent, tentative):
     w = space.wrap
@@ -355,18 +368,18 @@
         if not space.is_w(w_mod, space.w_None):
             return w_mod
     else:
-        import_info, w_loader = find_module(
+        find_info = find_module(
             space, modulename, w_modulename, partname, w_path)
 
         try:
-            if (import_info, w_loader) != (None, None):
-                w_mod = load_module(space, w_modulename, import_info, w_loader)
+            if find_info:
+                w_mod = load_module(space, w_modulename, find_info)
                 if w_parent is not None:
                     space.setattr(w_parent, space.wrap(partname), w_mod)
                 return w_mod
         finally:
-            if import_info:
-                _, _, stream, _, _ = import_info
+            if find_info:
+                stream = find_info.stream
                 if stream:
                     stream.close()
 
@@ -406,15 +419,15 @@
                     parent_name,)))
         w_path = space.getitem(w_parent, space.wrap("__path"))
 
-    import_info, w_loader = find_module(
+    find_info = find_module(
         space, modulename, w_modulename, subname, w_path)
 
-    if (import_info, w_loader) == (None, None):
+    if not find_info:
         # ImportError
         msg = "No module named %s" % modulename
         raise OperationError(space.w_ImportError, space.wrap(msg))
 
-    return load_module(space, w_modulename, import_info, w_loader)
+    return load_module(space, w_modulename, find_info)
 
 # __________________________________________________________________
 #

Modified: pypy/branch/import-builtin/pypy/module/imp/interp_imp.py
==============================================================================
--- pypy/branch/import-builtin/pypy/module/imp/interp_imp.py	(original)
+++ pypy/branch/import-builtin/pypy/module/imp/interp_imp.py	Fri Dec 11 01:02:24 2009
@@ -14,7 +14,15 @@
         ])
 
 def get_magic(space):
-    return space.wrap(struct.pack('<i', importing.get_pyc_magic(space)))
+    x = importing.get_pyc_magic(space)
+    a = x & 0xff
+    x >>= 8
+    b = x & 0xff
+    x >>= 8
+    c = x & 0xff
+    x >>= 8
+    d = x & 0xff
+    return space.wrap(chr(a) + chr(b) + chr(c) + chr(d))
 
 def get_file(space, w_file, filename, filemode):
     if w_file is None or space.is_w(w_file, space.w_None):
@@ -27,26 +35,28 @@
     if space.is_w(w_path, space.w_None):
         w_path = space.sys.get('path')
 
-    import_info, _ = importing.find_module(
+    find_info = importing.find_module(
         space, name, w_name, name, w_path, use_loader=False)
-    if import_info is None:
+    if not find_info:
         raise OperationError(
             space.w_ImportError,
             space.wrap("No module named %s" % (name,)))
-    modtype, filename, stream, suffix, filemode = import_info
 
-    w_filename = space.wrap(filename)
+    w_filename = space.wrap(find_info.filename)
+    stream = find_info.stream
 
     if stream is not None:
         fileobj = W_File(space)
         fileobj.fdopenstream(
             stream, stream.try_to_find_file_descriptor(),
-            filemode, w_filename)
+            find_info.filemode, w_filename)
         w_fileobj = space.wrap(fileobj)
     else:
         w_fileobj = space.w_None
     w_import_info = space.newtuple(
-        [space.wrap(suffix), space.wrap(filemode), space.wrap(modtype)])
+        [space.wrap(find_info.suffix),
+         space.wrap(find_info.filemode),
+         space.wrap(find_info.modtype)])
     return space.newtuple([w_fileobj, w_filename, w_import_info])
 
 def load_module(space, w_name, w_file, w_filename, w_info):
@@ -59,13 +69,14 @@
     else:
         stream = get_file(space, w_file, filename, filemode)
 
-    import_info = (space.int_w(w_modtype),
-                   filename,
-                   stream,
-                   space.str_w(w_suffix),
-                   filemode)
+    find_info = importing.FindInfo(
+        space.int_w(w_modtype),
+        filename,
+        stream,
+        space.str_w(w_suffix),
+        filemode)
     return importing.load_module(
-        space, w_name, import_info, None)
+        space, w_name, find_info)
 
 def load_source(space, w_modulename, w_filename, w_file=None):
     filename = space.str_w(w_filename)



More information about the Pypy-commit mailing list