summaryrefslogtreecommitdiffstats
path: root/Demo
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2010-10-13 22:02:44 (GMT)
committerBenjamin Peterson <benjamin@python.org>2010-10-13 22:02:44 (GMT)
commitae87a27a2994718e8e1b77f06b173882ec5c7704 (patch)
tree90b8df6ffac6332e3e31f11d55f6acfc649346ec /Demo
parenta0bf56d4f1e9ea9f47a7e18a29d7678df284e345 (diff)
downloadcpython-ae87a27a2994718e8e1b77f06b173882ec5c7704.zip
cpython-ae87a27a2994718e8e1b77f06b173882ec5c7704.tar.gz
cpython-ae87a27a2994718e8e1b77f06b173882ec5c7704.tar.bz2
Merged revisions 85429 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k ........ r85429 | benjamin.peterson | 2010-10-13 16:59:36 -0500 (Wed, 13 Oct 2010) | 1 line remove obselete import implementation #7287 ........
Diffstat (limited to 'Demo')
-rw-r--r--Demo/imputil/knee.py123
1 files changed, 0 insertions, 123 deletions
diff --git a/Demo/imputil/knee.py b/Demo/imputil/knee.py
deleted file mode 100644
index 435b51c..0000000
--- a/Demo/imputil/knee.py
+++ /dev/null
@@ -1,123 +0,0 @@
-"""An Python re-implementation of hierarchical module import.
-
-This code is intended to be read, not executed. However, it does work
--- all you need to do to enable it is "import knee".
-
-(The name is a pun on the klunkier predecessor of this module, "ni".)
-
-"""
-
-import sys, imp, builtins
-
-
-# Replacement for __import__()
-def import_hook(name, globals=None, locals=None, fromlist=None):
- parent = determine_parent(globals)
- q, tail = find_head_package(parent, name)
- m = load_tail(q, tail)
- if not fromlist:
- return q
- if hasattr(m, "__path__"):
- ensure_fromlist(m, fromlist)
- return m
-
-def determine_parent(globals):
- if not globals or "__name__" not in globals:
- return None
- pname = globals['__name__']
- if "__path__" in globals:
- parent = sys.modules[pname]
- assert globals is parent.__dict__
- return parent
- if '.' in pname:
- i = pname.rfind('.')
- pname = pname[:i]
- parent = sys.modules[pname]
- assert parent.__name__ == pname
- return parent
- return None
-
-def find_head_package(parent, name):
- if '.' in name:
- i = name.find('.')
- head = name[:i]
- tail = name[i+1:]
- else:
- head = name
- tail = ""
- if parent:
- qname = "%s.%s" % (parent.__name__, head)
- else:
- qname = head
- q = import_module(head, qname, parent)
- if q: return q, tail
- if parent:
- qname = head
- parent = None
- q = import_module(head, qname, parent)
- if q: return q, tail
- raise ImportError("No module named " + qname)
-
-def load_tail(q, tail):
- m = q
- while tail:
- i = tail.find('.')
- if i < 0: i = len(tail)
- head, tail = tail[:i], tail[i+1:]
- mname = "%s.%s" % (m.__name__, head)
- m = import_module(head, mname, m)
- if not m:
- raise ImportError("No module named " + mname)
- return m
-
-def ensure_fromlist(m, fromlist, recursive=0):
- for sub in fromlist:
- if sub == "*":
- if not recursive:
- try:
- all = m.__all__
- except AttributeError:
- pass
- else:
- ensure_fromlist(m, all, 1)
- continue
- if sub != "*" and not hasattr(m, sub):
- subname = "%s.%s" % (m.__name__, sub)
- submod = import_module(sub, subname, m)
- if not submod:
- raise ImportError("No module named " + subname)
-
-def import_module(partname, fqname, parent):
- try:
- return sys.modules[fqname]
- except KeyError:
- pass
- try:
- fp, pathname, stuff = imp.find_module(partname,
- parent and parent.__path__)
- except ImportError:
- return None
- try:
- m = imp.load_module(fqname, fp, pathname, stuff)
- finally:
- if fp: fp.close()
- if parent:
- setattr(parent, partname, m)
- return m
-
-
-def reload(module):
- name = module.__name__
- if '.' not in name:
- return import_module(name, name, None)
- i = name.rfind('.')
- pname = name[:i]
- parent = sys.modules[pname]
- return import_module(name[i+1:], name, parent)
-
-
-# Save the original hooks
-original_import = builtins.__import__
-
-# Now install our hooks
-builtins.__import__ = import_hook