Bug in `imp' module?

Andrew Dalke dalke at bioreason.com
Tue Jun 15 05:09:44 CEST 1999


Summary: imp.find_file returns the .py file before the .pyc file
when I think it should be the other way around.  Solution should be
to swap two lines in Python/importdl.c 


  I've been learning how to use the imp module.  There is a function
called find_module which looks up the initialization information
needed for a given module name.  So I tried it out:

val> cat > norm.py
bar = "Cheers"
val> python
Python 1.5.1 (#21, Nov 23 1998, 15:04:47) [C] on irix6
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import norm
>>> 
val> ls -l norm*
-rw-r--r--    1 dalke    staff         15 Jun 14 20:10 norm.py
-rw-r--r--    1 dalke    staff         98 Jun 14 20:10 norm.pyc
>>> import imp
>>> imp.find_module("norm")
(<open file 'norm.py', mode 'r' at 100a4a90>, 'norm.py', ('.py', 'r', 1))


--- PROBLEM ---

I expected to get something like:
(<open file 'norm.pyc', mode 'r' at 100a4a90>, 'norm.py', ('.py', 'r', 2))

 My expectation was that it would act like a normal import and try
to get the .pyc (or .pyo) file first, before getting the source file.
Hence, a ".pyc" for the filename extension and a 2 (==imp.PY_COMPILED)
instead of a 1 (==imp.PY_SOURCE).

  If I remove the .py file, it does get the .pyc file with the right
description information:

val> python
Python 1.5.1 (#21, Nov 23 1998, 15:04:47) [C] on irix6
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import imp
>>> imp.find_module("norm")
(<open file 'norm.pyc', mode 'rb' at 100a4a90>, 'norm.pyc', ('.pyc',
'rb', 2))

  I believe the problem boils down to the list returned from
imp.get_suffixes.  The standard list is:

[('.so', 'rb', 3), ('module.so', 'rb', 3), ('.py', 'r', 1),
 ('.pyc', 'rb', 2)]

so .py files are found before .pyc files.  (BTW, the .pyc will be
replaced with a .pyo if -O is used.)

I tested this possibility by changing Python/importdl.c to swap the
 .py and the .pyc lines when building _PyImport_Filetab:

>         {".pyc", "rb", PY_COMPILED},    /* added */
>         {".py", "r", PY_SOURCE},
> /*      {".pyc", "rb", PY_COMPILED},*/  /* original */


After recompiling, I get that the .pyc file is indeed found first:

val> ls -l norm*
-rw-r--r--    1 dalke    staff         15 Jun 14 20:10 norm.py
-rw-r--r--    1 dalke    staff         98 Jun 14 20:10 norm.pyc
val> ~/Python-1.5.1/python 
Python 1.5.1 (#32, Jun 14 1999, 20:37:01) [C] on irix6
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import imp
>>> imp.find_module("norm")
(<open file 'norm.pyc', mode 'rb' at 100a8fc8>, 'norm.pyc', ('.pyc',
 'rb', 2))
>>> 

and if the .pyc isn't present it will back down to the .py file.


  Is this really a bug?  Oh, and the most recent version I have,
1.5.2a2, has the same problem, and no one in Deja News has mentioned
it.

						Andrew Dalke
						dalke at bioreason.com




More information about the Python-list mailing list