[Jython-checkins] jython (merge default -> default): Merge slice of pickle
jeff.allen
jython-checkins at python.org
Sun Mar 3 18:11:58 CET 2013
http://hg.python.org/jython/rev/e7c373ed9da2
changeset: 7073:e7c373ed9da2
parent: 7071:baf84d8e91d0
parent: 7072:1b5e1afaddb4
user: Jeff Allen <ja...py at farowl.co.uk>
date: Sun Mar 03 17:01:25 2013 +0000
summary:
Merge slice of pickle
files:
Lib/_jyio.py | 112 ++++++++++++++++++++++++-
Lib/test/test_memoryio.py | 20 ----
2 files changed, 104 insertions(+), 28 deletions(-)
diff --git a/Lib/_jyio.py b/Lib/_jyio.py
--- a/Lib/_jyio.py
+++ b/Lib/_jyio.py
@@ -275,10 +275,48 @@
self._buffer = buf
self._pos = 0
+ # Jython: modelled after bytesio.c::bytesio_getstate
def __getstate__(self):
- if self.closed:
- raise ValueError("__getstate__ on closed file")
- return self.__dict__.copy()
+ d = getattr(self, '__dict__', None)
+ if d is not None :
+ d = d.copy()
+ return (self.getvalue(), self._pos, d)
+
+ # Jython: modelled after bytesio.c::bytesio_setstate
+ def __setstate__(self, state):
+
+ if not isinstance(state, tuple) or len(state) < 3 :
+ fmt = "%s.__setstate__ argument should be 3-tuple got %s"
+ raise TypeError( fmt % (type(self), type(state)) )
+
+ # Reset the object to its default state. This is only needed to handle
+ # the case of repeated calls to __setstate__. */
+ self._buffer = bytearray()
+ self._pos = 0
+
+ # Set the value of the internal buffer. If state[0] does not support the
+ # buffer protocol, bytesio_write will raise the appropriate TypeError. */
+ self.write(state[0]);
+
+ # Carefully set the position value. Alternatively, we could use the seek
+ # method instead of modifying self._pos directly to better protect the
+ # object internal state against erroneous (or malicious) inputs. */
+ p = state[1]
+ if not isinstance(p, (int, long)) :
+ fmt = "second item of state must be an integer, got %s"
+ raise TypeError( fmt % type(p) )
+ elif p < 0 :
+ raise ValueError("position value cannot be negative")
+ self._pos = p
+
+ # Set the dictionary of the instance variables. */
+ d = state[2]
+ if not d is None :
+ if isinstance(d, dict) :
+ self.__dict__ = d
+ else :
+ fmt = "third item of state should be a dict, got %s"
+ raise TypeError( fmt % type(d) )
def getvalue(self):
"""Return the bytes value (contents) of the buffer
@@ -1479,6 +1517,11 @@
"""
def __init__(self, initial_value="", newline="\n"):
+
+ # Newline mark needs to be in bytes: convert if not already so
+ if isinstance(newline, unicode) :
+ newline = newline.encode("utf-8")
+
super(StringIO, self).__init__(BytesIO(),
encoding="utf-8",
errors="strict",
@@ -1487,11 +1530,64 @@
# C version, even under Windows.
if newline is None:
self._writetranslate = False
- if initial_value:
- if not isinstance(initial_value, unicode):
- initial_value = unicode(initial_value)
- self.write(initial_value)
- self.seek(0)
+ # An initial value may have been supplied (and must be unicode)
+ if initial_value is not None:
+ if not isinstance(initial_value, unicode) :
+ fmt = "initial value should be unicode or None, got %s"
+ raise TypeError( fmt % type(initial_value) )
+ if initial_value:
+ self.write(initial_value)
+ self.seek(0)
+
+ # Jython: modelled after stringio.c::stringio_getstate
+ def __getstate__(self):
+ d = getattr(self, '__dict__', None)
+ if d is not None :
+ d = d.copy()
+ return (self.getvalue(), self._readnl, self.tell(), d)
+
+ # Jython: modelled after stringio.c:stringio_setstate
+ def __setstate__(self, state):
+ self._checkClosed()
+
+ if not isinstance(state, tuple) or len(state) < 4 :
+ fmt = "%s.__setstate__ argument should be 4-tuple got %s"
+ raise TypeError( fmt % (type(self), type(state)) )
+
+ # Initialize the object's state, but empty
+ self.__init__(None, state[1])
+
+ # Write the buffer, bypassing end-of-line translation.
+ value = state[0]
+ if value is not None:
+ if not isinstance(value, unicode) :
+ fmt = "ivalue should be unicode or None, got %s"
+ raise TypeError( fmt % type(value) )
+ encoder = self._encoder or self._get_encoder()
+ b = encoder.encode(state[0])
+ self.buffer.write(b)
+
+ # Reset the object to its default state. This is only needed to handle
+ # the case of repeated calls to __setstate__.
+ self.seek(0)
+
+ # Set the position value using seek. A long is tolerated (e.g from pickle).
+ p = state[2]
+ if not isinstance(p, (int, long)) :
+ fmt = "third item of state must be an integer, got %s"
+ raise TypeError( fmt % type(p) )
+ elif p < 0 :
+ raise ValueError("position value cannot be negative")
+ self.seek(p)
+
+ # Set the dictionary of the instance variables. */
+ d = state[3]
+ if not d is None :
+ if isinstance(d, dict) :
+ self.__dict__ = d
+ else :
+ fmt = "fourth item of state should be a dict, got %s"
+ raise TypeError( fmt % type(d) )
def getvalue(self):
self.flush()
diff --git a/Lib/test/test_memoryio.py b/Lib/test/test_memoryio.py
--- a/Lib/test/test_memoryio.py
+++ b/Lib/test/test_memoryio.py
@@ -629,11 +629,6 @@
if support.is_jython: # FIXME: Jython issue 1996
test_detach = MemoryTestMixin.test_detach
- # This test isn't working on Ubuntu on an Apple Intel powerbook,
- # Jython 2.7b1+ (default:6b4a1088566e, Feb 10 2013, 14:36:47)
- # [OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.7.0_09
- @unittest.skipIf(support.is_jython,
- "FIXME: Currently not working on jython")
def test_getstate(self):
memio = self.ioclass()
state = memio.__getstate__()
@@ -644,11 +639,6 @@
memio.close()
self.assertRaises(ValueError, memio.__getstate__)
- # This test isn't working on Ubuntu on an Apple Intel powerbook,
- # Jython 2.7b1+ (default:6b4a1088566e, Feb 10 2013, 14:36:47)
- # [OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.7.0_09
- @unittest.skipIf(support.is_jython,
- "FIXME: Currently not working on jython")
def test_setstate(self):
# This checks whether __setstate__ does proper input validation.
memio = self.ioclass()
@@ -720,11 +710,6 @@
self.assertEqual(memio.write(buf), len(buf))
self.assertEqual(memio.getvalue(), buf + buf)
- # This test isn't working on Ubuntu on an Apple Intel powerbook,
- # Jython 2.7b1+ (default:6b4a1088566e, Feb 10 2013, 14:36:47)
- # [OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.7.0_09
- @unittest.skipIf(support.is_jython,
- "FIXME: Currently not working on jython")
def test_getstate(self):
memio = self.ioclass()
state = memio.__getstate__()
@@ -736,11 +721,6 @@
memio.close()
self.assertRaises(ValueError, memio.__getstate__)
- # This test isn't working on Ubuntu on an Apple Intel powerbook,
- # Jython 2.7b1+ (default:6b4a1088566e, Feb 10 2013, 14:36:47)
- # [OpenJDK 64-Bit Server VM (Oracle Corporation)] on java1.7.0_09
- @unittest.skipIf(support.is_jython,
- "FIXME: Currently not working on jython")
def test_setstate(self):
# This checks whether __setstate__ does proper input validation.
memio = self.ioclass()
--
Repository URL: http://hg.python.org/jython
More information about the Jython-checkins
mailing list