thirdparty/google_appengine/google/appengine/dist/py_imp.py
changeset 2273 e4cb9c53db3e
parent 1278 a7766286a7be
equal deleted inserted replaced
2272:26491ee91e33 2273:e4cb9c53db3e
    25 PY_SOURCE, PY_COMPILED, C_EXTENSION = 1, 2, 3
    25 PY_SOURCE, PY_COMPILED, C_EXTENSION = 1, 2, 3
    26 PKG_DIRECTORY, C_BUILTIN, PY_FROZEN = 5, 6, 7
    26 PKG_DIRECTORY, C_BUILTIN, PY_FROZEN = 5, 6, 7
    27 
    27 
    28 
    28 
    29 def get_magic():
    29 def get_magic():
       
    30   """Return the magic string used to recognize byte-compiled code files."""
    30   return '\0\0\0\0'
    31   return '\0\0\0\0'
    31 
    32 
    32 
    33 
       
    34 _PY_SOURCE_SUFFIX = ('.py', 'U', PY_SOURCE)
       
    35 _PKG_DIRECTORY_SUFFIX = ('', '', PKG_DIRECTORY)
       
    36 
       
    37 
    33 def get_suffixes():
    38 def get_suffixes():
    34   return [('.py', 'U', PY_SOURCE)]
    39   """Return a list that describes the files that find_module() looks for."""
       
    40   return [_PY_SOURCE_SUFFIX]
       
    41 
       
    42 
       
    43 def find_module(name, path=None):
       
    44   """Try to find the named module on the given search path or sys.path."""
       
    45   if path == None:
       
    46     path = sys.path
       
    47 
       
    48   for directory in path:
       
    49     filename = os.path.join(directory, '%s.py' % name)
       
    50     if os.path.exists(filename):
       
    51       return open(filename, 'U'), filename, _PY_SOURCE_SUFFIX
       
    52 
       
    53     dirname = os.path.join(directory, name)
       
    54     filename = os.path.join(dirname, '__init__.py')
       
    55     if os.path.exists(filename):
       
    56       return None, dirname, _PKG_DIRECTORY_SUFFIX
       
    57 
       
    58   raise ImportError('No module named %s' % name)
       
    59 
       
    60 
       
    61 def load_module(name, file_, pathname, description):
       
    62   """Load or reload the specified module.
       
    63 
       
    64   Please note that this function has only rudimentary supported on App Engine:
       
    65   Only loading packages is supported.
       
    66   """
       
    67   suffix, mode, type_ = description
       
    68   if type_ == PKG_DIRECTORY:
       
    69     if name in sys.modules:
       
    70       mod = sys.modules[name]
       
    71     else:
       
    72       mod = new_module(name)
       
    73       sys.modules[name] = mod
       
    74     filename = os.path.join(pathname, '__init__.py')
       
    75     mod.__file__ = filename
       
    76     execfile(filename, mod.__dict__, mod.__dict__)
       
    77     return mod
       
    78   else:
       
    79     raise NotImplementedError('Only importing packages is supported on '
       
    80                               'App Engine')
    35 
    81 
    36 
    82 
    37 def new_module(name):
    83 def new_module(name):
    38   return type(sys.modules[__name__])(name)
    84   """Return a new empty module object."""
       
    85   return type(sys)(name)
    39 
    86 
    40 
    87 
    41 def lock_held():
    88 def lock_held():
    42   """Return False since threading is not supported."""
    89   """Return False since threading is not supported."""
    43   return False
    90   return False
    44 
    91 
       
    92 
    45 def acquire_lock():
    93 def acquire_lock():
    46   """Acquiring the lock is a no-op since no threading is supported."""
    94   """Acquiring the lock is a no-op since no threading is supported."""
    47   pass
    95   pass
       
    96 
    48 
    97 
    49 def release_lock():
    98 def release_lock():
    50   """There is no lock to release since acquiring is a no-op when there is no
    99   """There is no lock to release since acquiring is a no-op when there is no
    51   threading."""
   100   threading."""
    52   pass
   101   pass
       
   102 
       
   103 
       
   104 def init_builtin(name):
       
   105   raise NotImplementedError('This function is not supported on App Engine.')
       
   106 
       
   107 
       
   108 def init_frozen(name):
       
   109   raise NotImplementedError('This function is not supported on App Engine.')
    53 
   110 
    54 
   111 
    55 def is_builtin(name):
   112 def is_builtin(name):
    56   return name in sys.builtin_module_names
   113   return name in sys.builtin_module_names
    57 
   114 
    58 
   115 
    59 def is_frozen(name):
   116 def is_frozen(name):
    60   return False
   117   return False
    61 
   118 
    62 
   119 
       
   120 def load_compiled(name, pathname, file_=None):
       
   121   raise NotImplementedError('This function is not supported on App Engine.')
       
   122 
       
   123 
       
   124 def load_dynamic(name, pathname, file_=None):
       
   125   raise NotImplementedError('This function is not supported on App Engine.')
       
   126 
       
   127 
       
   128 def load_source(name, pathname, file_=None):
       
   129   raise NotImplementedError('This function is not supported on App Engine.')
       
   130 
       
   131 
    63 class NullImporter(object):
   132 class NullImporter(object):
       
   133   """Null importer object"""
    64 
   134 
    65   def __init__(self, path_string):
   135   def __init__(self, path_string):
    66     if not path_string:
   136     if not path_string:
    67       raise ImportError("empty pathname")
   137       raise ImportError("empty pathname")
    68     elif os.path.isdir(path_string):
   138     elif os.path.isdir(path_string):