isinstance() bug

Skip Montanaro skip at pobox.com
Wed Jan 28 12:32:19 EST 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




More information about the Python-list mailing list