3 # Copyright 2007 Google Inc.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 """Stub replacement for Python's imp module."""
25 PY_SOURCE
, PY_COMPILED
, C_EXTENSION
= 1, 2, 3
26 PKG_DIRECTORY
, C_BUILTIN
, PY_FROZEN
= 5, 6, 7
30 """Return the magic string used to recognize byte-compiled code files."""
34 _PY_SOURCE_SUFFIX
= ('.py', 'U', PY_SOURCE
)
35 _PKG_DIRECTORY_SUFFIX
= ('', '', PKG_DIRECTORY
)
39 """Return a list that describes the files that find_module() looks for."""
40 return [_PY_SOURCE_SUFFIX
]
43 def find_module(name
, path
=None):
44 """Try to find the named module on the given search path or sys.path."""
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
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
58 raise ImportError('No module named %s' % name
)
61 def load_module(name
, file_
, pathname
, description
):
62 """Load or reload the specified module.
64 Please note that this function has only rudimentary supported on App Engine:
65 Only loading packages is supported.
67 suffix
, mode
, type_
= description
68 if type_
== PKG_DIRECTORY
:
69 if name
in sys
.modules
:
70 mod
= sys
.modules
[name
]
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
__)
79 raise NotImplementedError('Only importing packages is supported on '
84 """Return a new empty module object."""
85 return type(sys
)(name
)
89 """Return False since threading is not supported."""
94 """Acquiring the lock is a no-op since no threading is supported."""
99 """There is no lock to release since acquiring is a no-op when there is no
104 def init_builtin(name
):
105 raise NotImplementedError('This function is not supported on App Engine.')
108 def init_frozen(name
):
109 raise NotImplementedError('This function is not supported on App Engine.')
112 def is_builtin(name
):
113 return name
in sys
.builtin_module_names
120 def load_compiled(name
, pathname
, file_
=None):
121 raise NotImplementedError('This function is not supported on App Engine.')
124 def load_dynamic(name
, pathname
, file_
=None):
125 raise NotImplementedError('This function is not supported on App Engine.')
128 def load_source(name
, pathname
, file_
=None):
129 raise NotImplementedError('This function is not supported on App Engine.')
132 class NullImporter(object):
133 """Null importer object"""
135 def __init__(self
, path_string
):
137 raise ImportError("empty pathname")
138 elif os
.path
.isdir(path_string
):
139 raise ImportError("existing directory")
141 def find_module(self
, fullname
):