Issue448398

classification
Title: open(\'test.txt\',\'w\').write(\'test\') fails
Type: Severity: normal
Components: Core Versions:
Milestone:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: bckfnn, cgroves, eris, leouserz, pedronis
Priority: low Keywords:

Created on 2001-08-06.12:48:39 by eris, last changed 2007-04-23.03:47:52 by cgroves.

Messages
msg366 (view) Author: Shae Erisson (eris) Date: 2001-08-06.12:48:39
Debian Linux, unstable
Blackdown JDK 1.3.1-1
Debian package of jython 2.1a2
open('test.txt','w').write('test') fails

in short, it seems you *must* call file.close or 
file.flush for jython to write a file to the disk.
even 
f = open('test.txt','w')
f.write('test')
import sys ; sys.exit()
will not write to the disk.

the file always ends up being empty unless flush() or 
close() is called.
msg367 (view) Author: Finn Bock (bckfnn) Date: 2001-10-30.20:25:53
Logged In: YES 
user_id=4201

I haven't made up my mind about this yet. We could register 
open PyFile instances in a global collection and have some 
exit code that close all the files in the collection. An 
implementation would probably have to use weak references 
on java2. On java1 the GC'ing of files would then be 
disabled, which means that we must have a way of disabling 
the entire close-at-exit feature.

It feels like a lot of code with only a little gain but I'm 
not dismissing it completely because the current behaviour 
result in complete and silent loss of a lot of information.

Is it worth the trouble? Of so, what should the default be? 
Enabled close-at-exit for java2 and disabled for java1?


Note 1: I'm not even considering changing the behaviour for
    open('test.txt','w').write('test')
Code like that will still not close the file immediately.

Note 2: Supporting the buffer size argument could be 
another workaround.
At the moment python files are always buffered and while 
disabling
buffering could help for the example code, the two issues 
(close-at-exit
and buffering) are different problems.
msg368 (view) Author: Samuele Pedroni (pedronis) Date: 2001-11-23.16:26:27
Logged In: YES 
user_id=61408

Probably I'm missing something but cannot we use
the same techinique used by Sun for FileInput/OutputStream
in the java std library?
In the source for jdk 1.3 they both have a finalize
method that calls close when meaningful.

We could do the same for RFileWrapper.

I know that finalize should not be ab/used but...
msg369 (view) Author: Finn Bock (bckfnn) Date: 2001-11-27.11:11:16
Logged In: YES 
user_id=4201

That would AFAICT require that finalizers should run at 
exit:
http://java.sun.com/products/jdk/1.2/docs/api/java/lang/Syst
em.html#runFinalizersOnExit(boolean)

That is not something that jython can/should enforce IMO. 
Adding a finalize method would still be an improvement in 
the cases where the file reference is dropped.
msg370 (view) Author: Samuele Pedroni (pedronis) Date: 2002-03-18.15:16:13
Logged In: YES 
user_id=61408

Oops, You're right. 

We probably should use a mixed
approach using both finalizers and a registration
mechanism.
If we use weak refs we should use:
System.gc()
System.runFinalization()

[Although from my experiments it seems that gc implies
runFinalization, but the doc does not confirm this]

in order to be sure that we do not have
cleared weak refs was referent finalizer will
not be called because we will just exit then soon.
msg371 (view) Author: Deleted User leouserz (leouserz) Date: 2007-01-17.18:02:06
patch for this is up here:
http://sourceforge.net/tracker/index.php?func=detail&aid=1637931&group_id=12867&atid=312867

leouser
msg372 (view) Author: Charlie Groves (cgroves) Date: 2007-04-23.03:47:52
Patch applied in r3178.
History
Date User Action Args
2001-08-06 12:48:39eriscreate