[pypy-svn] r56212 - in pypy/dist/pypy: module/marshal/test objspace/std
arigo at codespeak.net
arigo at codespeak.net
Tue Jul 1 19:56:57 CEST 2008
Author: arigo
Date: Tue Jul 1 19:56:57 2008
New Revision: 56212
Modified:
pypy/dist/pypy/module/marshal/test/make_test_marshal.py
pypy/dist/pypy/module/marshal/test/test_marshal.py
pypy/dist/pypy/module/marshal/test/test_marshalimpl.py
pypy/dist/pypy/objspace/std/marshal_impl.py
Log:
Fix marshalling of buffers and objects implementing the buffer protocol,
following CPython.
Modified: pypy/dist/pypy/module/marshal/test/make_test_marshal.py
==============================================================================
--- pypy/dist/pypy/module/marshal/test/make_test_marshal.py (original)
+++ pypy/dist/pypy/module/marshal/test/make_test_marshal.py Tue Jul 1 19:56:57 2008
@@ -25,8 +25,6 @@
func.func_code
scopefunc.func_code
u'hello'
- buffer(hello)
- buffer(u'unicode, too')
set()
set([1, 2])
frozenset()
Modified: pypy/dist/pypy/module/marshal/test/test_marshal.py
==============================================================================
--- pypy/dist/pypy/module/marshal/test/test_marshal.py (original)
+++ pypy/dist/pypy/module/marshal/test/test_marshal.py Tue Jul 1 19:56:57 2008
@@ -481,44 +481,6 @@
x = marshal.load(f)
assert x == case and type(x) is type(case)
- def test_buffer_brace_hello_ecarb_(self):
- import sys
- hello = "he"
- hello += "llo"
- def func(x):
- return lambda y: x+y
- scopefunc = func(42)
- import marshal, StringIO
- case = buffer(hello)
- print "case: %-30s func=buffer_brace_hello_ecarb_" % (case, )
- s = marshal.dumps(case)
- x = marshal.loads(s)
- assert x == case and type(x) is type(case)
- f = StringIO.StringIO()
- marshal.dump(case, f)
- f.seek(0)
- x = marshal.load(f)
- assert x == case and type(x) is type(case)
-
- def test_buffer_brace_u_quote_unicode_comma__too_quote__ecarb_(self):
- import sys
- hello = "he"
- hello += "llo"
- def func(x):
- return lambda y: x+y
- scopefunc = func(42)
- import marshal, StringIO
- case = buffer(u'unicode, too')
- print "case: %-30s func=buffer_brace_u_quote_unicode_comma__too_quote__ecarb_" % (case, )
- s = marshal.dumps(case)
- x = marshal.loads(s)
- assert x == case and type(x) is type(case)
- f = StringIO.StringIO()
- marshal.dump(case, f)
- f.seek(0)
- x = marshal.load(f)
- assert x == case and type(x) is type(case)
-
def test_set_brace__ecarb_(self):
import sys
hello = "he"
Modified: pypy/dist/pypy/module/marshal/test/test_marshalimpl.py
==============================================================================
--- pypy/dist/pypy/module/marshal/test/test_marshalimpl.py (original)
+++ pypy/dist/pypy/module/marshal/test/test_marshalimpl.py Tue Jul 1 19:56:57 2008
@@ -25,3 +25,15 @@
import marshal
z = marshal.loads(buffer('i\x02\x00\x00\x00???'))
assert z == 2
+
+ def test_marshal_buffer_object(self):
+ import marshal
+ s = marshal.dumps(buffer('foobar'))
+ t = marshal.loads(s)
+ assert type(t) is str and t == 'foobar'
+
+ def test_marshal_bufferlike_object(self):
+ import marshal, array
+ s = marshal.dumps(array.array('c', 'asd'))
+ t = marshal.loads(s)
+ assert type(t) is str and t == 'asd'
Modified: pypy/dist/pypy/objspace/std/marshal_impl.py
==============================================================================
--- pypy/dist/pypy/objspace/std/marshal_impl.py (original)
+++ pypy/dist/pypy/objspace/std/marshal_impl.py Tue Jul 1 19:56:57 2008
@@ -459,25 +459,6 @@
return PyUnicode_DecodeUTF8(space, space.wrap(u.get_str()))
register(TYPE_UNICODE, unmarshal_Unicode)
-# not directly supported:
-def marshal_w_buffer(space, w_buffer, m):
- s = space.str_w(space.str(w_buffer))
- m.atom_str(TYPE_UNKNOWN, s)
-
-handled_by_any.append( ('buffer', marshal_w_buffer) )
-
-app = gateway.applevel(r'''
- def string_to_buffer(s):
- return buffer(s)
-''')
-
-string_to_buffer = app.interphook('string_to_buffer')
-
-def unmarshal_buffer(space, u, tc):
- w_s = space.wrap(u.get_str())
- return string_to_buffer(space, w_s)
-register(TYPE_UNKNOWN, unmarshal_buffer)
-
app = gateway.applevel(r'''
def set_to_list(theset):
return [item for item in theset]
@@ -528,8 +509,19 @@
w_t = space.builtin.get(name)
if space.is_true(space.issubtype(w_type, w_t)):
func(space, w_obj, m)
- break
+ return
+
+ # any unknown object implementing the buffer protocol is
+ # accepted and encoded as a plain string
+ try:
+ s = space.bufferstr_w(w_obj)
+ except OperationError, e:
+ if not e.match(space, space.w_TypeError):
+ raise
else:
- raise_exception(space, "unmarshallable object")
+ m.atom_str(TYPE_STRING, s)
+ return
+
+ raise_exception(space, "unmarshallable object")
register_all(vars())
More information about the Pypy-commit
mailing list