isinstance() bug

Discussion in 'Python' started by Michal Vitecek, Jan 28, 2004.

  1. 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 ()
    Michal Vitecek, Jan 28, 2004
    #1
    1. Advertising

  2. "Michal Vitecek" <> wrote in message
    news:...
    > 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 ()
    >


    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.
    Sidharth Kuruvila, Jan 28, 2004
    #2
    1. Advertising

  3. 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
    Skip Montanaro, Jan 28, 2004
    #3
  4. i dont know unix.
    how does abs path work with sym-links.
    Sidharth Kuruvila, Jan 28, 2004
    #4
  5. 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
    Skip Montanaro, Jan 28, 2004
    #5
  6. Michal Vitecek

    Jeff Epler Guest

    posixpath.abspath and symlinks

    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
    Jeff Epler, Jan 28, 2004
    #6
  7. Stupid post must be ignored at all costs

    "Sidharth Kuruvila" <> wrote in message
    news:bv8qhc$ph1mk$-berlin.de...
    > i dont know unix.
    > how does abs path work with sym-links.
    >
    >
    >
    Sidharth Kuruvila, Jan 28, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joona I Palaste

    Re: isInstance problem

    Joona I Palaste, Jul 1, 2003, in forum: Java
    Replies:
    4
    Views:
    1,016
    George W. Cherry
    Jul 2, 2003
  2. Ben Jessel

    Re: isInstance problem

    Ben Jessel, Jul 10, 2003, in forum: Java
    Replies:
    0
    Views:
    385
    Ben Jessel
    Jul 10, 2003
  3. Michal Vitecek

    Re: isinstance() bug

    Michal Vitecek, Jan 28, 2004, in forum: Python
    Replies:
    7
    Views:
    327
    Rainer Deyke
    Jan 29, 2004
  4. Mac

    bug with isinstance() ?

    Mac, Jun 1, 2005, in forum: Python
    Replies:
    7
    Views:
    321
  5. dmitrey

    seems like a bug in isinstance()

    dmitrey, May 6, 2011, in forum: Python
    Replies:
    6
    Views:
    555
    Gregory Ewing
    May 7, 2011
Loading...

Share This Page