From: <pj...@us...> - 2009-09-09 03:24:25
|
Revision: 6768 http://jython.svn.sourceforge.net/jython/?rev=6768&view=rev Author: pjenvey Date: 2009-09-09 03:24:04 +0000 (Wed, 09 Sep 2009) Log Message: ----------- fix array.to/fromfile on files in + mode fixes #1457 Modified Paths: -------------- trunk/jython/Lib/test/test_array_jy.py trunk/jython/src/org/python/core/PyArray.java Modified: trunk/jython/Lib/test/test_array_jy.py =================================================================== --- trunk/jython/Lib/test/test_array_jy.py 2009-09-09 03:06:20 UTC (rev 6767) +++ trunk/jython/Lib/test/test_array_jy.py 2009-09-09 03:24:04 UTC (rev 6768) @@ -1,6 +1,7 @@ # The jarray module is being phased out, with all functionality # now available in the array module. - +from __future__ import with_statement +import os import unittest from test import test_support from array import array, zeros @@ -11,7 +12,6 @@ class ArrayJyTestCase(unittest.TestCase): def test_jarray(self): # until it is fully formally removed - # While jarray is still being phased out, just flex the initializers. # The rest of the test for array will catch all the big problems. import jarray @@ -44,8 +44,34 @@ Color.RGBtoHSB(0, 255, 255, hsb1) self.assertEqual(hsb, hsb1, "hsb float arrays were not equal") + +class ToFromfileTestCase(unittest.TestCase): + + def tearDown(self): + if os.path.exists(test_support.TESTFN): + os.remove(test_support.TESTFN) + + def test_tofromfile(self): + # http://bugs.jython.org/issue1457 + x = array('i', range(5)) + with open(test_support.TESTFN, 'wb') as f: + x.tofile(f) + + x = array('i', []) + with open(test_support.TESTFN, 'r+b') as f: + x.fromfile(f, 5) + x *= 2 + x.tofile(f) + + with open(test_support.TESTFN, 'rb') as f: + x.fromfile(f, 10) + self.assertEqual(x, array('i', range(5) * 4)) + + def test_main(): - test_support.run_unittest(ArrayJyTestCase) + test_support.run_unittest(ArrayJyTestCase, + ToFromfileTestCase) + if __name__ == "__main__": test_main() Modified: trunk/jython/src/org/python/core/PyArray.java =================================================================== --- trunk/jython/src/org/python/core/PyArray.java 2009-09-09 03:06:20 UTC (rev 6767) +++ trunk/jython/src/org/python/core/PyArray.java 2009-09-09 03:24:04 UTC (rev 6768) @@ -802,15 +802,10 @@ */ public void fromfile(PyObject f, int count) { // check for arg1 as file object - if(!(f instanceof PyFile)) { + if (!(f instanceof PyFile)) { throw Py.TypeError("arg1 must be open file"); } PyFile file = (PyFile)f; - // check for read only - if(file.mode.indexOf("r") == -1) { - throw Py.TypeError("file needs to be in read mode"); - } - // read the data via the PyFile int readbytes = count * getStorageSize(); String buffer = file.read(readbytes).toString(); // load whatever was collected into the array @@ -1588,13 +1583,10 @@ * Python builtin file object to write data */ public void tofile(PyObject f) { - if(!(f instanceof PyFile)) + if (!(f instanceof PyFile)) { throw Py.TypeError("arg must be open file"); - PyFile file = (PyFile)f; - if(file.mode.indexOf("w") == -1 && file.mode.indexOf("a") == -1) { - throw Py.TypeError("file needs to be in write or append mode"); } - // write via the PyFile + PyFile file = (PyFile)f; file.write(tostring()); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |