Author: Brian Kearns <[email protected]>
Branch:
Changeset: r62270:f0497ed91d44
Date: 2013-03-09 02:00 -0500
http://bitbucket.org/pypy/pypy/changeset/f0497ed91d44/
Log: make sure hg knows this is a copy of _marshal from lib_pypy
diff --git a/rpython/translator/sandbox/_marshal.py
b/rpython/translator/sandbox/_marshal.py
--- a/rpython/translator/sandbox/_marshal.py
+++ b/rpython/translator/sandbox/_marshal.py
@@ -4,9 +4,17 @@
This module contains functions that can read and write Python values in a
binary format. The format is specific to Python, but independent of machine
architecture issues (e.g., you can write a Python value to a file on a PC,
transport the file to a Sun, and read it back there). Details of the format may
change between Python versions.
"""
+# NOTE: This module is used in the Python3 interpreter, but also by
+# the "sandboxed" process. It must work for Python2 as well.
+
import types
from _codecs import utf_8_decode, utf_8_encode
+try:
+ intern
+except NameError:
+ from sys import intern
+
try: from __pypy__ import builtinify
except ImportError: builtinify = lambda f: f
@@ -50,7 +58,7 @@
if func:
break
else:
- raise ValueError, "unmarshallable object"
+ raise ValueError("unmarshallable object")
func(self, x)
def w_long64(self, x):
@@ -73,7 +81,7 @@
def dump_none(self, x):
self._write(TYPE_NONE)
- dispatch[types.NoneType] = dump_none
+ dispatch[type(None)] = dump_none
def dump_bool(self, x):
if x:
@@ -84,7 +92,7 @@
def dump_stopiter(self, x):
if x is not StopIteration:
- raise ValueError, "unmarshallable object"
+ raise ValueError("unmarshallable object")
self._write(TYPE_STOPITER)
dispatch[type(StopIteration)] = dump_stopiter
@@ -92,10 +100,11 @@
self._write(TYPE_ELLIPSIS)
try:
- dispatch[types.EllipsisType] = dump_ellipsis
+ dispatch[type(Ellipsis)] = dump_ellipsis
except NameError:
pass
+ # In Python3, this function is not used; see dump_long() below.
def dump_int(self, x):
y = x>>31
if y and y != -1:
@@ -104,7 +113,7 @@
else:
self._write(TYPE_INT)
self.w_long(x)
- dispatch[types.IntType] = dump_int
+ dispatch[int] = dump_int
def dump_long(self, x):
self._write(TYPE_LONG)
@@ -119,27 +128,32 @@
self.w_long(len(digits) * sign)
for d in digits:
self.w_short(d)
- dispatch[types.LongType] = dump_long
+ try:
+ long
+ except NameError:
+ dispatch[int] = dump_long
+ else:
+ dispatch[long] = dump_long
def dump_float(self, x):
write = self._write
write(TYPE_FLOAT)
- s = `x`
+ s = repr(x)
write(chr(len(s)))
write(s)
- dispatch[types.FloatType] = dump_float
+ dispatch[float] = dump_float
def dump_complex(self, x):
write = self._write
write(TYPE_COMPLEX)
- s = `x.real`
+ s = repr(x.real)
write(chr(len(s)))
write(s)
- s = `x.imag`
+ s = repr(x.imag)
write(chr(len(s)))
write(s)
try:
- dispatch[types.ComplexType] = dump_complex
+ dispatch[complex] = dump_complex
except NameError:
pass
@@ -149,7 +163,7 @@
self._write(TYPE_STRING)
self.w_long(len(x))
self._write(x)
- dispatch[types.StringType] = dump_string
+ dispatch[bytes] = dump_string
def dump_unicode(self, x):
self._write(TYPE_UNICODE)
@@ -157,21 +171,26 @@
s, len_s = utf_8_encode(x)
self.w_long(len_s)
self._write(s)
- dispatch[types.UnicodeType] = dump_unicode
+ try:
+ unicode
+ except NameError:
+ dispatch[str] = dump_unicode
+ else:
+ dispatch[unicode] = dump_unicode
def dump_tuple(self, x):
self._write(TYPE_TUPLE)
self.w_long(len(x))
for item in x:
self.dump(item)
- dispatch[types.TupleType] = dump_tuple
+ dispatch[tuple] = dump_tuple
def dump_list(self, x):
self._write(TYPE_LIST)
self.w_long(len(x))
for item in x:
self.dump(item)
- dispatch[types.ListType] = dump_list
+ dispatch[list] = dump_list
def dump_dict(self, x):
self._write(TYPE_DICT)
@@ -179,7 +198,7 @@
self.dump(key)
self.dump(value)
self._write(TYPE_NULL)
- dispatch[types.DictionaryType] = dump_dict
+ dispatch[dict] = dump_dict
def dump_code(self, x):
self._write(TYPE_CODE)
@@ -253,7 +272,7 @@
try:
return self.dispatch[c](self)
except KeyError:
- raise ValueError, "bad marshal code: %c (%d)" % (c, ord(c))
+ raise ValueError("bad marshal code: %c (%d)" % (c, ord(c)))
def r_short(self):
lo = ord(self._read(1))
@@ -271,7 +290,7 @@
d = ord(s[3])
x = a | (b<<8) | (c<<16) | (d<<24)
if d & 0x80 and x > 0:
- x = -((1L<<32) - x)
+ x = -((1<<32) - x)
return int(x)
else:
return x
@@ -281,14 +300,14 @@
b = ord(self._read(1))
c = ord(self._read(1))
d = ord(self._read(1))
- e = long(ord(self._read(1)))
- f = long(ord(self._read(1)))
- g = long(ord(self._read(1)))
- h = long(ord(self._read(1)))
+ e = ord(self._read(1))
+ f = ord(self._read(1))
+ g = ord(self._read(1))
+ h = ord(self._read(1))
x = a | (b<<8) | (c<<16) | (d<<24)
x = x | (e<<32) | (f<<40) | (g<<48) | (h<<56)
if h & 0x80 and x > 0:
- x = -((1L<<64) - x)
+ x = -((1<<64) - x)
return x
def load_null(self):
@@ -325,10 +344,10 @@
if size < 0:
sign = -1
size = -size
- x = 0L
+ x = 0
for i in range(size):
d = self.r_short()
- x = x | (d<<(i*15L))
+ x = x | (d<<(i*15))
return x * sign
dispatch[TYPE_LONG] = load_long
@@ -460,7 +479,7 @@
self.bufpos += 4
x = a | (b<<8) | (c<<16) | (d<<24)
if d & 0x80 and x > 0:
- x = -((1L<<32) - x)
+ x = -((1<<32) - x)
return int(x)
else:
return x
@@ -470,14 +489,14 @@
b = ord(_read1(self))
c = ord(_read1(self))
d = ord(_read1(self))
- e = long(ord(_read1(self)))
- f = long(ord(_read1(self)))
- g = long(ord(_read1(self)))
- h = long(ord(_read1(self)))
+ e = ord(_read1(self))
+ f = ord(_read1(self))
+ g = ord(_read1(self))
+ h = ord(_read1(self))
x = a | (b<<8) | (c<<16) | (d<<24)
x = x | (e<<32) | (f<<40) | (g<<48) | (h<<56)
if h & 0x80 and x > 0:
- x = -((1L<<64) - x)
+ x = -((1<<64) - x)
return x
_load_dispatch = {}
@@ -499,7 +518,7 @@
self.bufpos += 1
return _load_dispatch[c](self)
except KeyError:
- raise ValueError, "bad marshal code: %c (%d)" % (c, ord(c))
+ raise ValueError("bad marshal code: %c (%d)" % (c, ord(c)))
except IndexError:
raise EOFError
@@ -541,10 +560,10 @@
if size < 0:
sign = -1
size = -size
- x = 0L
+ x = 0
for i in range(size):
d = _r_short(self)
- x = x | (d<<(i*15L))
+ x = x | (d<<(i*15))
return x * sign
dispatch[TYPE_LONG] = load_long
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit