[Python-bugs-list] [ python-Bugs-563730 ] isinstance() fails to identify obj class

noreply@sourceforge.net noreply@sourceforge.net
Tue, 04 Jun 2002 02:35:14 -0700


Bugs item #563730, was opened at 2002-06-03 06:00
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=563730&group_id=5470

Category: None
Group: Python 2.2.1
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: Jessica Preston (jesspreston)
Assigned to: Nobody/Anonymous (nobody)
Summary: isinstance() fails to identify obj class

Initial Comment:
The problem seems to be that if I create two objects
using the same class
constructor, but having imported the class differently,
the isinstance() method
fails to identify them as being the same. The problem
only occurs (I think)
when cPickle is used, at least, that was how I managed
to reproduce it.

I have attached an example which includes a README and
a couple of directories:

SampleDir contains
- Simple.py (a simple class that has a compare() method
that uses isinstance)
- Main.py

SampleDir/NestedDir contains
- CreatePickle.py (which creates a Simple() object and
pickles it into the cwd)

Main.py unpickles the Simple object created by
CreatePickle.py, then compares it against a newly
created Simple() object using the Simple.compare()
method. It should raise as Assertion error claiming
that the passed object was not a Simple object.
However, it is.

I looked through existing bug reports and found a very
similar one from 2001 with tracking number 467267 -
this was marked as invalid, I think, but I'm not sure
whether this resolves my problem.

The README contains instructions on running the
example, plus my system information.

I am running RedHat7.1, with kernel 2.4.18 (if
relevant) and
the version of python I am using is Python 2.2.1

I hope that this will demonstrate the problem.

Thanks
-jess

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

>Comment By: Martin v. Löwis (loewis)
Date: 2002-06-04 11:35

Message:
Logged In: YES 
user_id=21627

So this clearly is a user error: you are using a nested
module as the main program, and have this module import
other modules as top-level modules.

Closing this report as invalid.

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

Comment By: Walter Dörwald (doerwalter)
Date: 2002-06-03 18:51

Message:
Logged In: YES 
user_id=89016

This has nothing to do with cPickle directly. Bug #467267
seems to report exactly the same problem. Adding the
following code to Simple.compare demonstrates that the
Simple module was imported twice and generated two different
module objects:
		import sys
		print id(sys.modules["Simple"])
		print id(sys.modules["SampleDir.Simple"])

The pickle file includes the module name SampleDir.Simple,
which will implicitly be imported. But Main.py does an
"import Simple".

The simplest example that provokes this error is the
following script (which should be named test.py and started
with "python test.py")
class Foo:
    pass
# reimport the module under a different name
import test
assert isinstance(Foo(), test.Foo)

Moving the CreatePickle.py file from NestedDir to SampleDir
and changing the import from "from SampleDir.Simple import
Simple
" to "from Simple import Simple" fixes the problem.

Maybe Python should use absolute file/package names as keys
for sys.modules?


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

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