Python と Jython とで動作が異なるスクリプトの例

C Python は Reference count GC であり、Jython はそうでない。それが原因で*1、C PythonJython とで動作が異なるスクリプトの例。

test.py (2008-03-23 modified):

def test(filename, s):
    open(filename, 'a').write(s)

filename = 'test.log'
for i in xrange(100):
    test(filename, str(i) + "\n")


実行例:

$ rm -f test.log
$ python test.py
$ wc test.log
     100     100     290 test.log
$ rm -f test.log
$ jython test.py
$ wc test.log
       2       2       6 test.log
$ cat test.log
51
99

(環境:MacOS X 10.4, Java 1.5.0_13, CPython 2.5.2, Jython 2.2.1)

*1:動作が異なるのは GC の違いに起因すると思うけど、もしそれ以外の原因が考えられる場合は教えてください。