[Python-Dev] bug or feature in imp.load_module()?
Guido van Rossum
guido@python.org
Wed, 17 Oct 2001 15:05:35 -0400
> > If you pass a file to a function, I don't think that there is any
> > guarantee to the file position when it returns, so I don't think this
> > should be documented. I can't think of why you'd expect it to be at
> > any specific position.
>
> I'm not sure either. :-) It was odd for me to pass both file & filename.
Well, you've got to realize that this is a really low-level interface
designed to make certain optimizations possible. The filename is
needed (a) for diagnostics, (b) for opening the .pyc file; the open
file is a side effect from the search. It uses fopen() to check for
file existence, to prevent silly things like existing files that
cannot be opened for whatever reason). You don't want to have to open
the file twice!
> The doc says that:
>
> The file argument is an open file, and filename is
> the corresponding file name; these can be None and '',
> respectively, when the module is not being loaded from a file.
>
> I'm not sure how the module could not be loaded from a file (unless
> the implication is that the file argument is None and a new file will
> be opened from the filename).
Read the C source and weep. :-) Some systems have other places from
which they can load modules, e.g. on the Mac a module can be in a
"resource". Also there are frozen modules.
> If the file can be None anytime, it makes more sense to me.
> But the last phrase in the doc seems to indicate the file argument
> should not be None.
>
> Perhaps, it's just me, it wouldn't be the first time. :-)
Why are you trying to use this? Can't you use __import__?
--Guido van Rossum (home page: http://www.python.org/~guido/)