[Python-bugs-list] [ python-Bugs-417913 ] odd behavior when reloading "exceptions"

noreply@sourceforge.net noreply@sourceforge.net
Sat, 21 Apr 2001 23:44:16 -0700


Bugs item #417913, was updated on 2001-04-21 14:57
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=417913&group_id=5470

Category: Extension Modules
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Michael Klatt (michaelklatt)
Assigned to: Nobody/Anonymous (nobody)
>Summary: odd behavior when reloading "exceptions"

Initial Comment:

After reloading the "exceptions" module, "exceptions.OSError" is not the same object as "os.error".

Let me illustrate by example:


# ./python
Python 2.1 (#1, Apr 20 2001, 23:16:45) 
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import os, exceptions
>>> os.error == exceptions.OSError
1
>>> reload(exceptions)
<module 'exceptions' (built-in)>
>>> os.error == exceptions.OSError
0
>>>


Here's an example of how this could wreak havoc in a python script:

# ./python
Python 2.1 (#1, Apr 20 2001, 23:16:45) 
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import os
>>> import exceptions
>>> 
>>> try:
...    os.stat("dummy")
... except os.error, e:
...     print "exception caught"
... 
exception caught
>>> reload(exceptions)
<module 'exceptions' (built-in)>
>>> 
>>> try:
...    os.stat("dummy")
... except os.error, e:
...     print "exception caught"
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in ?
OSError: [Errno 2] No such file or directory: 'dummy'
>>> 


A quick test shows that this problem also occurred in python 1.5.2 and python 2.0.  I believe I 
understand why this is happening (when the exceptions module is reloaded, a new object for 
OSError is created), but I'm hoping someone has a creative solution.  

This will cause problems in any application which calls Py_NewInterpreter() (like mod_python).  
To verify this, add the following lines to Demo/embed/demo.c at line 36:

 
        PyRun_SimpleString("import os,exceptions\n");
        PyRun_SimpleString("print os.error == exceptions.OSError\n");
 
        Py_NewInterpreter(); 
        PyRun_SimpleString("import os,exceptions\n");
        PyRun_SimpleString("print os.error == exceptions.OSError\n");

The first comparison will result in 1, and the second will result in 0. 

Thanks!

Mike



----------------------------------------------------------------------

>Comment By: Michael Klatt (michaelklatt)
Date: 2001-04-21 23:44

Message:
Logged In: YES 
user_id=201661


I did find a workaround for mod_python (and any application that uses Py_NewInterpreter()...).  I modified it 
to add this line after *every* call to Py_NewInterpreter()

PyRun_SimpleString("__import__(\os\).error = __import__(\exceptions\).OSError\n");

This is ugly, but it's a works.

Mike


----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=417913&group_id=5470