Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
0ec58557
Commit
0ec58557
authored
May 08, 2008
by
Brett Cannon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove the imputil module.
parent
6d4df9b3
Changes
6
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
0 additions
and
951 deletions
+0
-951
Doc/library/imputil.rst
Doc/library/imputil.rst
+0
-234
Doc/library/modules.rst
Doc/library/modules.rst
+0
-1
Lib/imputil.py
Lib/imputil.py
+0
-712
Lib/linecache.py
Lib/linecache.py
+0
-2
Lib/test/test___all__.py
Lib/test/test___all__.py
+0
-1
Lib/test/test_sundry.py
Lib/test/test_sundry.py
+0
-1
No files found.
Doc/library/imputil.rst
deleted
100644 → 0
View file @
6d4df9b3
:mod:`imputil` --- Import utilities
===================================
.. module:: imputil
:synopsis: Manage and augment the import process.
.. index:: statement: import
This module provides a very handy and useful mechanism for custom
:keyword:`import` hooks. Compared to the older :mod:`ihooks` module,
:mod:`imputil` takes a dramatically simpler and more straight-forward
approach to custom :keyword:`import` functions.
.. class:: ImportManager([fs_imp])
Manage the import process.
.. method:: ImportManager.install([namespace])
Install this ImportManager into the specified namespace.
.. method:: ImportManager.uninstall()
Restore the previous import mechanism.
.. method:: ImportManager.add_suffix(suffix, importFunc)
Undocumented.
.. class:: Importer()
Base class for replacing standard import functions.
.. method:: Importer.import_top(name)
Import a top-level module.
.. method:: Importer.get_code(parent, modname, fqname)
Find and retrieve the code for the given module.
*parent* specifies a parent module to define a context for importing.
It may be ``None``, indicating no particular context for the search.
*modname* specifies a single module (not dotted) within the parent.
*fqname* specifies the fully-qualified module name. This is a
(potentially) dotted name from the "root" of the module namespace
down to the modname.
If there is no parent, then modname==fqname.
This method should return ``None``, or a 3-tuple.
* If the module was not found, then ``None`` should be returned.
* The first item of the 2- or 3-tuple should be the integer 0 or 1,
specifying whether the module that was found is a package or not.
* The second item is the code object for the module (it will be
executed within the new module's namespace). This item can also
be a fully-loaded module object (e.g. loaded from a shared lib).
* The third item is a dictionary of name/value pairs that will be
inserted into new module before the code object is executed. This
is provided in case the module's code expects certain values (such
as where the module was found). When the second item is a module
object, then these names/values will be inserted *after* the module
has been loaded/initialized.
.. class:: BuiltinImporter()
Emulate the import mechanism for builtin and frozen modules. This is a
sub-class of the :class:`Importer` class.
.. method:: BuiltinImporter.get_code(parent, modname, fqname)
Undocumented.
.. function:: py_suffix_importer(filename, finfo, fqname)
Undocumented.
.. class:: DynLoadSuffixImporter([desc])
Undocumented.
.. method:: DynLoadSuffixImporter.import_file(filename, finfo, fqname)
Undocumented.
.. _examples-imputil:
Examples
--------
This is a 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 not "__name__" 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
# Replacement for reload()
def reload_hook(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__
original_reload = builtins.reload
# Now install our hooks
builtins.__import__ = import_hook
builtins.reload = reload_hook
.. index::
module: knee
Also see the :mod:`importers` module (which can be found
in :file:`Demo/imputil/` in the Python source distribution) for additional
examples.
Doc/library/modules.rst
View file @
0ec58557
...
...
@@ -14,7 +14,6 @@ The full list of modules described in this chapter is:
.. toctree::
imp.rst
imputil.rst
zipimport.rst
pkgutil.rst
modulefinder.rst
...
...
Lib/imputil.py
deleted
100644 → 0
View file @
6d4df9b3
This diff is collapsed.
Click to expand it.
Lib/linecache.py
View file @
0ec58557
...
...
@@ -108,8 +108,6 @@ def updatecache(filename, module_globals=None):
# Try looking through the module search path.
for
dirname
in
sys
.
path
:
# When using imputil, sys.path may contain things other than
# strings; ignore them when it happens.
try
:
fullname
=
os
.
path
.
join
(
dirname
,
basename
)
except
(
TypeError
,
AttributeError
):
...
...
Lib/test/test___all__.py
View file @
0ec58557
...
...
@@ -79,7 +79,6 @@ class AllTest(unittest.TestCase):
self
.
check_all
(
"ihooks"
)
self
.
check_all
(
"imaplib"
)
self
.
check_all
(
"imghdr"
)
self
.
check_all
(
"imputil"
)
self
.
check_all
(
"keyword"
)
self
.
check_all
(
"linecache"
)
self
.
check_all
(
"locale"
)
...
...
Lib/test/test_sundry.py
View file @
0ec58557
...
...
@@ -51,7 +51,6 @@ class TestUntestedModules(unittest.TestCase):
import
htmlentitydefs
import
ihooks
import
imghdr
import
imputil
import
keyword
import
linecache
import
macurl2path
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment