.pyo's without .pyc's?

M.-A. Lemburg mal at lemburg.com
Thu Jul 8 08:31:59 EDT 1999


[CCed to Guido, because I think what you've found is true bug in 1.5.2
 and earlier versions]

Randall Hopper wrote:
> 
> M.-A. Lemburg:
>  |Looks like you have setup PYTHONPATH to point to some old
>  |extensions.py file (the compiled versions seemed to be ok).
> 
> It doesn't appear this is the case (please see below).
> 
> Unsuccessful exceptions module search -- *.py and *.pyc pruned:
> 
> > find . -name 'exceptions*'
> ./lib/python1.5/exceptions.pyo
> 
> > bin/python -O -vv | & grep except
> # trying /home/rhh/software/python-1.5.2/lib/python1.5/exceptions.so
> # trying /home/rhh/software/python-1.5.2/lib/python1.5/exceptionsmodule.so
> # trying /home/rhh/software/python-1.5.2/lib/python1.5/exceptions.py
> # trying /home/rhh/software/python-1.5.2/lib/python1.5/exceptions.pyc
> # trying /home/rhh/software/python-1.5.2/lib/python1.5/plat-irix646-n32/exceptions.so
> # trying /home/rhh/software/python-1.5.2/lib/python1.5/plat-irix646-n32/exceptionsmodule.so
> # trying /home/rhh/software/python-1.5.2/lib/python1.5/plat-irix646-n32/exceptions.py
> # trying /home/rhh/software/python-1.5.2/lib/python1.5/plat-irix646-n32/exceptions.pyc
> # trying /home/rhh/software/python-1.5.2/lib/python1.5/lib-tk/exceptions.so
> # trying /home/rhh/software/python-1.5.2/lib/python1.5/lib-tk/exceptionsmodule.so
> # trying /home/rhh/software/python-1.5.2/lib/python1.5/lib-tk/exceptions.py
> # trying /home/rhh/software/python-1.5.2/lib/python1.5/lib-tk/exceptions.pyc
> # trying /home/rhh/software/python-1.5.2/lib/python1.5/lib-dynload/exceptions.so
> # trying /home/rhh/software/python-1.5.2/lib/python1.5/lib-dynload/exceptionsmodule.so
> # trying /home/rhh/software/python-1.5.2/lib/python1.5/lib-dynload/exceptions.py
> # trying /home/rhh/software/python-1.5.2/lib/python1.5/lib-dynload/exceptions.pyc
> 'import exceptions' failed; traceback:
> ImportError: No module named exceptions
> Warning!  Falling back to string-based exceptions

I've checked this and indeed you're right. exceptions.py is imported
prior to all other modules at a time where the switch from .pyc to
.pyo hasn't been done yet (this is done in _PyImport_Init(), yet
exceptions.py is imported by _PyBuiltin_Init_2()):

>From pythonrun.c:

	bimod = _PyBuiltin_Init_1();
	if (bimod == NULL)
		Py_FatalError("Py_Initialize: can't initialize __builtin__");
	interp->builtins = PyModule_GetDict(bimod);
	Py_INCREF(interp->builtins);

	sysmod = _PySys_Init();
	if (sysmod == NULL)
		Py_FatalError("Py_Initialize: can't initialize sys");
	interp->sysdict = PyModule_GetDict(sysmod);
	Py_INCREF(interp->sysdict);
	_PyImport_FixupExtension("sys", "sys");
	PySys_SetPath(Py_GetPath());
	PyDict_SetItemString(interp->sysdict, "modules",
			     interp->modules);

	/* phase 2 of builtins */
	_PyBuiltin_Init_2(interp->builtins);
	_PyImport_FixupExtension("__builtin__", "__builtin__");

	_PyImport_Init();

To work around this, simply add the excpetions.py file (and only
this one) to the python1.5/lib/ directory.

Still, I think you're right in that the .pyc -> .pyo switch should
be done prior to importing *any* Python module, meaning that
_PyImport_Init() should be called prior to importing anything.

Cheers,
-- 
Marc-Andre Lemburg
______________________________________________________________________
Y2000:                                                   176 days left
Business:                                      http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/





More information about the Python-list mailing list