[Python-bugs-list] [ python-Bugs-467267 ] isinstance depends on import form
noreply@sourceforge.net
noreply@sourceforge.net
Tue, 02 Oct 2001 14:01:26 -0700
Bugs item #467267, was opened at 2001-10-02 11:56
You can respond by visiting:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=467267&group_id=5470
Category: Python Interpreter Core
Group: Python 2.1.1
Status: Open
Resolution: None
Priority: 5
Submitted By: Luis P Caamano (lcaamano)
Assigned to: Nobody/Anonymous (nobody)
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: Luis P Caamano (lcaamano)
Date: 2001-10-02 14: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 13: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 12: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:
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=467267&group_id=5470