[Python-checkins] r53800 - in python/trunk: Lib/encodings/__init__.py Misc/NEWS

M.-A. Lemburg mal at egenix.com
Fri Feb 16 12:20:24 CET 2007


On 2007-02-16 00:51, Brett Cannon wrote:
> On 2/15/07, M.-A. Lemburg <mal at egenix.com> wrote:
>> On 2007-02-15 23:54, brett.cannon wrote:
>>> Author: brett.cannon
>>> Date: Thu Feb 15 23:54:39 2007
>>> New Revision: 53800
>>>
>>> Modified:
>>>    python/trunk/Lib/encodings/__init__.py
>>>    python/trunk/Misc/NEWS
>>> Log:
>>> Update the encoding package's search function to use absolute imports when
>>> calling __import__.  This helps make the expected search locations for encoding
>>> modules be more explicit.
>> Your change does not make the import absolute - to the contrary: we
>> now have a relative import.
>>
> 
> Right, but I just associate it with the absolute import PEP and the
> term.  Probably could have called it a "new-style import" or
> something.
> 
>> Please change that back to the original scheme which is indeed an
>> absolute import.
> 
> Why?  What's wrong with a relative import?

Whenever possible we should use absolute imports. Relative imports
are really only necessary when you want to make packages relocatable
which is not the case for the encodings package.

>> If you want to make sure that the search function
>> is not importing from encodings.encodings, then you can add
>> a 0 parameter as last parameter to __import__().
> 
> Right.  But that will essentially do the same thing I just committed
> since what you are proposing will grab 'encodings' from sys.modules,
> pull out its __path__ entry, and then do the import from there.  What
> I did above skips that look up for 'encodings' and instead uses the
> module that the function was defined in.

If you look at the import source code, you'll find that both methods
turn out to do the same thing. They both end up with a lookup for
'encodings'.

> -Brett
> 
> 
>> The other changes
>> are not necessary.
>>
>> Thanks.
>>
>>> One could use an explicit value for __path__ when making the call to __import__
>>> to force the exact location searched for encodings.  This would give the most
>>> strict search path possible if one is worried about malicious code being
>>> imported.  The unfortunate side-effect of that is that if __path__ was modified
>>> on 'encodings' on purpose in a safe way it would not be picked up in future
>>> __import__ calls.
>>>
>>>
>>> Modified: python/trunk/Lib/encodings/__init__.py
>>> ==============================================================================
>>> --- python/trunk/Lib/encodings/__init__.py    (original)
>>> +++ python/trunk/Lib/encodings/__init__.py    Thu Feb 15 23:54:39 2007
>>> @@ -93,8 +93,10 @@
>>>          if not modname or '.' in modname:
>>>              continue
>>>          try:
>>> -            mod = __import__('encodings.' + modname,
>>> -                             globals(), locals(), _import_tail)
>>> +            # Import equivalent to `` from .modname import *``.
>>> +            # '*' is used so that __import__ returns the desired module and not
>>> +            # 'encodings' itself.
>>> +            mod = __import__(modname, globals(), locals(), ['*'], 1)
>>>          except ImportError:
>>>              pass
>>>          else:
>>>
>>> Modified: python/trunk/Misc/NEWS
>>> ==============================================================================
>>> --- python/trunk/Misc/NEWS    (original)
>>> +++ python/trunk/Misc/NEWS    Thu Feb 15 23:54:39 2007
>>> @@ -128,6 +128,9 @@
>>>  Library
>>>  -------
>>>
>>> +- Have the encoding package's search function dynamically import using absolute
>>> +  import semantics.
>>> +
>>>  - Patch #1647484: Renamed GzipFile's filename attribute to name.
>>>
>>>  - Patch #1517891: Mode 'a' for ZipFile now creates the file if it
>>> _______________________________________________
>>> Python-checkins mailing list
>>> Python-checkins at python.org
>>> http://mail.python.org/mailman/listinfo/python-checkins
>> --
>> Marc-Andre Lemburg
>> eGenix.com
>>
>> Professional Python Services directly from the Source  (#1, Feb 16 2007)
>>>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
>> ________________________________________________________________________
>>
>> :::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::
>>
> _______________________________________________
> Python-checkins mailing list
> Python-checkins at python.org
> http://mail.python.org/mailman/listinfo/python-checkins

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Feb 16 2007)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::


More information about the Python-checkins mailing list