Author: Ronan Lamy <[email protected]>
Branch: unicode-utf8-py3
Changeset: r95593:c56cd272e570
Date: 2019-01-09 18:25 +0000
http://bitbucket.org/pypy/pypy/changeset/c56cd272e570/

Log:    hg merge py3.5

diff --git a/pypy/module/_sre/interp_sre.py b/pypy/module/_sre/interp_sre.py
--- a/pypy/module/_sre/interp_sre.py
+++ b/pypy/module/_sre/interp_sre.py
@@ -152,16 +152,19 @@
             return False
         return space.isinstance_w(self.w_pattern, space.w_unicode)
 
-    def getstring(self, w_string):
-        """Accepts a string-like object (str, bytes, bytearray, buffer...)
-        and returns a tuple (len, rpython_unicode, rpython_str, rpython_buf),
-        where only one of the rpython_xxx is non-None.
-        """
-        unicodestr = None
-        string = None
-        buf = None
+    def make_ctx(self, w_string, pos=0, endpos=sys.maxint):
+        """Make a StrMatchContext, BufMatchContext or a UnicodeMatchContext for
+        searching in the given w_string object."""
         space = self.space
+        if pos < 0:
+            pos = 0
+        if endpos < pos:
+            endpos = pos
         if space.isinstance_w(w_string, space.w_unicode):
+            if self.is_known_bytes():
+                raise oefmt(space.w_TypeError,
+                            "can't use a bytes pattern on a string-like "
+                            "object")
             unicodestr = space.realunicode_w(w_string)
             length = len(unicodestr)
         elif space.isinstance_w(w_string, space.w_bytes):
@@ -169,43 +172,13 @@
             length = len(string)
         else:
             buf = space.readbuf_w(w_string)
-            length = buf.getlength()
-            assert length >= 0
-        return (length, unicodestr, string, buf)
-
-    def make_ctx(self, w_string, pos=0, endpos=sys.maxint, flags=0):
-        """Make a StrMatchContext, BufMatchContext or a UnicodeMatchContext for
-        searching in the given w_string object."""
-        space = self.space
-        length, unicodestr, string, buf = self.getstring(w_string)
-        if pos < 0:
-            pos = 0
-        elif pos > length:
-            pos = length
-        if endpos < pos:
-            endpos = pos
-        elif endpos > length:
-            endpos = length
-        flags = self.flags | flags
-        #
-        if unicodestr is not None:
-            if self.is_known_bytes():
-                raise oefmt(space.w_TypeError,
-                            "can't use a bytes pattern on a string-like "
-                            "object")
-            return rsre_core.UnicodeMatchContext(unicodestr,
-                                                 pos, endpos, flags)
-        else:
-            if self.is_known_unicode():
-                raise oefmt(space.w_TypeError,
-                            "can't use a string pattern on a bytes-like "
-                            "object")
-            if string is not None:
-                return rsre_core.StrMatchContext(string,
-                                                 pos, endpos, flags)
-            else:
-                return rsre_core.BufMatchContext(buf,
-                                                 pos, endpos, flags)
+            size = buf.getlength()
+            assert size >= 0
+            if pos > size:
+                pos = size
+            if endpos > size:
+                endpos = size
+            return rsre_core.BufMatchContext(buf, pos, endpos, self.flags)
 
     def getmatch(self, ctx, found):
         if found:
@@ -318,20 +291,23 @@
         # w_string are both string or both unicode objects, and if w_ptemplate
         # is a literal
         use_builder = False
+        is_buffer = False
         filter_as_unicode = filter_as_string = None
         if space.is_true(space.callable(w_ptemplate)):
             w_filter = w_ptemplate
             filter_is_callable = True
         else:
-            length, filter_as_unicode, filter_as_string, buf = (
-                self.getstring(w_ptemplate))
-            if filter_as_unicode is not None:
+            if space.isinstance_w(w_ptemplate, space.w_unicode):
+                filter_as_unicode = space.realunicode_w(w_ptemplate)
                 literal = u'\\' not in filter_as_unicode
                 use_builder = (
                     space.isinstance_w(w_string, space.w_unicode) and literal)
             else:
-                if buf is not None:
-                    filter_as_string = buf.as_str()
+                if space.isinstance_w(w_ptemplate, space.w_bytes):
+                    filter_as_string = space.bytes_w(w_ptemplate)
+                else:
+                    filter_as_string = space.readbuf_w(w_ptemplate).as_str()
+                    is_buffer = True
                 literal = '\\' not in filter_as_string
                 use_builder = (
                     space.isinstance_w(w_string, space.w_bytes) and literal)
@@ -342,7 +318,7 @@
                 # not a literal; hand it over to the template compiler
                 # FIX for a CPython 3.5 bug: if w_ptemplate is a buffer
                 # (e.g. a bytearray), convert it to a byte string here.
-                if buf is not None:
+                if is_buffer:
                     w_ptemplate = space.newbytes(filter_as_string)
                 w_re = import_re(space)
                 w_filter = space.call_method(w_re, '_subx',
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to