Reindented, removed tabs.

This commit is contained in:
Guido van Rossum 1998-03-05 04:56:37 +00:00
parent 1e07403bbf
commit 912a14c028
2 changed files with 319 additions and 311 deletions

View File

@ -10,52 +10,56 @@ header = """
static struct _frozen _PyImport_FrozenModules[] = { static struct _frozen _PyImport_FrozenModules[] = {
""" """
trailer = """\ trailer = """\
{0, 0, 0} /* sentinel */ {0, 0, 0} /* sentinel */
}; };
int int
main(argc, argv) main(argc, argv)
int argc; int argc;
char **argv; char **argv;
{ {
PyImport_FrozenModules = _PyImport_FrozenModules; PyImport_FrozenModules = _PyImport_FrozenModules;
return Py_FrozenMain(argc, argv); return Py_FrozenMain(argc, argv);
} }
""" """
def makefreeze(outfp, dict, debug=0): def makefreeze(outfp, dict, debug=0):
done = [] done = []
mods = dict.keys() mods = dict.keys()
mods.sort() mods.sort()
for mod in mods: for mod in mods:
m = dict[mod] m = dict[mod]
mangled = string.join(string.split(mod, "."), "__") mangled = string.join(string.split(mod, "."), "__")
if m.__code__: if m.__code__:
if debug: if debug:
print "freezing", mod, "..." print "freezing", mod, "..."
str = marshal.dumps(m.__code__) str = marshal.dumps(m.__code__)
size = len(str) size = len(str)
if m.__path__: if m.__path__:
# Indicate package by negative size # Indicate package by negative size
size = -size size = -size
done.append((mod, mangled, size)) done.append((mod, mangled, size))
writecode(outfp, mangled, str) writecode(outfp, mangled, str)
if debug: if debug:
print "generating table of frozen modules" print "generating table of frozen modules"
outfp.write(header) outfp.write(header)
for mod, mangled, size in done: for mod, mangled, size in done:
outfp.write('\t{"%s", M_%s, %d},\n' % (mod, mangled, size)) outfp.write('\t{"%s", M_%s, %d},\n' % (mod, mangled, size))
outfp.write(trailer) outfp.write(trailer)
# Write a C initializer for a module containing the frozen python code. # Write a C initializer for a module containing the frozen python code.
# The array is called M_<mod>. # The array is called M_<mod>.
def writecode(outfp, mod, str): def writecode(outfp, mod, str):
outfp.write('static unsigned char M_%s[] = {' % mod) outfp.write('static unsigned char M_%s[] = {' % mod)
for i in range(0, len(str), 16): for i in range(0, len(str), 16):
outfp.write('\n\t') outfp.write('\n\t')
for c in str[i:i+16]: for c in str[i:i+16]:
outfp.write('%d,' % ord(c)) outfp.write('%d,' % ord(c))
outfp.write('\n};\n') outfp.write('\n};\n')
# Local Variables:
# indent-tabs-mode: nil
# End:

View File

@ -16,358 +16,362 @@ IMPORT_FROM = dis.opname.index('IMPORT_FROM')
class Module: class Module:
def __init__(self, name, file=None, path=None): def __init__(self, name, file=None, path=None):
self.__name__ = name self.__name__ = name
self.__file__ = file self.__file__ = file
self.__path__ = path self.__path__ = path
self.__code__ = None self.__code__ = None
def __repr__(self): def __repr__(self):
s = "Module(%s" % `self.__name__` s = "Module(%s" % `self.__name__`
if self.__file__ is not None: if self.__file__ is not None:
s = s + ", %s" % `self.__file__` s = s + ", %s" % `self.__file__`
if self.__path__ is not None: if self.__path__ is not None:
s = s + ", %s" % `self.__path__` s = s + ", %s" % `self.__path__`
s = s + ")" s = s + ")"
return s return s
class ModuleFinder: class ModuleFinder:
def __init__(self, path=None, debug=0): def __init__(self, path=None, debug=0):
if path is None: if path is None:
path = sys.path path = sys.path
self.path = path self.path = path
self.modules = {} self.modules = {}
self.badmodules = {} self.badmodules = {}
self.debug = debug self.debug = debug
self.indent = 0 self.indent = 0
def msg(self, level, str, *args): def msg(self, level, str, *args):
if level <= self.debug: if level <= self.debug:
for i in range(self.indent): for i in range(self.indent):
print " ", print " ",
print str, print str,
for arg in args: for arg in args:
print repr(arg), print repr(arg),
print print
def msgin(self, *args): def msgin(self, *args):
level = args[0] level = args[0]
if level <= self.debug: if level <= self.debug:
self.indent = self.indent + 1 self.indent = self.indent + 1
apply(self.msg, args) apply(self.msg, args)
def msgout(self, *args): def msgout(self, *args):
level = args[0] level = args[0]
if level <= self.debug: if level <= self.debug:
self.indent = self.indent - 1 self.indent = self.indent - 1
apply(self.msg, args) apply(self.msg, args)
def run_script(self, pathname): def run_script(self, pathname):
self.msg(2, "run_script", pathname) self.msg(2, "run_script", pathname)
fp = open(pathname) fp = open(pathname)
stuff = ("", "r", imp.PY_SOURCE) stuff = ("", "r", imp.PY_SOURCE)
self.load_module('__main__', fp, pathname, stuff) self.load_module('__main__', fp, pathname, stuff)
def load_file(self, pathname): def load_file(self, pathname):
dir, name = os.path.split(pathname) dir, name = os.path.split(pathname)
name, ext = os.path.splitext(name) name, ext = os.path.splitext(name)
fp = open(pathname) fp = open(pathname)
stuff = (ext, "r", imp.PY_SOURCE) stuff = (ext, "r", imp.PY_SOURCE)
self.load_module(name, fp, pathname, stuff) self.load_module(name, fp, pathname, stuff)
def import_hook(self, name, caller=None, fromlist=None): def import_hook(self, name, caller=None, fromlist=None):
self.msg(3, "import_hook", name, caller, fromlist) self.msg(3, "import_hook", name, caller, fromlist)
parent = self.determine_parent(caller) parent = self.determine_parent(caller)
q, tail = self.find_head_package(parent, name) q, tail = self.find_head_package(parent, name)
m = self.load_tail(q, tail) m = self.load_tail(q, tail)
if not fromlist: if not fromlist:
return q return q
if m.__path__: if m.__path__:
self.ensure_fromlist(m, fromlist) self.ensure_fromlist(m, fromlist)
def determine_parent(self, caller): def determine_parent(self, caller):
self.msgin(4, "determine_parent", caller) self.msgin(4, "determine_parent", caller)
if not caller: if not caller:
self.msgout(4, "determine_parent -> None") self.msgout(4, "determine_parent -> None")
return None return None
pname = caller.__name__ pname = caller.__name__
if caller.__path__: if caller.__path__:
parent = self.modules[pname] parent = self.modules[pname]
assert caller is parent assert caller is parent
self.msgout(4, "determine_parent ->", parent) self.msgout(4, "determine_parent ->", parent)
return parent return parent
if '.' in pname: if '.' in pname:
i = string.rfind(pname, '.') i = string.rfind(pname, '.')
pname = pname[:i] pname = pname[:i]
parent = self.modules[pname] parent = self.modules[pname]
assert parent.__name__ == pname assert parent.__name__ == pname
self.msgout(4, "determine_parent ->", parent) self.msgout(4, "determine_parent ->", parent)
return parent return parent
self.msgout(4, "determine_parent -> None") self.msgout(4, "determine_parent -> None")
return None return None
def find_head_package(self, parent, name): def find_head_package(self, parent, name):
self.msgin(4, "find_head_package", parent, name) self.msgin(4, "find_head_package", parent, name)
if '.' in name: if '.' in name:
i = string.find(name, '.') i = string.find(name, '.')
head = name[:i] head = name[:i]
tail = name[i+1:] tail = name[i+1:]
else: else:
head = name head = name
tail = "" tail = ""
if parent: if parent:
qname = "%s.%s" % (parent.__name__, head) qname = "%s.%s" % (parent.__name__, head)
else: else:
qname = head qname = head
q = self.import_module(head, qname, parent) q = self.import_module(head, qname, parent)
if q: if q:
self.msgout(4, "find_head_package ->", (q, tail)) self.msgout(4, "find_head_package ->", (q, tail))
return q, tail return q, tail
if parent: if parent:
qname = head qname = head
parent = None parent = None
q = self.import_module(head, qname, parent) q = self.import_module(head, qname, parent)
if q: if q:
self.msgout(4, "find_head_package ->", (q, tail)) self.msgout(4, "find_head_package ->", (q, tail))
return q, tail return q, tail
self.msgout(4, "raise ImportError: No module named", qname) self.msgout(4, "raise ImportError: No module named", qname)
raise ImportError, "No module named " + qname raise ImportError, "No module named " + qname
def load_tail(self, q, tail): def load_tail(self, q, tail):
self.msgin(4, "load_tail", q, tail) self.msgin(4, "load_tail", q, tail)
m = q m = q
while tail: while tail:
i = string.find(tail, '.') i = string.find(tail, '.')
if i < 0: i = len(tail) if i < 0: i = len(tail)
head, tail = tail[:i], tail[i+1:] head, tail = tail[:i], tail[i+1:]
mname = "%s.%s" % (m.__name__, head) mname = "%s.%s" % (m.__name__, head)
m = self.import_module(head, mname, m) m = self.import_module(head, mname, m)
if not m: if not m:
self.msgout(4, "raise ImportError: No module named", mname) self.msgout(4, "raise ImportError: No module named", mname)
raise ImportError, "No module named " + mname raise ImportError, "No module named " + mname
self.msgout(4, "load_tail ->", m) self.msgout(4, "load_tail ->", m)
return m return m
def ensure_fromlist(self, m, fromlist, recursive=0): def ensure_fromlist(self, m, fromlist, recursive=0):
self.msg(4, "ensure_fromlist", m, fromlist, recursive) self.msg(4, "ensure_fromlist", m, fromlist, recursive)
for sub in fromlist: for sub in fromlist:
if sub == "*": if sub == "*":
if not recursive: if not recursive:
all = self.find_all_submodules(m) all = self.find_all_submodules(m)
if all: if all:
self.ensure_fromlist(m, all, 1) self.ensure_fromlist(m, all, 1)
elif not hasattr(m, sub): elif not hasattr(m, sub):
subname = "%s.%s" % (m.__name__, sub) subname = "%s.%s" % (m.__name__, sub)
submod = self.import_module(sub, subname, m) submod = self.import_module(sub, subname, m)
if not submod: if not submod:
raise ImportError, "No module named " + subname raise ImportError, "No module named " + subname
def find_all_submodules(self, m): def find_all_submodules(self, m):
if not m.__path__: if not m.__path__:
return return
modules = {} modules = {}
suffixes = [".py", ".pyc", ".pyo"] suffixes = [".py", ".pyc", ".pyo"]
for dir in m.__path__: for dir in m.__path__:
try: try:
names = os.listdir(dir) names = os.listdir(dir)
except os.error: except os.error:
self.msg(2, "can't list directory", dir) self.msg(2, "can't list directory", dir)
continue continue
for name in names: for name in names:
mod = None mod = None
for suff in suffixes: for suff in suffixes:
n = len(suff) n = len(suff)
if name[-n:] == suff: if name[-n:] == suff:
mod = name[:-n] mod = name[:-n]
break break
if mod and mod != "__init__": if mod and mod != "__init__":
modules[mod] = mod modules[mod] = mod
return modules.keys() return modules.keys()
def import_module(self, partname, fqname, parent): def import_module(self, partname, fqname, parent):
self.msgin(3, "import_module", partname, fqname, parent) self.msgin(3, "import_module", partname, fqname, parent)
try: try:
m = self.modules[fqname] m = self.modules[fqname]
except KeyError: except KeyError:
pass pass
else: else:
self.msgout(3, "import_module ->", m) self.msgout(3, "import_module ->", m)
return m return m
if self.badmodules.has_key(fqname): if self.badmodules.has_key(fqname):
self.msgout(3, "import_module -> None") self.msgout(3, "import_module -> None")
return None return None
try: try:
fp, pathname, stuff = self.find_module(partname, fp, pathname, stuff = self.find_module(partname,
parent and parent.__path__) parent and parent.__path__)
except ImportError: except ImportError:
self.msgout(3, "import_module ->", None) self.msgout(3, "import_module ->", None)
return None return None
try: try:
m = self.load_module(fqname, fp, pathname, stuff) m = self.load_module(fqname, fp, pathname, stuff)
finally: finally:
if fp: fp.close() if fp: fp.close()
if parent: if parent:
setattr(parent, partname, m) setattr(parent, partname, m)
self.msgout(3, "import_module ->", m) self.msgout(3, "import_module ->", m)
return m return m
def load_module(self, fqname, fp, pathname, (suffix, mode, type)): def load_module(self, fqname, fp, pathname, (suffix, mode, type)):
self.msgin(2, "load_module", fqname, fp and "fp", pathname) self.msgin(2, "load_module", fqname, fp and "fp", pathname)
if type == imp.PKG_DIRECTORY: if type == imp.PKG_DIRECTORY:
m = self.load_package(fqname, pathname) m = self.load_package(fqname, pathname)
self.msgout(2, "load_module ->", m) self.msgout(2, "load_module ->", m)
return m return m
if type == imp.PY_SOURCE: if type == imp.PY_SOURCE:
co = compile(fp.read(), pathname, 'exec') co = compile(fp.read(), pathname, 'exec')
elif type == imp.PY_COMPILED: elif type == imp.PY_COMPILED:
if fp.read(4) != imp.get_magic(): if fp.read(4) != imp.get_magic():
self.msgout(2, "raise ImportError: Bad magic number", pathname) self.msgout(2, "raise ImportError: Bad magic number", pathname)
raise ImportError, "Bad magic number in %s", pathname raise ImportError, "Bad magic number in %s", pathname
fp.read(4) fp.read(4)
co = marshal.load(fp) co = marshal.load(fp)
else: else:
co = None co = None
m = self.add_module(fqname) m = self.add_module(fqname)
if co: if co:
m.__file__ = pathname m.__file__ = pathname
m.__code__ = co m.__code__ = co
code = co.co_code code = co.co_code
n = len(code) n = len(code)
i = 0 i = 0
lastname = None lastname = None
while i < n: while i < n:
c = code[i] c = code[i]
i = i+1 i = i+1
op = ord(c) op = ord(c)
if op >= dis.HAVE_ARGUMENT: if op >= dis.HAVE_ARGUMENT:
oparg = ord(code[i]) + ord(code[i+1])*256 oparg = ord(code[i]) + ord(code[i+1])*256
i = i+2 i = i+2
if op == IMPORT_NAME: if op == IMPORT_NAME:
name = lastname = co.co_names[oparg] name = lastname = co.co_names[oparg]
if not self.badmodules.has_key(lastname): if not self.badmodules.has_key(lastname):
try: try:
self.import_hook(name, m) self.import_hook(name, m)
except ImportError, msg: except ImportError, msg:
self.msg(2, "ImportError:", str(msg)) self.msg(2, "ImportError:", str(msg))
self.badmodules[name] = None self.badmodules[name] = None
elif op == IMPORT_FROM: elif op == IMPORT_FROM:
name = co.co_names[oparg] name = co.co_names[oparg]
assert lastname is not None assert lastname is not None
if not self.badmodules.has_key(lastname): if not self.badmodules.has_key(lastname):
try: try:
self.import_hook(lastname, m, [name]) self.import_hook(lastname, m, [name])
except ImportError, msg: except ImportError, msg:
self.msg(2, "ImportError:", str(msg)) self.msg(2, "ImportError:", str(msg))
fullname = lastname + "." + name fullname = lastname + "." + name
self.badmodules[fullname] = None self.badmodules[fullname] = None
else: else:
lastname = None lastname = None
self.msgout(2, "load_module ->", m) self.msgout(2, "load_module ->", m)
return m return m
def load_package(self, fqname, pathname): def load_package(self, fqname, pathname):
self.msgin(2, "load_package", fqname, pathname) self.msgin(2, "load_package", fqname, pathname)
m = self.add_module(fqname) m = self.add_module(fqname)
m.__file__ = pathname m.__file__ = pathname
m.__path__ = [pathname] m.__path__ = [pathname]
fp, buf, stuff = self.find_module("__init__", m.__path__) fp, buf, stuff = self.find_module("__init__", m.__path__)
self.load_module(fqname, fp, buf, stuff) self.load_module(fqname, fp, buf, stuff)
self.msgout(2, "load_package ->", m) self.msgout(2, "load_package ->", m)
return m return m
def add_module(self, fqname): def add_module(self, fqname):
if self.modules.has_key(fqname): if self.modules.has_key(fqname):
return self.modules[fqname] return self.modules[fqname]
self.modules[fqname] = m = Module(fqname) self.modules[fqname] = m = Module(fqname)
return m return m
def find_module(self, name, path): def find_module(self, name, path):
if path is None: if path is None:
if name in sys.builtin_module_names: if name in sys.builtin_module_names:
return (None, None, ("", "", imp.C_BUILTIN)) return (None, None, ("", "", imp.C_BUILTIN))
path = self.path path = self.path
return imp.find_module(name, path) return imp.find_module(name, path)
def report(self): def report(self):
print print
print " %-25s %s" % ("Name", "File") print " %-25s %s" % ("Name", "File")
print " %-25s %s" % ("----", "----") print " %-25s %s" % ("----", "----")
# Print modules found # Print modules found
keys = self.modules.keys() keys = self.modules.keys()
keys.sort() keys.sort()
for key in keys: for key in keys:
m = self.modules[key] m = self.modules[key]
if m.__path__: if m.__path__:
print "P", print "P",
else: else:
print "m", print "m",
print "%-25s" % key, m.__file__ or "" print "%-25s" % key, m.__file__ or ""
# Print missing modules # Print missing modules
keys = self.badmodules.keys() keys = self.badmodules.keys()
keys.sort() keys.sort()
for key in keys: for key in keys:
print "?", key print "?", key
def test(): def test():
# Parse command line # Parse command line
import getopt import getopt
try: try:
opts, args = getopt.getopt(sys.argv[1:], "dmp:q") opts, args = getopt.getopt(sys.argv[1:], "dmp:q")
except getopt.error, msg: except getopt.error, msg:
print msg print msg
return return
# Process options # Process options
debug = 1 debug = 1
domods = 0 domods = 0
addpath = [] addpath = []
for o, a in opts: for o, a in opts:
if o == '-d': if o == '-d':
debug = debug + 1 debug = debug + 1
if o == '-m': if o == '-m':
domods = 1 domods = 1
if o == '-p': if o == '-p':
addpath = addpath + string.split(a, os.pathsep) addpath = addpath + string.split(a, os.pathsep)
if o == '-q': if o == '-q':
debug = 0 debug = 0
# Provide default arguments # Provide default arguments
if not args: if not args:
script = "hello.py" script = "hello.py"
else: else:
script = args[0] script = args[0]
# Set the path based on sys.path and the script directory # Set the path based on sys.path and the script directory
path = sys.path[:] path = sys.path[:]
path[0] = os.path.dirname(script) path[0] = os.path.dirname(script)
path = addpath + path path = addpath + path
if debug > 1: if debug > 1:
print "path:" print "path:"
for item in path: for item in path:
print " ", `item` print " ", `item`
# Create the module finder and turn its crank # Create the module finder and turn its crank
mf = ModuleFinder(path, debug) mf = ModuleFinder(path, debug)
for arg in args[1:]: for arg in args[1:]:
if arg == '-m': if arg == '-m':
domods = 1 domods = 1
continue continue
if domods: if domods:
if arg[-2:] == '.*': if arg[-2:] == '.*':
mf.import_hook(arg[:-2], None, ["*"]) mf.import_hook(arg[:-2], None, ["*"])
else: else:
mf.import_hook(arg) mf.import_hook(arg)
else: else:
mf.load_file(arg) mf.load_file(arg)
mf.run_script(script) mf.run_script(script)
mf.report() mf.report()
if __name__ == '__main__': if __name__ == '__main__':
try: try:
test() test()
except KeyboardInterrupt: except KeyboardInterrupt:
print "\n[interrupt]" print "\n[interrupt]"
# Local Variables:
# indent-tabs-mode: nil
# End: