Hello Sharon,

I tried with regular Python and copying file objects does not seem to work well.

I copied a file object and while I can access data from the source, the copied target does not work and says that the file is closed.

I think the problem is fundamentally that the object owns the file and this resource is not managed by the garbage collector.

I suggest you write a wrapper class to manage this issue on file objects.

2012/1/31 Sharon Lucas <lucass@us.ibm.com>
With Jython 2.5.2, it allows doing a shallow or deep copy of a file object (which wasn't allowed in Jython 2.1).  However, when you try to use the copied file (e.g. print it, write to it, etc), you get a NullPointerException.  Is this a known bug?

It seems to me that if Jython 2.5.2 also cannot perform a valid shallow copy or deep copy of a PyFile object that it should give a similar error as it did in Jython 2.1 when doing the copy instead of later when trying to use the copied PyFile object.  This new behavior is causing issues in our Java application that we migrated to use Jython 2.5.2 instead of Jython 2.1.

Jython 2.5.2 (Release_2_5_2:7206, Mar 2 2011, 23:12:06)
[IBM J9 VM (IBM Corporation)] on java1.5.0
Type "help", "copyright", "credits" or "license" for more information.
>>> log_file = open('C:\Test\log.txt', "w")
>>> print log_file
<open file 'C:\Test\log.txt', mode 'w' at 0x2>
>>> import copy
>>> log_file2 = copy.deepcopy(log_file)   <== copy.copy(log_file) will also cause NullPointerException
>>> type(log_file2)
<type 'file'>
>>> print log_file2                       <== log_file2.write("hi") and str(log_file2) also cause NullPointerExceptions.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
        at org.python.core.PyFile.file_toString(PyFile.java:499)
        at org.python.core.PyFile.toString(PyFile.java:510)
        at org.python.core.PyObject.__repr__(PyObject.java:174)
        at org.python.core.PyObject.__str__(PyObject.java:207)
        at org.python.core.StdoutWrapper.print(StdoutWrapper.java:116)
        at org.python.core.StdoutWrapper.println(StdoutWrapper.java:214)
        at org.python.core.Py.println(Py.java:1393)
        at org.python.pycode._pyx5.f$0(<stdin>:1)
        at org.python.pycode._pyx5.call_function(<stdin>)
        at org.python.core.PyTableCode.call(PyTableCode.java:165)
        at org.python.core.PyCode.call(PyCode.java:18)
        at org.python.core.Py.runCode(Py.java:1261)
        at org.python.core.Py.exec(Py.java:1305)
        at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:215)
        at org.python.util.InteractiveInterpreter.runcode(InteractiveInterpreter.java:89)
        at org.python.util.InteractiveInterpreter.runsource(InteractiveInterpreter.java:70)
        at org.python.util.InteractiveInterpreter.runsource(InteractiveInterpreter.java:46)
        at org.python.util.InteractiveConsole.push(InteractiveConsole.java:110)
        at org.python.util.InteractiveConsole.interact(InteractiveConsole.java:90)
        at org.python.util.jython.run(jython.java:317)
        at org.python.util.jython.main(jython.java:129)

java.lang.NullPointerException: java.lang.NullPointerException

In Jython 2.1, attempting to perform a shallow or deep copy of a PyFile object failed up front with an error message saying that objects of type PyFile are un-copyable.  For example:

Jython 2.1 on java1.5.0 (JIT: j9jit23)

Type "copyright", "credits" or "license" for more information.
>>> log_file = open('C:\Test\log.txt', "y")
>>> print log_file
<file C:\Test\log.txt, mode y at 1392792324>
>>> import copy
>>> log_file2 = copy.deepcopy(log_file)
Traceback (innermost last):
  File "<console>", line 1, in ?
  File "C:\tools\jython-2.1\Lib\copy.py", line 159, in deepcopy
Error: un-deep-copyable object of type org.python.core.PyFile
>>> log_file2 = copy.copy(log_file)
Traceback (innermost last):
  File "<console>", line 1, in ?
  File "C:\tools\jython-2.1\Lib\copy.py", line 78, in copy
Error: un(shallow)copyable object of type org.python.core.PyFile

Sharon Lucas
IBM Austin,   lucass@us.ibm.com
(512) 286-7313 or Tieline 363-7313

Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
Jython-users mailing list

Pierre Thibault

Python Developer/Développeur Python
Montréal, QC