[Python-Dev] Document None values in sys.modules?

Guido van Rossum guido at python.org
Fri Jul 24 19:58:39 CEST 2009


On Fri, Jul 24, 2009 at 10:50 AM, Brett Cannon<brett at python.org> wrote:
>
>
> On Fri, Jul 24, 2009 at 10:39, Guido van Rossum <guido at python.org> wrote:
>>
>> Still works, at least in some old 3.1 I had lying around:
>>
>> $ python3.1
>> Python 3.1a0 (py3k:70152, Mar  3 2009, 16:55:42)
>> [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
>> Type "help", "copyright", "credits" or "license" for more information.
>> >>> import sys
>> >>> sys.modules['string'] = None
>> >>> import string
>> Traceback (most recent call last):
>>  File "<stdin>", line 1, in <module>
>> ImportError: No module named string
>> >>>
>> $ python3.1
>> Python 3.1a0 (py3k:70152, Mar  3 2009, 16:55:42)
>> [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
>> Type "help", "copyright", "credits" or "license" for more information.
>> >>> import string
>> >>>
>> $
>>
>> The experiment should be easily repeatable. :-)
>
> =) Yes, the None raising ImportError semantics can be added easily and
> codified as official import semantics in 3.1.

I thought it was too simple. :-) Maybe I should go back to bed and
nurse my cough some more instead of playing hookie from being sick and
indulging in email.

> I was talking about the "None
> triggers another import as with relative imports" semantics and if there was
> some rather convoluted way to trigger that.

I can't think of one and doubt you will find one -- it was introduced
(in the earliest of early package import implementations) to optimize
the problem we had with code living inside a package trying to stat()
a toplevel module locally each time an import of it was processed (a
problem if a package contains lots of modules each of which import os,
sys, etc.). Since we no longer to relative import, I don't see how
there could be another use -- if it's not there it's not there.

> I am guessing not as that would
> require a value of -1 for level which is no longer valid.

Ah. Sure.

> I will fix importlib in both 3.2 and 3.1.1. And speaking of fixing, should I
> put the failing test in now and decorate it with unittest.expectedFailure
> until I get around to fixing it?

Oh, I don't know. All this new-fangled technology... Can't you just
leave it in your workspace, unsubmitted, until you get to fixing it?

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list