Title: Inconsistent conversion of PyObject to String in StdoutWrapper
Type: behaviour Severity: normal
Components: Core Versions: Jython 2.7
Milestone: Jython 2.7.0
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: zyasoft Nosy List: qny31541, richardfearn, zyasoft
Priority: high Keywords:

Created on 2013-11-30.13:50:42 by richardfearn, last changed 2015-04-27.01:52:52 by zyasoft.

File name Uploaded Description Edit Remove richardfearn, 2013-11-30.13:59:44 Test program
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 

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

  $ jython 

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 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 
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:
msg9335 (view) Author: Jim Baker (zyasoft) Date: 2015-01-07.07:08:18
Need to commit the outstanding PR attached to this bug, it's been too long!
msg9892 (view) Author: Jim Baker (zyasoft) Date: 2015-04-19.20:47:44
Fixed as of

Made it in 2.7.0 after all
Date User Action Args
2015-04-27 01:52:52zyasoftsetstatus: pending -> closed
2015-04-19 20:47:44zyasoftsetstatus: open -> pending
resolution: fixed
milestone: Jython 2.7.1 -> Jython 2.7.0
messages: + msg9892
versions: - Jython 2.5
2015-04-15 20:44:50zyasoftsetmilestone: Jython 2.7.1
2015-01-07 07:08:18zyasoftsetpriority: high
assignee: zyasoft
messages: + msg9335
nosy: + zyasoft
2014-05-08 14:52:52qny31541setnosy: + qny31541
messages: + msg8347
2013-11-30 14:02:49richardfearnsetmessages: + msg8185
2013-11-30 13:59:44richardfearnsetfiles: +
messages: + msg8184
2013-11-30 13:50:42richardfearncreate