Author: Armin Rigo <ar...@tunes.org>
Branch: py3k
Changeset: r86710:5e30f5104630
Date: 2016-08-29 21:02 +0200
http://bitbucket.org/pypy/pypy/changeset/5e30f5104630/

Log:    hg merge default

        with suitable refactorings of 990f5b2322e1 for py3k

diff --git a/lib_pypy/_ctypes/function.py b/lib_pypy/_ctypes/function.py
--- a/lib_pypy/_ctypes/function.py
+++ b/lib_pypy/_ctypes/function.py
@@ -342,7 +342,7 @@
             thisarg = cast(thisvalue, POINTER(POINTER(c_void_p)))
             keepalives, newargs, argtypes, outargs, errcheckargs = (
                 self._convert_args(argtypes, args[1:], kwargs))
-            newargs.insert(0, thisvalue.value)
+            newargs.insert(0, thisarg)
             argtypes.insert(0, c_void_p)
         else:
             thisarg = None
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
@@ -145,6 +145,24 @@
     #state.print_refcounts()
     self.frozen_ll2callocations = set(ll2ctypes.ALLOCATED.values())
 
+class FakeSpace(object):
+    """Like TinyObjSpace, but different"""
+    def __init__(self, config):
+        from distutils.sysconfig import get_python_inc
+        self.config = config
+        self.include_dir = get_python_inc()
+
+    def passthrough(self, arg):
+        return arg
+    listview = passthrough
+    str_w = passthrough
+
+    def unwrap(self, args):
+        try:
+            return args.str_w(None)
+        except:
+            return args
+
 class LeakCheckingTest(object):
     """Base class for all cpyext tests."""
     spaceconfig = dict(usemodules=['cpyext', 'thread', '_rawffi', 'array',
@@ -440,21 +458,8 @@
         self.imported_module_names = []
 
         if self.runappdirect:
+            fake = FakeSpace(self.space.config)
             def interp2app(func):
-                from distutils.sysconfig import get_python_inc
-                class FakeSpace(object):
-                    def passthrough(self, arg):
-                        return arg
-                    listview = passthrough
-                    str_w = passthrough
-                    def unwrap(self, args):
-                        try:
-                            return args.str_w(None)
-                        except:
-                            return args
-                fake = FakeSpace()
-                fake.include_dir = get_python_inc()
-                fake.config = self.space.config
                 def run(*args, **kwargs):
                     for k in kwargs.keys():
                         if k not in func.unwrap_spec and not 
k.startswith('w_'):
diff --git a/pypy/objspace/std/bytearrayobject.py 
b/pypy/objspace/std/bytearrayobject.py
--- a/pypy/objspace/std/bytearrayobject.py
+++ b/pypy/objspace/std/bytearrayobject.py
@@ -926,7 +926,6 @@
         of the specified width.  B is never truncated.
         """
 
-
 W_BytearrayObject.typedef = TypeDef(
     "bytearray",
     __doc__ = BytearrayDocstrings.__doc__,
diff --git a/pypy/objspace/std/iterobject.py b/pypy/objspace/std/iterobject.py
--- a/pypy/objspace/std/iterobject.py
+++ b/pypy/objspace/std/iterobject.py
@@ -114,6 +114,31 @@
         return w_item
 
 
+class W_StringIterObject(W_AbstractSeqIterObject):
+    """Sequence iterator specialized for string-like objects, used
+    for bytes.__iter__() or str.__iter__() or bytearray.__iter__().
+    Needed because otherwise these methods would call the possibly
+    overridden __getitem__() method, which they must not.
+    """
+    def __init__(self, w_seq, getitem_fn):
+        W_AbstractSeqIterObject.__init__(self, w_seq)
+        self.getitem_fn = getitem_fn
+
+    def descr_next(self, space):
+        if self.w_seq is None:
+            raise OperationError(space.w_StopIteration, space.w_None)
+        index = self.index
+        try:
+            w_item = self.getitem_fn(self.w_seq, space, index)
+        except OperationError as e:
+            self.w_seq = None
+            if not e.match(space, space.w_IndexError):
+                raise
+            raise OperationError(space.w_StopIteration, space.w_None)
+        self.index = index + 1
+        return w_item
+
+
 class W_ReverseSeqIterObject(W_Root):
     def __init__(self, space, w_seq, index=-1):
         self.w_seq = w_seq
diff --git a/pypy/objspace/std/stringmethods.py 
b/pypy/objspace/std/stringmethods.py
--- a/pypy/objspace/std/stringmethods.py
+++ b/pypy/objspace/std/stringmethods.py
@@ -75,7 +75,8 @@
         return space.wrap(self._len())
 
     def descr_iter(self, space):
-        return space.newseqiter(self)
+        from pypy.objspace.std.iterobject import W_StringIterObject
+        return W_StringIterObject(self, self.__class__._getitem_result)
 
     def descr_contains(self, space, w_sub):
         value = self._val(space)
@@ -133,14 +134,15 @@
         return self._getitem_result(space, index)
 
     def _getitem_result(self, space, index):
+        # Returns the result of 'self[index]', where index is an unwrapped int.
+        # Used by descr_getitem() and by descr_iter().
         selfvalue = self._val(space)
         try:
             character = selfvalue[index]
         except IndexError:
             raise oefmt(space.w_IndexError, "string index out of range")
         from pypy.objspace.std.bytesobject import W_BytesObject
-        from pypy.objspace.std.bytearrayobject import W_BytearrayObject
-        if isinstance(self, W_BytesObject) or isinstance(self, 
W_BytearrayObject):
+        if isinstance(self, W_BytesObject):
             return space.wrap(ord(character))
         return self._new(character)
 
diff --git a/pypy/objspace/std/test/test_listobject.py 
b/pypy/objspace/std/test/test_listobject.py
--- a/pypy/objspace/std/test/test_listobject.py
+++ b/pypy/objspace/std/test/test_listobject.py
@@ -432,7 +432,7 @@
 
 
 class AppTestListObject(object):
-    spaceconfig = {"objspace.std.withliststrategies": True}  # it's the default
+    #spaceconfig = {"objspace.std.withliststrategies": True}  # it's the 
default
 
     def setup_class(cls):
         import platform
@@ -1524,6 +1524,16 @@
             def __iter__(self):
                 yield "ok"
         assert list(U("don't see me")) == ["ok"]
+        #
+        class S(bytes):
+            def __getitem__(self, index):
+                never_called
+        assert list(S(b"abc")) == list(b"abc")   # __getitem__ ignored
+        #
+        class U(str):
+            def __getitem__(self, index):
+                never_called
+        assert list(U("abc")) == list("abc")     # __getitem__ ignored
 
     def test_extend_from_nonempty_list_with_subclasses(self):
         l = ["hi!"]
@@ -1549,6 +1559,20 @@
         l.extend(U("don't see me"))
         #
         assert l == ["hi!", "okT", "okL", "okL", "okS", "okU"]
+        #
+        class S(bytes):
+            def __getitem__(self, index):
+                never_called
+        l = []
+        l.extend(S(b"abc"))
+        assert l == list(b"abc")    # __getitem__ ignored
+        #
+        class U(str):
+            def __getitem__(self, index):
+                never_called
+        l = []
+        l.extend(U("abc"))
+        assert l == list("abc")     # __getitem__ ignored
 
     def test_issue1266(self):
         l = list(range(1))
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to