Commits

Brian Kearns committed f0497ed

make sure hg knows this is a copy of _marshal from lib_pypy

  • Participants
  • Parent commits 9d25b20

Comments (0)

Files changed (1)

File rpython/translator/sandbox/_marshal.py

 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
 
                 if func:
                     break
             else:
-                raise ValueError, "unmarshallable object"
+                raise ValueError("unmarshallable object")
             func(self, x)
 
     def w_long64(self, x):
 
     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:
 
     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
 
         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:
         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)
         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
 
         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)
         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)
             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)
         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))
         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
         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):
         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
 
     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
     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 = {}
             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
 
         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