Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > isinstance() bug

Reply
Thread Tools

isinstance() bug

 
 
Michal Vitecek
Guest
Posts: n/a
 
      01-28-2004
hello,

please consider the following situation:

under the current directory there's a subdirectory 'package' with two
files: __init__.py and module.py

./package:
__init__.py
module.py

module.py contains:

class A(object):
pass

aModule = A()


now, let's do:

current directory:

>>> import package.module
>>> type(package.module.aModule)

<class 'package.module.A'>
>>> isinstance(package.module.aModule, package.module.A) # so far good

1
>>> a = package.module.A()
>>> isinstance(a, package.module.A) # so far good
>>> import sys
>>> sys.path.append('package')
>>> import module
>>> a = module.A()
>>> isinstance(a, package.module.A) # will return 0 !!!

0
>>> isinstance(package.module.aModule, module.A) # will return 0 !!!

0
>>>


how is it possible that it IS important how you imported a class
definition for isinstance() to work? it's insane!

--
fuf ((E-Mail Removed))

 
Reply With Quote
 
 
 
 
Sidharth Kuruvila
Guest
Posts: n/a
 
      01-28-2004

"Michal Vitecek" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> hello,
>
> please consider the following situation:
>
> under the current directory there's a subdirectory 'package' with two
> files: __init__.py and module.py
>
> ./package:
> __init__.py
> module.py
>
> module.py contains:
>
> class A(object):
> pass
>
> aModule = A()
>
>
> now, let's do:
>
> current directory:
>
> >>> import package.module
> >>> type(package.module.aModule)

> <class 'package.module.A'>
> >>> isinstance(package.module.aModule, package.module.A) # so far good

> 1
> >>> a = package.module.A()
> >>> isinstance(a, package.module.A) # so far good
> >>> import sys
> >>> sys.path.append('package')
> >>> import module
> >>> a = module.A()
> >>> isinstance(a, package.module.A) # will return 0 !!!

> 0
> >>> isinstance(package.module.aModule, module.A) # will return 0 !!!

> 0
> >>>

>
> how is it possible that it IS important how you imported a class
> definition for isinstance() to work? it's insane!
>
> --
> fuf ((E-Mail Removed))
>


its got to do with how python imports modules.
both imports are treated as seperate modules because the relative paths are
diferent.
python is a dynamic language it figures out where modules are located at run
time, it would be expensive to check all paths to see if they ended up at
the same file.


 
Reply With Quote
 
 
 
 
Skip Montanaro
Guest
Posts: n/a
 
      01-28-2004

Sidharth> python is a dynamic language it figures out where modules are
Sidharth> located at run time, it would be expensive to check all paths
Sidharth> to see if they ended up at the same file.

Maybe not:

Help on function abspath in module posixpath:

abspath(path)
Return an absolute path.

Considering all the other stuff import has to do, this seems like only a
small extra bit of work.

Skip

 
Reply With Quote
 
Sidharth Kuruvila
Guest
Posts: n/a
 
      01-28-2004
i dont know unix.
how does abs path work with sym-links.



 
Reply With Quote
 
Skip Montanaro
Guest
Posts: n/a
 
      01-28-2004

Sidharth> i dont know unix.
Sidharth> how does abs path work with sym-links.

Not well. Seems that os.path.realpath does the right thing though:

% ls -l /etc/rc2.d
lrwxrwxrwx 1 root root 10 May 21 2002 /etc/rc2.d -> rc.d/rc2.d/
% python
Python 2.2.3 (#1, Jun 21 2003, 08:08:22)
[GCC 3.0.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.path.abspath("/etc/rc2.d")

'/etc/rc2.d'
>>> os.path.realpath("/etc/rc2.d")

'/etc/rc.d/rc2.d'

As someone else pointed out, the issue of multiple hard links to the same
file is a bit tougher nut to crack.

% cd ~/tmp
% cat > a.py
class A: pass
% ln a.py b.py
% python
Python 2.2.3 (#1, Jun 21 2003, 08:08:22)
[GCC 3.0.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import a
>>> import b
>>> a.A == b.A

0

You need to compare inode numbers to see if you have the same or different
files.

>>> import stat
>>> os.stat(a.__file__)[stat.ST_INO]

37041L
>>> os.stat(b.__file__)[stat.ST_INO]

37041L

Skip

 
Reply With Quote
 
Jeff Epler
Guest
Posts: n/a
 
      01-28-2004
On Wed, Jan 28, 2004 at 10:37:39PM +0530, Sidharth Kuruvila wrote:
> i dont know unix.
> how does abs path work with sym-links.



os.abspath does not check whether any components are symlinks. In the
presence of symlinks,
/a/../b
and
/b
can refer to separate files if /a is a symlink to a directory. As far as
I know, posixpath.abspath normalizes the first into the second anyway.

Similarly, if you have a symlink
/a -> c
then posixpath.abspath("a") doesn't return /c, it still returns /a

You can use os.readlink() on posix systems to get the thing a symlink
points to:
>>> os.readlink("/proc/self")

'14717'
>>> os.readlink("/")

OSError: [Errno 22] Invalid argument: '/'

Jeff

 
Reply With Quote
 
Sidharth Kuruvila
Guest
Posts: n/a
 
      01-28-2004
Stupid post must be ignored at all costs

"Sidharth Kuruvila" <(E-Mail Removed)> wrote in message
news:bv8qhc$ph1mk$(E-Mail Removed)-berlin.de...
> i dont know unix.
> how does abs path work with sym-links.
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
*bug* *bug* *bug* David Raleigh Arnold Firefox 12 04-02-2007 03:13 AM
ASP.NET Login control bug or SQL 2005 bug? RedEye ASP .Net 2 12-13-2005 10:57 AM
Re: BUG? OR NOT A BUG? John ASP .Net 2 09-21-2005 10:31 AM
Bug Parade Bug 4953793 Michel Joly de Lotbiniere Java 4 12-02-2003 05:05 AM
how to report bug to g++ ? got a bug and fixed up source code DarkSpy C++ 4 06-27-2003 09:05 AM



Advertisments