Merge lp:~maxb/launchpad/py2.6-importfascist into lp:launchpad

Proposed by Max Bowsher
Status: Merged
Approved by: Guilherme Salgado
Approved revision: not available
Merged at revision: not available
Proposed branch: lp:~maxb/launchpad/py2.6-importfascist
Merge into: lp:launchpad
Diff against target: 26 lines (+3/-3)
1 file modified
lib/lp/scripts/utilities/importfascist.py (+3/-3)
To merge this branch: bzr merge lp:~maxb/launchpad/py2.6-importfascist
Reviewer Review Type Date Requested Status
Guilherme Salgado (community) code Approve
Review via email: mp+18646@code.launchpad.net

Commit message

Accept and propagate a 'level' parameter in importfascist. (New feature in Python 2.5, helps compatibility with 2.6)

To post a comment you must log in.
Revision history for this message
Max Bowsher (maxb) wrote :

This branch upgrades the importfascist's __import__ hook to include the fifth argument 'level' which was introduced in Python 2.5.

We have survived without this change so far, as few things use it yet.

I am submitting this branch now as preparatory effort for the future move to Python 2.6, as internal parts of Python such as PyImport_Import(modulename) start to use the level parameter there.

The semantics of level are as follows:

  -1 : traditional behaviour
   0 : absolute import, do not even try to consider as relative
  >=1 : explicit relative import, stated number of levels up

Because the existing importfascist makes no attempt to support relative imports, and binning the current implementation has been discussed as a likely option, I have taken the simple route of just accepting and propagating the parameter without bothering to use it in any way.

This does not weaken the import fascist any more than it already is liable to fail to properly process relative imports.

This change was previously part of the python-migration2.6 sprint branch, now cherrypicked for landing on devel whilst still in the 2.5 era.

Revision history for this message
Guilherme Salgado (salgado) wrote :

looks good to me

review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'lib/lp/scripts/utilities/importfascist.py'
--- lib/lp/scripts/utilities/importfascist.py 2010-01-19 04:15:24 +0000
+++ lib/lp/scripts/utilities/importfascist.py 2010-02-04 23:07:16 +0000
@@ -174,11 +174,11 @@
174174
175175
176# pylint: disable-msg=W0102,W0602176# pylint: disable-msg=W0102,W0602
177def import_fascist(module_name, globals={}, locals={}, from_list=[]):177def import_fascist(module_name, globals={}, locals={}, from_list=[], level=-1):
178 global naughty_imports178 global naughty_imports
179179
180 try:180 try:
181 module = original_import(module_name, globals, locals, from_list)181 module = original_import(module_name, globals, locals, from_list, level)
182 except ImportError:182 except ImportError:
183 # XXX sinzui 2008-04-17 bug=277274:183 # XXX sinzui 2008-04-17 bug=277274:
184 # import_fascist screws zope configuration module which introspects184 # import_fascist screws zope configuration module which introspects
@@ -190,7 +190,7 @@
190 # module.190 # module.
191 if module_name.startswith('zope.app.layers.'):191 if module_name.startswith('zope.app.layers.'):
192 module_name = module_name[16:]192 module_name = module_name[16:]
193 module = original_import(module_name, globals, locals, from_list)193 module = original_import(module_name, globals, locals, from_list, level)
194 else:194 else:
195 raise195 raise
196 # Python's re module imports some odd stuff every time certain regexes196 # Python's re module imports some odd stuff every time certain regexes