Title: Creating a PyFile from an InputStream is broken.
Type: Severity: urgent
Components: Core Versions:
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: pjenvey Nosy List: amak, fwierzbicki, kam, pjenvey
Priority: Keywords:

Created on 2008-11-12.17:15:08 by amak, last changed 2008-12-18.22:45:37 by amak.

msg3778 (view) Author: Alan Kennedy (amak) Date: 2008-11-12.17:15:08
When trying to create a PyFile from a, a TypeError
is created. The following snippet illustrates

# -=-=-=-=-=-=-=-=-=-=-=
from import FileInputStream
from org.python.core import PyFile

fis = FileInputStream("error.txt")
pyf = PyFile(fis)

Which outputs 

Traceback (most recent call last):
  File "", line 7, in <module>
    pyf = PyFile(fis)
TypeError: coercing to Unicode: need string, 'javainstance' type found

This bug was introduced sometime between 2.5a3 and 2.5b0.

It was reported by two different modjy users.

One of them on Frank's blog
msg3779 (view) Author: Alan Kennedy (amak) Date: 2008-11-12.17:26:50
Until this is fixed, I can only recommend to users not to use modjy with
jython 2.5b0; use a previous version instead.
msg3780 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2008-11-12.18:25:22
Alan, there was a recent backwards incompatible change that will mean
that the code sample you have posted would need to be changed to:

from import FileInputStream
from org.python.core.util import FileUtil

fis = FileInputStream("error.txt")
pyf = FileUtil.wrap(fis)

I've started a conversation on the jython-dev list about this.
msg3814 (view) Author: Philip Jenvey (pjenvey) Date: 2008-11-23.01:57:33
We decided on:

o Nuking support for In/OutputStream in file() and PyFile(). groves 
originally did this, mostly because supporting this requires a really 
gross hack

o Going forward we want this functionality to live elsewhere, in 

o However since open is now a function in 2.5, supporting 
In/OutputStreams there isn't such a lame hack -- so I've added support 
for this there in r5606 -- albeit deprecated. Using open on streams will 
work on 2.5 but emit a DeprecationWarning to use FileUtil instead. We'll 
remove this in 2.6

So modjy should really move to FileUtil if sys.version_info >= (2, 5)
msg3965 (view) Author: Kevin A. Mitchell (kam) Date: 2008-12-18.20:58:23
I reported the same problem in, not
having found this one in my search. I realize the case is closed, but I
wanted to record some background in case it ever gets revisited.

The application that broke for me was a unit test rig (and accompanying
application shell) that tested equivalent interfaces in both .NET (with
IronPython) and Java (with Jython). Jython and IronPython both allowed
constructing file objects from .NET/Java streams by using the file()
constructor, which I tried because it seemed obvious. And then I used it
because I had achieved compatibility without having to resort to some
adapter function.

I know it's rather odd to discuss feature parity with some other Python
implementation that isn't CPython. I've found it quite useful that the
two implementations are as compatible as they are.
msg3967 (view) Author: Alan Kennedy (amak) Date: 2008-12-18.22:45:37
I must confess that I was a little perplexed by the change myself,
especially when you look at the wrap() code

    public static PyFile wrap(InputStream is, int bufsize) {
        return new PyFile(is, bufsize);

    public static PyFile wrap(InputStream is) {
        return wrap(is, -1);

This could have been dealt with by adding extra constructors for
PyFile(InputStream) with defaults for the bufsize parameter.
Date User Action Args
2008-12-18 22:45:37amaksetmessages: + msg3967
2008-12-18 20:58:24kamsetnosy: + kam
messages: + msg3965
2008-11-23 01:57:34pjenveysetstatus: open -> closed
resolution: fixed
messages: + msg3814
2008-11-23 00:01:29pjenveysetassignee: pjenvey
nosy: + pjenvey
2008-11-12 18:25:22fwierzbickisetmessages: + msg3780
2008-11-12 17:51:27fwierzbickisetnosy: + fwierzbicki
2008-11-12 17:26:50amaksetmessages: + msg3779
2008-11-12 17:15:08amakcreate