is this a python bug?

Wolfgang Lipp paragate at gmx.net
Thu May 31 00:46:16 EDT 2001


Dear Python fans,

I'm not so sure about what this is I've found.
I run the code snippet below (Py2.1, Win95),
and the Python Interpreter blows up. Now, this
code does have a few problems and looks of
sort-a-recursive (when you comment out line
(*), you get in fact an infinite recursion).
Obviously, when trying to print the __dir__ in
line (**), then a __repr__ or __str__ method
of _Indirect would be in need; no such method
is found, whereupon the _Indirect instance
tries to hand the problem back to the parent
instance. Shouldn't we expect a plain KeyError
here? I'm puzzled.

The code below is more or less reduced to the
max. Don't fight over the meaningfulness of it;
it's but a tiny senseless portion of the
original. My issue is here: Granted that the
code is dumb, should it (or any code fed to
python.exe) be able to blow up the interpreter?
I don't think so. Please have a look whether the
script runs on you machine if you like to.

Cheers,

Wolfgang
lipp at epost.de


#   +++'_'+++
#   {'_':  ***'__repr__'***
#   +++'__private__'+++
#   <__main__._Indirect instance at 00777A2C>, '__public__': {}, <nil>:
#   Tool completed with exit code -1073741819


#   PYTHON caused an invalid page fault in
#   module PYTHON21.DLL at 0137:1e147601.
#   Registers:
#   EAX=65636e61 CS=0137 EIP=1e147601 EFLGS=00010202
#   EBX=00000000 SS=013f ESP=0063fbf0 EBP=7802419f
#   ECX=0063fe28 DS=013f ESI=7803bbb0 FS=2307
#   EDX=65636e61 ES=013f EDI=7803bbb0 GS=0000
#   Bytes at CS:EIP:
#   8b 08 85 c9 7f 13 50 51 68 64 25 19 1e 57 ff 15
#   Stack dump:
#       00772470 7803bbb0 00773fcc 780115fb
#       1e12bc24 65636e61 7803bbb0 00000000
#       7803bbb0 00762790 7802419f 00000000
#       1e14768a 00000002 00000002 00000001



class _Indirect:

    def __init__( self, parent = None ):
        self.__dict__[ 'parent' ] = parent

    def __getattr__( self, name ):
        print "***'%s'***" % name
        self.parent.__private__
        return self.__dict__[ '__private__' ][ 'name' ]

class V:
    def __getattr__( self, name ):
        print "+++'%s'+++" % name
        try:
            return self.__public__[ name ]
        except KeyError:
            if name == '__private__':
                self.__dict__[ '__private__' ] = {}
                return self.__dict__[ '__private__' ]
            elif name == '_':
                self.__dict__[ '_' ] = _Indirect( self )
                return self._

if __name__ == '__main__':

    v = V()
    v.__dict__[ '__public__' ] = {} #    (*)
    v._
    print v.__dict__                #    (**)



More information about the Python-list mailing list