[Python-bugs-list] [ python-Bugs-467267 ] isinstance depends on import form
noreply@sourceforge.net
noreply@sourceforge.net
Mon, 16 Sep 2002 14:58:18 -0700
Bugs item #467267, was opened at 2001-10-02 14:56
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=467267&group_id=5470
Category: Python Interpreter Core
Group: Not a Bug
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: Luis P Caamano (lcaamano)
Assigned to: Guido van Rossum (gvanrossum)
Summary: isinstance depends on import form
Initial Comment:
isinstance(obj, cls) fails if cls is in a package and
the import line of the obj creator is different from
the import line in the code calling isinstance.
It seems to me that the problem is the address test in
PyObject_IsInstance(), because different import forms
produce objects with different addresses.
A limited workaround would be to test for class name
but that doesn't include subclasses or types. So,
instead of:
if isinstance(obj, class):
use
if obj.__class__.__name__ == 'classname':
-------Sample--------------------
Python 2.1.1 (#2, Jul 25 2001, 17:54:11)
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-81)] on
linux2
Type "copyright", "credits" or "license" for more
information.
>>> from spam.eggs import SpamAndEggs
>>> import spam.eggs
>>> '0x%x' % id(spam.eggs.SpamAndEggs)
'0x8161ce4'
>>> '0x%x' % id(SpamAndEggs)
'0x814c29c'
>>> sag = spam.eggs.SpamAndEggs()
>>> isinstance(sag, SpamAndEggs)
0
>>> isinstance(sag, spam.eggs.SpamAndEggs)
1
>>> sag.__class__
<class spam.eggs.SpamAndEggs at 0x8161ce4>
>>> sag.__class__.__name__
'SpamAndEggs'
>>> SpamAndEggs.__name__
'SpamAndEggs'
>>> spam.eggs.SpamAndEggs.__name__
'SpamAndEggs'
>>>
----------------------------------------------------------------------
>Comment By: Guido van Rossum (gvanrossum)
Date: 2002-09-16 17:58
Message:
Logged In: YES
user_id=6380
This problem happens when the same module is loaded twice in
two different ways, one as part of a package, and one as a
top-level module. It is not a bug.
----------------------------------------------------------------------
Comment By: Nobody/Anonymous (nobody)
Date: 2002-09-16 17:21
Message:
Logged In: NO
I hit this exact problem in Python 2.2.1, and am not sure why
this is a closed issue. The behavior seems to be incorrect (it
broke a runtime event-type checking system I wrote in
python.)
It is true that if the import statement in the object creator and
the caller of isinstance are identical, the problem does not
occur. Still, this is only a workaround, and the problem
appears to persist in the general case.
What is the status of this bug? I would be happy to provide
example code if needed. Please email
<beach@verinet.com>
Thanks!
----------------------------------------------------------------------
Comment By: Guido van Rossum (gvanrossum)
Date: 2001-10-02 17:09
Message:
Logged In: YES
user_id=6380
OK, closing this as Invalid.
----------------------------------------------------------------------
Comment By: Luis P Caamano (lcaamano)
Date: 2001-10-02 17:01
Message:
Logged In: YES
user_id=279987
With the site-packages/pkg1.pth file:
>>> from pkg1.spam.eggs import SpamAndEggs
>>> import spam.eggs
>>> '0x%x' % id(SpamAndEggs)
'0x814e174'
>>> '0x%x' % id(spam.eggs.SpamAndEggs)
'0x8168fac'
>>> import pkg1.spam.eggs
>>> '0x%x' % id(pkg1.spam.eggs.SpamAndEggs)
'0x814e174'
>>>
It is starting to make sense.
--
lpc
----------------------------------------------------------------------
Comment By: Luis P Caamano (lcaamano)
Date: 2001-10-02 16:34
Message:
Logged In: YES
user_id=279987
After getting your email I went and rechecked things in
site-packages and the only suspicious thing was a .pth
file. I removed the .pth file and now I'm getting the same
address. I'm still investigating. At this moment I have
no idea why I'm getting different objects.
Let me investigate a little bit more and I'll get back to
you.
I apologize for not having verified this better.
--
Luis Caamano
----------------------------------------------------------------------
Comment By: Guido van Rossum (gvanrossum)
Date: 2001-10-02 15:26
Message:
Logged In: YES
user_id=6380
We need more information: please show all code involved (use
the file upload, please check the checkbox or the upload
won't work; you must be logged in to SourceForge as
yourself).
>From the session you post, it seems that there are two
distinct SpamAndEggs objects, but without seeing your code
it's hard to understand why.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=467267&group_id=5470