Author: Philip Jenvey <pjen...@underboss.org>
Branch: py3k
Changeset: r62300:0d2d672668f3
Date: 2013-03-11 15:19 -0700
http://bitbucket.org/pypy/pypy/changeset/0d2d672668f3/

Log:    merge default

diff --git a/dotviewer/graphclient.py b/dotviewer/graphclient.py
--- a/dotviewer/graphclient.py
+++ b/dotviewer/graphclient.py
@@ -128,7 +128,14 @@
 
 def spawn_local_handler():
     if hasattr(sys, 'pypy_objspaceclass'):
-        python = '/usr/bin/python'
+        # if 'python' is actually PyPy, e.g. in a virtualenv, then
+        # try hard to find a real CPython
+        try:
+            python = subprocess.check_output(
+                'env -i $SHELL -l -c "which python"', shell=True).strip()
+        except subprocess.CalledProcessError:
+            # did not work, fall back to 'python'
+            python = 'python'
     else:
         python = sys.executable
     args = [python, '-u', GRAPHSERVER, '--stdio']
diff --git a/lib-python/2/distutils/sysconfig_pypy.py 
b/lib-python/2/distutils/sysconfig_pypy.py
--- a/lib-python/2/distutils/sysconfig_pypy.py
+++ b/lib-python/2/distutils/sysconfig_pypy.py
@@ -9,6 +9,7 @@
 
 
 PREFIX = os.path.normpath(sys.prefix)
+EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
 project_base = os.path.dirname(os.path.abspath(sys.executable))
 python_build = False
 
diff --git a/lib_pypy/_sqlite3.py b/lib_pypy/_sqlite3.py
--- a/lib_pypy/_sqlite3.py
+++ b/lib_pypy/_sqlite3.py
@@ -480,8 +480,6 @@
     @_check_thread_wrap
     @_check_closed_wrap
     def __call__(self, sql):
-        if not isinstance(sql, basestring):
-            raise Warning("SQL is of wrong type. Must be string or unicode.")
         return self._statement_cache.get(sql, self.row_factory)
 
     def cursor(self, factory=None):
@@ -513,7 +511,7 @@
     def _begin(self):
         statement = c_void_p()
         ret = _lib.sqlite3_prepare_v2(self._db, self.__begin_statement, -1,
-                                        byref(statement), None)
+                                      byref(statement), None)
         try:
             if ret != _lib.SQLITE_OK:
                 raise self._get_exception(ret)
@@ -537,7 +535,7 @@
 
         statement = c_void_p()
         ret = _lib.sqlite3_prepare_v2(self._db, b"COMMIT", -1,
-                                        byref(statement), None)
+                                      byref(statement), None)
         try:
             if ret != _lib.SQLITE_OK:
                 raise self._get_exception(ret)
@@ -566,7 +564,7 @@
 
         statement = c_void_p()
         ret = _lib.sqlite3_prepare_v2(self._db, b"ROLLBACK", -1,
-                                        byref(statement), None)
+                                      byref(statement), None)
         try:
             if ret != _lib.SQLITE_OK:
                 raise self._get_exception(ret)
@@ -600,10 +598,10 @@
         if isinstance(name, unicode):
             name = name.encode('utf-8')
         ret = _lib.sqlite3_create_function(self._db, name, num_args,
-                                             _lib.SQLITE_UTF8, None,
-                                             c_closure,
-                                             cast(None, _STEP),
-                                             cast(None, _FINAL))
+                                           _lib.SQLITE_UTF8, None,
+                                           c_closure,
+                                           cast(None, _STEP),
+                                           cast(None, _FINAL))
         if ret != _lib.SQLITE_OK:
             raise self.OperationalError("Error creating function")
 
@@ -669,10 +667,10 @@
         if isinstance(name, unicode):
             name = name.encode('utf-8')
         ret = _lib.sqlite3_create_function(self._db, name, num_args,
-                                             _lib.SQLITE_UTF8, None,
-                                             cast(None, _FUNC),
-                                             c_step_callback,
-                                             c_final_callback)
+                                           _lib.SQLITE_UTF8, None,
+                                           cast(None, _FUNC),
+                                           c_step_callback,
+                                           c_final_callback)
         if ret != _lib.SQLITE_OK:
             raise self._get_exception(ret)
 
@@ -839,14 +837,14 @@
             return func(self, *args, **kwargs)
         return wrapper
 
-    @__check_cursor_wrap
-    def execute(self, sql, params=[]):
+    def __execute(self, multiple, sql, many_params):
         self.__locked = True
         try:
-            self.__description = None
             self._reset = False
             if not isinstance(sql, basestring):
                 raise ValueError("operation parameter must be str or unicode")
+            self.__description = None
+            self.__rowcount = -1
             self.__statement = self.__connection._statement_cache.get(
                 sql, self.row_factory)
 
@@ -858,83 +856,61 @@
                     if not self.__connection._in_transaction:
                         self.__connection._begin()
 
-            self.__statement._set_params(params)
+            if multiple and self.__statement._kind != Statement._DML:
+                raise ProgrammingError("executemany is only for DML 
statements")
 
-            # Actually execute the SQL statement
-            ret = _lib.sqlite3_step(self.__statement._statement)
-            if ret not in (_lib.SQLITE_DONE, _lib.SQLITE_ROW):
-                self.__statement._reset()
-                self.__connection._in_transaction = \
-                    not _lib.sqlite3_get_autocommit(self.__connection._db)
-                raise self.__connection._get_exception(ret)
+            for params in many_params:
+                self.__statement._set_params(params)
 
-            if self.__statement._kind == Statement._DML:
-                self.__statement._reset()
+                # Actually execute the SQL statement
+                ret = _lib.sqlite3_step(self.__statement._statement)
+                if ret not in (_lib.SQLITE_DONE, _lib.SQLITE_ROW):
+                    self.__statement._reset()
+                    self.__connection._in_transaction = \
+                        not _lib.sqlite3_get_autocommit(self.__connection._db)
+                    raise self.__connection._get_exception(ret)
 
-            if self.__statement._kind == Statement._DQL and ret == 
_lib.SQLITE_ROW:
-                self.__statement._build_row_cast_map()
-                self.__statement._readahead(self)
-            else:
-                self.__statement._item = None
-                self.__statement._exhausted = True
+                if self.__statement._kind == Statement._DML:
+                    self.__statement._reset()
 
-            self.__rowcount = -1
-            if self.__statement._kind == Statement._DML:
-                self.__rowcount = _lib.sqlite3_changes(self.__connection._db)
+                if self.__statement._kind == Statement._DQL and ret == 
_lib.SQLITE_ROW:
+                    self.__statement._build_row_cast_map()
+                    self.__statement._readahead(self)
+                else:
+                    self.__statement._item = None
+                    self.__statement._exhausted = True
+
+                if self.__statement._kind == Statement._DML:
+                    if self.__rowcount == -1:
+                        self.__rowcount = 0
+                    self.__rowcount += 
_lib.sqlite3_changes(self.__connection._db)
         finally:
             self.__locked = False
 
         return self
 
     @__check_cursor_wrap
+    def execute(self, sql, params=[]):
+        return self.__execute(False, sql, [params])
+
+    @__check_cursor_wrap
     def executemany(self, sql, many_params):
-        self.__locked = True
-        try:
-            self.__description = None
-            self._reset = False
-            if not isinstance(sql, basestring):
-                raise ValueError("operation parameter must be str or unicode")
-            self.__statement = self.__connection._statement_cache.get(
-                sql, self.row_factory)
-
-            if self.__statement._kind == Statement._DML:
-                if self.__connection._isolation_level is not None:
-                    if not self.__connection._in_transaction:
-                        self.__connection._begin()
-            else:
-                raise ProgrammingError(
-                    "executemany is only for DML statements")
-
-            self.__rowcount = 0
-            for params in many_params:
-                self.__statement._set_params(params)
-                ret = _lib.sqlite3_step(self.__statement._statement)
-                if ret != _lib.SQLITE_DONE:
-                    self.__statement._reset()
-                    self.__connection._in_transaction = \
-                        not _lib.sqlite3_get_autocommit(self.__connection._db)
-                    raise self.__connection._get_exception(ret)
-                self.__statement._reset()
-                self.__rowcount += _lib.sqlite3_changes(self.__connection._db)
-        finally:
-            self.__locked = False
-
-        return self
+        return self.__execute(True, sql, many_params)
 
     def executescript(self, sql):
-        self.__description = None
+        self.__check_cursor()
         self._reset = False
-        self.__check_cursor()
-        statement = c_void_p()
         if isinstance(sql, unicode):
             sql = sql.encode('utf-8')
         elif not isinstance(sql, str):
             raise ValueError("script argument must be unicode or string.")
-        c_sql = c_char_p(sql)
+        sql = c_char_p(sql)
+        statement = c_void_p()
 
         self.__connection.commit()
         while True:
-            rc = _lib.sqlite3_prepare(self.__connection._db, c_sql, -1, 
byref(statement), byref(c_sql))
+            rc = _lib.sqlite3_prepare(self.__connection._db, sql, -1,
+                                      byref(statement), byref(sql))
             if rc != _lib.SQLITE_OK:
                 raise self.__connection._get_exception(rc)
 
@@ -955,7 +931,7 @@
             if rc != _lib.SQLITE_OK:
                 raise self.__connection._get_exception(rc)
 
-            if not c_sql.value:
+            if not sql.value:
                 break
         return self
 
@@ -1029,7 +1005,7 @@
         self.__con = connection
 
         if not isinstance(sql, basestring):
-            raise ValueError("sql must be a string")
+            raise Warning("SQL is of wrong type. Must be string or unicode.")
         first_word = self._statement_kind = sql.lstrip().split(" ")[0].upper()
         if first_word in ("INSERT", "UPDATE", "DELETE", "REPLACE"):
             self._kind = Statement._DML
@@ -1042,24 +1018,26 @@
         self._exhausted = False
         self._row_factory = None
 
-        self._statement = c_void_p()
-        next_char = c_char_p()
         if isinstance(sql, unicode):
             sql = sql.encode('utf-8')
+        sql = c_char_p(sql)
+        self._statement = c_void_p()
 
-        ret = _lib.sqlite3_prepare_v2(self.__con._db, sql, -1, 
byref(self._statement), byref(next_char))
+        ret = _lib.sqlite3_prepare_v2(self.__con._db, sql, -1,
+                                      byref(self._statement), byref(sql))
         if ret == _lib.SQLITE_OK and self._statement.value is None:
-            # an empty statement, we work around that, as it's the least 
trouble
-            ret = _lib.sqlite3_prepare_v2(self.__con._db, b"select 42", -1, 
byref(self._statement), byref(next_char))
+            # an empty statement, work around that, as it's the least trouble
+            sql = c_char_p(b"select 42")
+            ret = _lib.sqlite3_prepare_v2(self.__con._db, sql, -1,
+                                          byref(self._statement), byref(sql))
             self._kind = Statement._DQL
 
         if ret != _lib.SQLITE_OK:
             raise self.__con._get_exception(ret)
         self.__con._remember_statement(self)
-        next_char = next_char.value.decode('utf-8')
-        if _check_remaining_sql(next_char):
-            raise Warning("One and only one statement required: %r" %
-                          next_char)
+        sql = sql.value.decode('utf-8')
+        if _check_remaining_sql(sql):
+            raise Warning("You can only execute one statement at a time.")
 
     def __del__(self):
         if self._statement:
@@ -1077,35 +1055,6 @@
             self._in_use = False
         self._exhausted = False
 
-    def _build_row_cast_map(self):
-        self.__row_cast_map = []
-        for i in xrange(_lib.sqlite3_column_count(self._statement)):
-            converter = None
-
-            if self.__con._detect_types & PARSE_COLNAMES:
-                colname = _lib.sqlite3_column_name(self._statement, i)
-                if colname is not None:
-                    colname = colname.decode('utf-8')
-                    type_start = -1
-                    key = None
-                    for pos in range(len(colname)):
-                        if colname[pos] == '[':
-                            type_start = pos + 1
-                        elif colname[pos] == ']' and type_start != -1:
-                            key = colname[type_start:pos]
-                            converter = converters[key.upper()]
-
-            if converter is None and self.__con._detect_types & 
PARSE_DECLTYPES:
-                decltype = _lib.sqlite3_column_decltype(self._statement, i)
-                if decltype is not None:
-                    decltype = decltype.decode('utf-8')
-                    decltype = decltype.split()[0]      # if multiple words, 
use first, eg. "INTEGER NOT NULL" => "INTEGER"
-                    if '(' in decltype:
-                        decltype = decltype[:decltype.index('(')]
-                    converter = converters.get(decltype.upper(), None)
-
-            self.__row_cast_map.append(converter)
-
     if sys.version_info[0] < 3:
         def __check_decodable(self, param):
             if self.__con.text_factory in (unicode, OptimizedUnicode,
@@ -1137,13 +1086,16 @@
             rc = _lib.sqlite3_bind_double(self._statement, idx, param)
         elif isinstance(param, unicode):
             param = param.encode("utf-8")
-            rc = _lib.sqlite3_bind_text(self._statement, idx, param, 
len(param), _lib.SQLITE_TRANSIENT)
+            rc = _lib.sqlite3_bind_text(self._statement, idx, param,
+                                        len(param), _lib.SQLITE_TRANSIENT)
         elif isinstance(param, str):
             self.__check_decodable(param)
-            rc = _lib.sqlite3_bind_text(self._statement, idx, param, 
len(param), _lib.SQLITE_TRANSIENT)
+            rc = _lib.sqlite3_bind_text(self._statement, idx, param,
+                                        len(param), _lib.SQLITE_TRANSIENT)
         elif isinstance(param, (buffer, bytes)):
             param = bytes(param)
-            rc = _lib.sqlite3_bind_blob(self._statement, idx, param, 
len(param), _lib.SQLITE_TRANSIENT)
+            rc = _lib.sqlite3_bind_blob(self._statement, idx, param,
+                                        len(param), _lib.SQLITE_TRANSIENT)
         else:
             rc = -1
         return rc
@@ -1190,6 +1142,81 @@
         else:
             raise ValueError("parameters are of unsupported type")
 
+    def _build_row_cast_map(self):
+        if not self.__con._detect_types:
+            return
+        self.__row_cast_map = []
+        for i in xrange(_lib.sqlite3_column_count(self._statement)):
+            converter = None
+
+            if self.__con._detect_types & PARSE_COLNAMES:
+                colname = _lib.sqlite3_column_name(self._statement, i)
+                if colname is not None:
+                    colname = colname.decode('utf-8')
+                    type_start = -1
+                    key = None
+                    for pos in range(len(colname)):
+                        if colname[pos] == '[':
+                            type_start = pos + 1
+                        elif colname[pos] == ']' and type_start != -1:
+                            key = colname[type_start:pos]
+                            converter = converters[key.upper()]
+
+            if converter is None and self.__con._detect_types & 
PARSE_DECLTYPES:
+                decltype = _lib.sqlite3_column_decltype(self._statement, i)
+                if decltype is not None:
+                    decltype = decltype.decode('utf-8')
+                    # if multiple words, use first, eg.
+                    # "INTEGER NOT NULL" => "INTEGER"
+                    decltype = decltype.split()[0]
+                    if '(' in decltype:
+                        decltype = decltype[:decltype.index('(')]
+                    converter = converters.get(decltype.upper(), None)
+
+            self.__row_cast_map.append(converter)
+
+    def _readahead(self, cursor):
+        row = []
+        num_cols = _lib.sqlite3_column_count(self._statement)
+        for i in xrange(num_cols):
+            if self.__con._detect_types:
+                converter = self.__row_cast_map[i]
+            else:
+                converter = None
+
+            if converter is not None:
+                blob = _lib.sqlite3_column_blob(self._statement, i)
+                if not blob:
+                    val = None
+                else:
+                    blob_len = _lib.sqlite3_column_bytes(self._statement, i)
+                    val = bytes(string_at(blob, blob_len))
+                    val = converter(val)
+            else:
+                typ = _lib.sqlite3_column_type(self._statement, i)
+                if typ == _lib.SQLITE_NULL:
+                    val = None
+                elif typ == _lib.SQLITE_INTEGER:
+                    val = _lib.sqlite3_column_int64(self._statement, i)
+                    val = int(val)
+                elif typ == _lib.SQLITE_FLOAT:
+                    val = _lib.sqlite3_column_double(self._statement, i)
+                elif typ == _lib.SQLITE_TEXT:
+                    text = _lib.sqlite3_column_text(self._statement, i)
+                    text_len = _lib.sqlite3_column_bytes(self._statement, i)
+                    val = string_at(text, text_len)
+                    val = self.__con.text_factory(val)
+                elif typ == _lib.SQLITE_BLOB:
+                    blob = _lib.sqlite3_column_blob(self._statement, i)
+                    blob_len = _lib.sqlite3_column_bytes(self._statement, i)
+                    val = _BLOB_TYPE(string_at(blob, blob_len))
+            row.append(val)
+
+        row = tuple(row)
+        if self._row_factory is not None:
+            row = self._row_factory(cursor, row)
+        self._item = row
+
     def _next(self, cursor):
         if self._exhausted:
             raise StopIteration
@@ -1207,45 +1234,6 @@
         self._readahead(cursor)
         return item
 
-    def _readahead(self, cursor):
-        self.column_count = _lib.sqlite3_column_count(self._statement)
-        row = []
-        for i in xrange(self.column_count):
-            typ = _lib.sqlite3_column_type(self._statement, i)
-
-            converter = self.__row_cast_map[i]
-            if converter is None:
-                if typ == _lib.SQLITE_NULL:
-                    val = None
-                elif typ == _lib.SQLITE_INTEGER:
-                    val = _lib.sqlite3_column_int64(self._statement, i)
-                    val = int(val)
-                elif typ == _lib.SQLITE_FLOAT:
-                    val = _lib.sqlite3_column_double(self._statement, i)
-                elif typ == _lib.SQLITE_TEXT:
-                    text = _lib.sqlite3_column_text(self._statement, i)
-                    text_len = _lib.sqlite3_column_bytes(self._statement, i)
-                    val = string_at(text, text_len)
-                    val = self.__con.text_factory(val)
-                elif typ == _lib.SQLITE_BLOB:
-                    blob = _lib.sqlite3_column_blob(self._statement, i)
-                    blob_len = _lib.sqlite3_column_bytes(self._statement, i)
-                    val = _BLOB_TYPE(string_at(blob, blob_len))
-            else:
-                blob = _lib.sqlite3_column_blob(self._statement, i)
-                if not blob:
-                    val = None
-                else:
-                    blob_len = _lib.sqlite3_column_bytes(self._statement, i)
-                    val = bytes(string_at(blob, blob_len))
-                    val = converter(val)
-            row.append(val)
-
-        row = tuple(row)
-        if self._row_factory is not None:
-            row = self._row_factory(cursor, row)
-        self._item = row
-
     def _get_description(self):
         if self._kind == Statement._DML:
             return None
diff --git a/pypy/interpreter/astcompiler/assemble.py 
b/pypy/interpreter/astcompiler/assemble.py
--- a/pypy/interpreter/astcompiler/assemble.py
+++ b/pypy/interpreter/astcompiler/assemble.py
@@ -425,7 +425,8 @@
                 if instr.lineno:
                     # compute deltas
                     line = instr.lineno - current_line
-                    assert line >= 0
+                    if line < 0:
+                        continue
                     addr = offset - current_off
                     # Python assumes that lineno always increases with
                     # increasing bytecode address (lnotab is unsigned char).
diff --git a/pypy/module/_ast/test/test_ast.py 
b/pypy/module/_ast/test/test_ast.py
--- a/pypy/module/_ast/test/test_ast.py
+++ b/pypy/module/_ast/test/test_ast.py
@@ -306,3 +306,18 @@
         ast.fix_missing_locations(m)
         exc = raises(TypeError, compile, m, "<test>", "exec")
 
+    def test_hacked_lineno(self):
+        import _ast
+        stmt = '''if 1:
+            try:
+                foo
+            except Exception as error:
+                bar
+            except Baz as error:
+                bar
+            '''
+        mod = compile(stmt, "<test>", "exec", _ast.PyCF_ONLY_AST)
+        # These lineno are invalid, but should not crash the interpreter.
+        mod.body[0].body[0].handlers[0].lineno = 7
+        mod.body[0].body[0].handlers[1].lineno = 6
+        code = compile(mod, "<test>", "exec")
diff --git a/pypy/module/cpyext/test/test_cpyext.py 
b/pypy/module/cpyext/test/test_cpyext.py
--- a/pypy/module/cpyext/test/test_cpyext.py
+++ b/pypy/module/cpyext/test/test_cpyext.py
@@ -85,6 +85,8 @@
     """Base class for all cpyext tests."""
     spaceconfig = dict(usemodules=['cpyext', 'thread', '_rawffi', 'array',
                                    'itertools', 'rctime', 'binascii'])
+    spaceconfig['std.withmethodcache'] = True
+
     enable_leak_checking = True
 
     @staticmethod
diff --git a/pypy/module/cpyext/test/test_typeobject.py 
b/pypy/module/cpyext/test/test_typeobject.py
--- a/pypy/module/cpyext/test/test_typeobject.py
+++ b/pypy/module/cpyext/test/test_typeobject.py
@@ -285,7 +285,38 @@
         class C(object):
             pass
         assert module.name_by_heaptype(C) == "C"
-        
+
+    def test_type_dict(self):
+        foo = self.import_module("foo")
+        module = self.import_extension('test', [
+           ("hack_tp_dict", "METH_O",
+            '''
+                 PyTypeObject *type = args->ob_type;
+                 PyObject *a1 = PyLong_FromLong(1);
+                 PyObject *a2 = PyLong_FromLong(2);
+                 PyObject *value;
+
+                 if (PyDict_SetItemString(type->tp_dict, "a",
+                         a1) < 0)
+                     return NULL;
+                 Py_DECREF(a1);
+                 PyType_Modified(type);
+                 value = PyObject_GetAttrString(type, "a");
+                 Py_DECREF(value);
+
+                 if (PyDict_SetItemString(type->tp_dict, "a",
+                         a2) < 0)
+                     return NULL;
+                 Py_DECREF(a2);
+                 PyType_Modified(type);
+                 value = PyObject_GetAttrString(type, "a");
+                 return value;
+             '''
+             )
+            ])
+        obj = foo.new()
+        assert module.hack_tp_dict(obj) == 2
+
 
 class TestTypes(BaseApiTest):
     def test_type_attributes(self, space, api):
@@ -323,7 +354,7 @@
         w_obj = api._PyType_Lookup(w_type, space.wrap("__invalid"))
         assert w_obj is None
         assert api.PyErr_Occurred() is None
-    
+
 class AppTestSlots(AppTestCpythonExtensionBase):
     def test_some_slots(self):
         module = self.import_extension('foo', [
diff --git a/pypy/module/cpyext/typeobject.py b/pypy/module/cpyext/typeobject.py
--- a/pypy/module/cpyext/typeobject.py
+++ b/pypy/module/cpyext/typeobject.py
@@ -674,6 +674,9 @@
     subtypes.  This function must be called after any manual
     modification of the attributes or base classes of the type.
     """
-    # PyPy already takes care of direct modifications to type.__dict__
-    # (which is a W_DictProxyObject).
-    pass
+    # Invalidate the type cache in case of a builtin type.
+    if not isinstance(w_obj, W_TypeObject):
+        return
+    if w_obj.is_cpytype():
+        w_obj.mutated(None)
+    
diff --git a/pypy/objspace/std/complexobject.py 
b/pypy/objspace/std/complexobject.py
--- a/pypy/objspace/std/complexobject.py
+++ b/pypy/objspace/std/complexobject.py
@@ -9,7 +9,7 @@
 from rpython.rlib.rfloat import (
     formatd, DTSF_STR_PRECISION, isinf, isnan, copysign)
 from rpython.rlib import jit, rcomplex
-from rpython.rlib.rarithmetic import intmask
+from rpython.rlib.rarithmetic import intmask, r_ulonglong
 
 import math
 
@@ -43,7 +43,7 @@
         real = space.float_w(space.getattr(self, space.wrap("real")))
         imag = space.float_w(space.getattr(self, space.wrap("imag")))
         real_b = rbigint.fromrarith_int(float2longlong(real))
-        imag_b = rbigint.fromrarith_int(float2longlong(imag))
+        imag_b = rbigint.fromrarith_int(r_ulonglong(float2longlong(imag)))
         val = real_b.lshift(64).or_(imag_b).lshift(3).or_(rbigint.fromint(tag))
         return space.newlong_from_rbigint(val)
 
diff --git a/pypy/objspace/std/dictproxyobject.py 
b/pypy/objspace/std/dictproxyobject.py
--- a/pypy/objspace/std/dictproxyobject.py
+++ b/pypy/objspace/std/dictproxyobject.py
@@ -44,10 +44,11 @@
                 raise
             if not w_type.is_cpytype():
                 raise
-            # xxx obscure workaround: allow cpyext to write to type->tp_dict
-            # xxx even in the case of a builtin type.
-            # xxx like CPython, we assume that this is only done early after
-            # xxx the type is created, and we don't invalidate any cache.
+            # Allow cpyext to write to type->tp_dict even in the case
+            # of a builtin type.
+            # Like CPython, we assume that this is only done early
+            # after the type is created, and we don't invalidate any
+            # cache.  User code shoud call PyType_Modified().
             w_type.dict_w[key] = w_value
 
     def setdefault(self, w_dict, w_key, w_default):
@@ -71,7 +72,7 @@
     def length(self, w_dict):
         return len(self.unerase(w_dict.dstorage).dict_w)
 
-    def keys(self, w_dict):
+    def w_keys(self, w_dict):
         space = self.space
         return space.newlist_str(self.unerase(w_dict.dstorage).dict_w.keys())
 
diff --git a/pypy/objspace/std/test/test_obj.py 
b/pypy/objspace/std/test/test_obj.py
--- a/pypy/objspace/std/test/test_obj.py
+++ b/pypy/objspace/std/test/test_obj.py
@@ -192,7 +192,9 @@
             l.append(i + sys.maxsize)
             l.append(i - sys.maxsize)
             l.append(i + 1j)
+            l.append(i - 1j)
             l.append(1 + i * 1j)
+            l.append(1 - i * 1j)
             s = str(i)
             l.append(s)
             u = bytes(s, 'ascii')
diff --git a/pypy/objspace/std/typeobject.py b/pypy/objspace/std/typeobject.py
--- a/pypy/objspace/std/typeobject.py
+++ b/pypy/objspace/std/typeobject.py
@@ -161,7 +161,7 @@
         generic mutation.
         """
         space = w_self.space
-        assert w_self.is_heaptype()
+        assert w_self.is_heaptype() or w_self.is_cpytype()
         if (not space.config.objspace.std.withtypeversion and
             not space.config.objspace.std.getattributeshortcut and
             not space.config.objspace.std.withidentitydict and
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
pypy-commit@python.org
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to