Message12203

Author jeff.allen
Recipients jeff.allen, rferguson@devendortech.com
Date 2018-12-11.23:43:25
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1544571806.46.0.788709270274.issue2719@psf.upfronthosting.co.za>
In-reply-to
Content
I'm raising this to cover what (I think) https://github.com/jythontools/jython/pull/126 aims to fix.

The following simple function probes str() and repr() behaviour for a supplied instance:

def dump(a):
    print type(a).__name__

    print "  repr(a):     ", repr(a)
    if hasattr(a, '__repr__'):
        print "  a.__repr__():", a.__repr__()
    else:
        print "  a.__repr__(): not defined"

    print "  str(a):      ", str(a)
    if hasattr(a, '__str__'):
        print "  a.__str__(): ", a.__str__()
    else:
        print "  a.__str__():  not defined"

In CPython:
>>> dump(buffer('hello'))
buffer
  repr(a):      <read-only buffer for 0x0000000002EA0238, size -1, offset 0 at 0x0000000002DCED88>
  a.__repr__(): <read-only buffer for 0x0000000002EA0238, size -1, offset 0 at 0x0000000002DCED88>
  str(a):       hello
  a.__str__():  hello
>>> def g(): pass
...
>>> dump(g)
function
  repr(a):      <function g at 0x0000000002E9C198>
  a.__repr__(): <function g at 0x0000000002E9C198>
  str(a):       <function g at 0x0000000002E9C198>
  a.__str__():  <function g at 0x0000000002E9C198>

While in Jython:
>>> dump(buffer('hello'))
buffer
  repr(a):      <read-only buffer for 0x8, size -1, offset 0 at 0x0x9>
  a.__repr__(): <buffer object at 0x9>
  str(a):       hello
  a.__str__():  <read-only buffer for 0x8, size -1, offset 0 at 0x0x9>
>>> def g(): pass
...
>>> dump(g)
function
  repr(a):      <function g at 0xa>
  a.__repr__(): <function object at 0xa>
  str(a):       <function g at 0xa>
  a.__str__():  <function g at 0xa>

Apart from the bovine superfluity in repr for buffer (one 0x too many), the main bug is that __repr__ and __str__ do not match expectations. The GitHub PR blames the curious and unexplained cross-wiring of __str__ and __repr__ as exposed from PyObject, without which there is apparently a stack overflow when new-style classes are defined. (This happens because PyObjectDerived.toString calls __repr__.)

While the cross-wiring doesn't lead to these errors directly, it makes them more difficult to understand and solve. The PR seeks to eliminate this confusion, defuse the recursion another way, and fix the substantive errors.
History
Date User Action Args
2018-12-11 23:43:26jeff.allensetrecipients: + jeff.allen, rferguson@devendortech.com
2018-12-11 23:43:26jeff.allensetmessageid: <1544571806.46.0.788709270274.issue2719@psf.upfronthosting.co.za>
2018-12-11 23:43:26jeff.allenlinkissue2719 messages
2018-12-11 23:43:25jeff.allencreate