isinstance() bug

M

Michal Vitecek

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:

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

Sidharth Kuruvila

Michal Vitecek said:
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:


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

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.
 
S

Skip Montanaro

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
 
S

Skip Montanaro

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. '/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. 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
 
J

Jeff Epler

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:OSError: [Errno 22] Invalid argument: '/'

Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top