[pypy-commit] pypy py3k: partially backout 9831468e1882: readapt to py3

pjenvey noreply at buildbot.pypy.org
Fri Sep 12 00:15:21 CEST 2014


Author: Philip Jenvey <pjenvey at underboss.org>
Branch: py3k
Changeset: r73487:e577a2f0be5d
Date: 2014-09-11 15:07 -0700
http://bitbucket.org/pypy/pypy/changeset/e577a2f0be5d/

Log:	partially backout 9831468e1882: readapt to py3

diff --git a/pypy/module/operator/test/test_tscmp.py b/pypy/module/operator/test/test_tscmp.py
--- a/pypy/module/operator/test/test_tscmp.py
+++ b/pypy/module/operator/test/test_tscmp.py
@@ -1,28 +1,14 @@
-from pypy.module.operator.tscmp import pypy_tscmp, pypy_tscmp_wide
+from pypy.module.operator.tscmp import pypy_tscmp
 
 class TestTimingSafeCompare:
-    tostr = str
-    tscmp = staticmethod(pypy_tscmp)
-
     def test_tscmp_neq(self):
-        assert not self.tscmp(self.tostr('asd'), self.tostr('qwe'), 3, 3)
+        assert not pypy_tscmp('asd', 'qwe', 3, 3)
 
     def test_tscmp_eq(self):
-        assert self.tscmp(self.tostr('asd'), self.tostr('asd'), 3, 3)
+        assert pypy_tscmp('asd', 'asd', 3, 3)
 
     def test_tscmp_len(self):
-        assert self.tscmp(self.tostr('asdp'), self.tostr('asdq'), 3, 3)
+        assert pypy_tscmp('asdp', 'asdq', 3, 3)
 
     def test_tscmp_nlen(self):
-        assert not self.tscmp(self.tostr('asd'), self.tostr('asd'), 2, 3)
-
-
-class TestTimingSafeCompareWide(TestTimingSafeCompare):
-    tostr = unicode
-    tscmp = staticmethod(pypy_tscmp_wide)
-
-    def test_tscmp_wide_nonascii(self):
-        a, b = u"\ud808\udf45", u"\ud808\udf45"
-        assert self.tscmp(a, b, len(a), len(b))
-        a, b = u"\ud808\udf45", u"\ud808\udf45 "
-        assert not self.tscmp(a, b, len(a), len(b))
+        assert not pypy_tscmp('asd', 'asd', 2, 3)
diff --git a/pypy/module/operator/tscmp.c b/pypy/module/operator/tscmp.c
--- a/pypy/module/operator/tscmp.c
+++ b/pypy/module/operator/tscmp.c
@@ -2,7 +2,6 @@
  */
 
 #include <stdlib.h>
-#include <wchar.h>
 #include "tscmp.h"
 
 int
@@ -41,40 +40,3 @@
 
     return (result == 0);
 }
-
-int
-pypy_tscmp_wide(const wchar_t *a, const wchar_t *b, long len_a, long len_b)
-{
-    /* The volatile type declarations make sure that the compiler has no
-     * chance to optimize and fold the code in any way that may change
-     * the timing.
-     */
-    volatile long length;
-    volatile const wchar_t *left;
-    volatile const wchar_t *right;
-    long i;
-    wchar_t result;
-
-    /* loop count depends on length of b */
-    length = len_b;
-    left = NULL;
-    right = b;
-
-    /* don't use else here to keep the amount of CPU instructions constant,
-     * volatile forces re-evaluation
-     *  */
-    if (len_a == length) {
-        left = *((volatile const wchar_t**)&a);
-        result = 0;
-    }
-    if (len_a != length) {
-        left = b;
-        result = 1;
-    }
-
-    for (i=0; i < length; i++) {
-        result |= *left++ ^ *right++;
-    }
-
-    return (result == 0);
-}
diff --git a/pypy/module/operator/tscmp.h b/pypy/module/operator/tscmp.h
--- a/pypy/module/operator/tscmp.h
+++ b/pypy/module/operator/tscmp.h
@@ -1,2 +1,1 @@
 int pypy_tscmp(const char *, const char *, long, long);
-int pypy_tscmp_wide(const wchar_t *, const wchar_t *, long, long);
diff --git a/pypy/module/operator/tscmp.py b/pypy/module/operator/tscmp.py
--- a/pypy/module/operator/tscmp.py
+++ b/pypy/module/operator/tscmp.py
@@ -7,14 +7,14 @@
 from rpython.rtyper.lltypesystem import lltype, rffi
 from rpython.translator.tool.cbuild import ExternalCompilationInfo
 
-from pypy.interpreter.error import oefmt
+from pypy.interpreter.error import OperationError, oefmt
 
 cwd = py.path.local(__file__).dirpath()
 eci = ExternalCompilationInfo(
     includes=[cwd.join('tscmp.h')],
     include_dirs=[str(cwd)],
     separate_module_files=[cwd.join('tscmp.c')],
-    export_symbols=['pypy_tscmp', 'pypy_tscmp_wide'])
+    export_symbols=['pypy_tscmp'])
 
 
 def llexternal(*args, **kwargs):
@@ -27,10 +27,6 @@
     'pypy_tscmp',
     [rffi.CCHARP, rffi.CCHARP, rffi.LONG, rffi.LONG],
     rffi.INT)
-pypy_tscmp_wide = llexternal(
-    'pypy_tscmp_wide',
-    [rffi.CWCHARP, rffi.CWCHARP, rffi.LONG, rffi.LONG],
-    rffi.INT)
 
 
 def compare_digest(space, w_a, w_b):
@@ -47,26 +43,21 @@
     """
     if (space.isinstance_w(w_a, space.w_unicode) and
         space.isinstance_w(w_b, space.w_unicode)):
-        a = space.unicode_w(w_a)
-        b = space.unicode_w(w_b)
-        with rffi.scoped_nonmoving_unicodebuffer(a) as a_buf:
-            with rffi.scoped_nonmoving_unicodebuffer(b) as b_buf:
-                result = pypy_tscmp_wide(a_buf, b_buf, len(a), len(b))
-        return space.wrap(rffi.cast(lltype.Bool, result))
+        try:
+            w_a = space.call_method(w_a, 'encode', space.wrap('ascii'))
+            w_b = space.call_method(w_b, 'encode', space.wrap('ascii'))
+        except OperationError as e:
+            if not e.match(space, space.w_UnicodeEncodeError):
+                raise
+            raise oefmt(space.w_TypeError,
+                        "comparing strings with non-ASCII characters is not "
+                        "supported")
     return compare_digest_buffer(space, w_a, w_b)
 
 
 def compare_digest_buffer(space, w_a, w_b):
-    try:
-        a_buf = w_a.buffer_w(space, space.BUF_SIMPLE)
-        b_buf = w_b.buffer_w(space, space.BUF_SIMPLE)
-    except TypeError:
-        raise oefmt(space.w_TypeError,
-                    "unsupported operand types(s) or combination of types: "
-                    "'%T' and '%T'", w_a, w_b)
-
-    a = a_buf.as_str()
-    b = b_buf.as_str()
+    a = space.bufferstr_w(w_a)
+    b = space.bufferstr_w(w_b)
     with rffi.scoped_nonmovingbuffer(a) as a_buf:
         with rffi.scoped_nonmovingbuffer(b) as b_buf:
             result = pypy_tscmp(a_buf, b_buf, len(a), len(b))


More information about the pypy-commit mailing list