Issue2105

classification
Title: Inconsistent conversion of PyObject to String in StdoutWrapper
Type: behaviour Severity: normal
Components: Core Versions: Jython 2.7, Jython 2.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: qny31541, richardfearn
Priority: Keywords:

Created on 2013-11-30.13:50:42 by richardfearn, last changed 2014-05-08.14:52:52 by qny31541.

Files
File name Uploaded Description Edit Remove
issue2105.py richardfearn, 2013-11-30.13:59:44 Test program
Messages
msg8183 (view) Author: Richard Fearn (richardfearn) Date: 2013-11-30.13:50:40
Inconsistent conversion of PyObject to String in StdoutWrapper

Given some object o, executing

  print o

causes Jython to build a string representation of o to be displayed on the console.

The conversion is done differently depending on whether stdout/stderr are accessed through a PyFile or a PyFileWriter.

Jython uses PyFile by default, and results in an object's __str__ method being called.

PyFileWriter causes an object's __repr__ method to be called instead, which is inconsistent with CPython.
msg8184 (view) Author: Richard Fearn (richardfearn) Date: 2013-11-30.13:59:44
Running the test program using CPython (2.7.5 in this case) shows an object's __str__ method is called:

  $ python issue2105.py 
  str

Running with Jython 2.5.3 or the trunk also shows the __str__ method being called:

  $ jython issue2105.py 
  str

so out of the box, Jython is consistent with CPython.

But if stdout/stderr are changed to point to a Writer, so that Jython uses a PyFileWriter, for example by changing jython.java to:

  InteractiveConsole interp = new InteractiveConsole();
  interp.setOut(new PrintWriter(System.out));
  interp.setErr(new PrintWriter(System.err));

then the __repr__ method is called instead:

  $ jython issue2105.py 
  repr
msg8185 (view) Author: Richard Fearn (richardfearn) Date: 2013-11-30.14:02:49
On the trunk, the print method in StdoutWrapper calls printToFile if stdout is a PyFile, and calls printToFileWriter if stdout is a PyFileWriter.

printToFile does this:

  s = o.__str__().toString();

whereas printToFileWriter does this:

  s = o.toString();

which leads to the __repr__ method being called.

It's unclear why printToFileWriter doesn't also invoke the __str__ method.
msg8347 (view) Author: Jonathan Blakes (qny31541) Date: 2014-05-08.14:52:51
Submitted Bitbucket pull request: https://bitbucket.org/jython/jython/pull-request/32/2105-fix-inconsistent-conversion-of/diff
History
Date User Action Args
2014-05-08 14:52:52qny31541setnosy: + qny31541
messages: + msg8347
2013-11-30 14:02:49richardfearnsetmessages: + msg8185
2013-11-30 13:59:44richardfearnsetfiles: + issue2105.py
messages: + msg8184
2013-11-30 13:50:42richardfearncreate