[pypy-commit] pypy default: merge heads

bdkearns noreply at buildbot.pypy.org
Wed Apr 10 06:33:33 CEST 2013


Author: Brian Kearns <bdkearns at gmail.com>
Branch: 
Changeset: r63192:007e2e9fca39
Date: 2013-04-10 00:33 -0400
http://bitbucket.org/pypy/pypy/changeset/007e2e9fca39/

Log:	merge heads

diff --git a/pypy/interpreter/pyparser/parsestring.py b/pypy/interpreter/pyparser/parsestring.py
--- a/pypy/interpreter/pyparser/parsestring.py
+++ b/pypy/interpreter/pyparser/parsestring.py
@@ -91,9 +91,9 @@
         assert 0 <= bufp <= bufq
         substr = buf[bufp:bufq]
         if rawmode:
-            v = unicodehelper.PyUnicode_DecodeRawUnicodeEscape(space, substr)
+            v = unicodehelper.decode_raw_unicode_escape(space, substr)
         else:
-            v = unicodehelper.PyUnicode_DecodeUnicodeEscape(space, substr)
+            v = unicodehelper.decode_unicode_escape(space, substr)
         return space.wrap(v)
 
     need_encoding = (encoding is not None and
@@ -103,8 +103,8 @@
     substr = s[ps : q]
     if rawmode or '\\' not in s[ps:]:
         if need_encoding:
-            w_u = space.wrap(unicodehelper.PyUnicode_DecodeUTF8(space, substr))
-            w_v = unicodehelper.PyUnicode_AsEncodedString(space, w_u, space.wrap(encoding))
+            w_u = space.wrap(unicodehelper.decode_utf8(space, substr))
+            w_v = unicodehelper.encode(space, w_u, encoding)
             return w_v
         else:
             return space.wrap(substr)
@@ -219,8 +219,8 @@
     # while (s < end && *s != '\\') s++; */ /* inefficient for u".."
     while ps < end and ord(s[ps]) & 0x80:
         ps += 1
-    w_u = space.wrap(unicodehelper.PyUnicode_DecodeUTF8(space, s[pt:ps]))
-    w_v = unicodehelper.PyUnicode_AsEncodedString(space, w_u, space.wrap(encoding))
+    w_u = space.wrap(unicodehelper.decode_utf8(space, s[pt:ps]))
+    w_v = unicodehelper.encode(space, w_u, encoding)
     v = space.str_w(w_v)
     return v, ps
 
diff --git a/pypy/interpreter/unicodehelper.py b/pypy/interpreter/unicodehelper.py
--- a/pypy/interpreter/unicodehelper.py
+++ b/pypy/interpreter/unicodehelper.py
@@ -29,11 +29,12 @@
 
 # ____________________________________________________________
 
-def PyUnicode_AsEncodedString(space, w_data, w_encoding):
-    return interp_codecs.encode(space, w_data, w_encoding)
+def encode(space, w_data, encoding=None, errors='strict'):
+    from pypy.objspace.std.unicodetype import encode_object
+    return encode_object(space, w_data, encoding, errors)
 
 # These functions take and return unwrapped rpython strings and unicodes
-def PyUnicode_DecodeUnicodeEscape(space, string):
+def decode_unicode_escape(space, string):
     state = space.fromcache(interp_codecs.CodecState)
     unicodedata_handler = state.get_unicodedata_handler(space)
     result, consumed = runicode.str_decode_unicode_escape(
@@ -42,20 +43,20 @@
         unicodedata_handler=unicodedata_handler)
     return result
 
-def PyUnicode_DecodeRawUnicodeEscape(space, string):
+def decode_raw_unicode_escape(space, string):
     result, consumed = runicode.str_decode_raw_unicode_escape(
         string, len(string), "strict",
         final=True, errorhandler=decode_error_handler(space))
     return result
 
-def PyUnicode_DecodeUTF8(space, string):
+def decode_utf8(space, string):
     result, consumed = runicode.str_decode_utf_8(
         string, len(string), "strict",
         final=True, errorhandler=decode_error_handler(space),
         allow_surrogates=True)
     return result
 
-def PyUnicode_EncodeUTF8(space, uni):
+def encode_utf8(space, uni):
     return runicode.unicode_encode_utf_8(
         uni, len(uni), "strict",
         errorhandler=encode_error_handler(space),
diff --git a/pypy/module/_codecs/interp_codecs.py b/pypy/module/_codecs/interp_codecs.py
--- a/pypy/module/_codecs/interp_codecs.py
+++ b/pypy/module/_codecs/interp_codecs.py
@@ -1,10 +1,18 @@
+from rpython.rlib import jit
+from rpython.rlib.objectmodel import we_are_translated
+from rpython.rlib.rstring import UnicodeBuilder
+
 from pypy.interpreter.error import OperationError, operationerrfmt
 from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault
-from rpython.rlib.rstring import UnicodeBuilder
-from rpython.rlib.objectmodel import we_are_translated
+
+
+class VersionTag(object):
+    pass
 
 
 class CodecState(object):
+    _immutable_fields_ = ["version?"]
+
     def __init__(self, space):
         self.codec_search_path = []
         self.codec_search_cache = {}
@@ -14,6 +22,7 @@
         self.encode_error_handler = self.make_encode_errorhandler(space)
 
         self.unicodedata_handler = None
+        self.modified()
 
     def _make_errorhandler(self, space, decode):
         def call_errorhandler(errors, encoding, reason, input, startpos,
@@ -86,9 +95,20 @@
             self.unicodedata_handler = UnicodeData_Handler(space, w_getcode)
             return self.unicodedata_handler
 
+    def modified(self):
+        self.version = VersionTag()
+
+    def get_codec_from_cache(self, key):
+        return self._get_codec_with_version(key, self.version)
+
+    @jit.elidable
+    def _get_codec_with_version(self, key, version):
+        return self.codec_search_cache.get(key, None)
+
     def _cleanup_(self):
         assert not self.codec_search_path
 
+
 def register_codec(space, w_search_function):
     """register(search_function)
 
@@ -115,11 +135,12 @@
         "lookup_codec() should not be called during translation"
     state = space.fromcache(CodecState)
     normalized_encoding = encoding.replace(" ", "-").lower()
-    w_result = state.codec_search_cache.get(normalized_encoding, None)
+    w_result = state.get_codec_from_cache(normalized_encoding)
     if w_result is not None:
         return w_result
     return _lookup_codec_loop(space, encoding, normalized_encoding)
 
+
 def _lookup_codec_loop(space, encoding, normalized_encoding):
     state = space.fromcache(CodecState)
     if state.codec_need_encodings:
@@ -143,6 +164,7 @@
                     space.wrap("codec search functions must return 4-tuples"))
             else:
                 state.codec_search_cache[normalized_encoding] = w_result
+                state.modified()
                 return w_result
     raise operationerrfmt(
         space.w_LookupError,
diff --git a/pypy/module/pyexpat/interp_pyexpat.py b/pypy/module/pyexpat/interp_pyexpat.py
--- a/pypy/module/pyexpat/interp_pyexpat.py
+++ b/pypy/module/pyexpat/interp_pyexpat.py
@@ -475,8 +475,8 @@
 
     def w_convert(self, space, s):
         if self.returns_unicode:
-            from pypy.interpreter.unicodehelper import PyUnicode_DecodeUTF8
-            return space.wrap(PyUnicode_DecodeUTF8(space, s))
+            from pypy.interpreter.unicodehelper import decode_utf8
+            return space.wrap(decode_utf8(space, s))
         else:
             return space.wrap(s)
 
diff --git a/pypy/module/test_lib_pypy/cffi_tests/test_function.py b/pypy/module/test_lib_pypy/cffi_tests/test_function.py
--- a/pypy/module/test_lib_pypy/cffi_tests/test_function.py
+++ b/pypy/module/test_lib_pypy/cffi_tests/test_function.py
@@ -16,7 +16,7 @@
     """xxx limited to capture at most 512 bytes of output, according
     to the Posix manual."""
 
-    def __init__(self, capture_fd=1):   # stdout, by default
+    def __init__(self, capture_fd):
         self.capture_fd = capture_fd
 
     def __enter__(self):
@@ -109,80 +109,66 @@
         y = lib.TlsFree(x)
         assert y != 0
 
-    def test_puts(self):
+    def test_fputs(self):
+        if not sys.platform.startswith('linux'):
+            py.test.skip("probably no symbol 'stderr' in the lib")
         ffi = FFI(backend=self.Backend())
         ffi.cdef("""
-            int puts(const char *);
-            int fflush(void *);
+            int fputs(const char *, void *);
+            void *stderr;
         """)
         ffi.C = ffi.dlopen(None)
-        ffi.C.puts   # fetch before capturing, for easier debugging
-        with FdWriteCapture() as fd:
-            ffi.C.puts(b"hello")
-            ffi.C.puts(b"  world")
-            ffi.C.fflush(ffi.NULL)
+        ffi.C.fputs   # fetch before capturing, for easier debugging
+        with FdWriteCapture(2) as fd:
+            ffi.C.fputs(b"hello\n", ffi.C.stderr)
+            ffi.C.fputs(b"  world\n", ffi.C.stderr)
         res = fd.getvalue()
         assert res == b'hello\n  world\n'
 
-    def test_puts_without_const(self):
+    def test_fputs_without_const(self):
+        if not sys.platform.startswith('linux'):
+            py.test.skip("probably no symbol 'stderr' in the lib")
         ffi = FFI(backend=self.Backend())
         ffi.cdef("""
-            int puts(char *);
-            int fflush(void *);
+            int fputs(char *, void *);
+            void *stderr;
         """)
         ffi.C = ffi.dlopen(None)
-        ffi.C.puts   # fetch before capturing, for easier debugging
-        with FdWriteCapture() as fd:
-            ffi.C.puts(b"hello")
-            ffi.C.puts(b"  world")
-            ffi.C.fflush(ffi.NULL)
+        ffi.C.fputs   # fetch before capturing, for easier debugging
+        with FdWriteCapture(2) as fd:
+            ffi.C.fputs(b"hello\n", ffi.C.stderr)
+            ffi.C.fputs(b"  world\n", ffi.C.stderr)
         res = fd.getvalue()
         assert res == b'hello\n  world\n'
 
-    def test_fputs(self):
+    def test_vararg(self):
         if not sys.platform.startswith('linux'):
-            py.test.skip("probably no symbol 'stdout' in the lib")
+            py.test.skip("probably no symbol 'stderr' in the lib")
         ffi = FFI(backend=self.Backend())
         ffi.cdef("""
-            int fputs(const char *, void *);
-            void *stdout, *stderr;
+           int fprintf(void *, const char *format, ...);
+           void *stderr;
         """)
         ffi.C = ffi.dlopen(None)
         with FdWriteCapture(2) as fd:
-            ffi.C.fputs(b"hello from stderr\n", ffi.C.stderr)
+            ffi.C.fprintf(ffi.C.stderr, b"hello with no arguments\n")
+            ffi.C.fprintf(ffi.C.stderr,
+                          b"hello, %s!\n", ffi.new("char[]", b"world"))
+            ffi.C.fprintf(ffi.C.stderr,
+                          ffi.new("char[]", b"hello, %s!\n"),
+                          ffi.new("char[]", b"world2"))
+            ffi.C.fprintf(ffi.C.stderr,
+                          b"hello int %d long %ld long long %lld\n",
+                          ffi.cast("int", 42),
+                          ffi.cast("long", 84),
+                          ffi.cast("long long", 168))
+            ffi.C.fprintf(ffi.C.stderr, b"hello %p\n", ffi.NULL)
         res = fd.getvalue()
-        assert res == b'hello from stderr\n'
-
-    def test_vararg(self):
-        ffi = FFI(backend=self.Backend())
-        ffi.cdef("""
-           int printf(const char *format, ...);
-           int fflush(void *);
-        """)
-        ffi.C = ffi.dlopen(None)
-        with FdWriteCapture() as fd:
-            ffi.C.printf(b"hello with no arguments\n")
-            ffi.C.printf(b"hello, %s!\n", ffi.new("char[]", b"world"))
-            ffi.C.printf(ffi.new("char[]", b"hello, %s!\n"),
-                         ffi.new("char[]", b"world2"))
-            ffi.C.printf(b"hello int %d long %ld long long %lld\n",
-                         ffi.cast("int", 42),
-                         ffi.cast("long", 84),
-                         ffi.cast("long long", 168))
-            ffi.C.printf(b"hello %p\n", ffi.NULL)
-            ffi.C.fflush(ffi.NULL)
-        res = fd.getvalue()
-        if sys.platform == 'win32':
-            NIL = b"00000000"
-        elif sys.platform.startswith(('linux', 'gnu')):
-            NIL = b"(nil)"
-        else:
-            NIL = b"0x0"    # OS/X at least
         assert res == (b"hello with no arguments\n"
                        b"hello, world!\n"
                        b"hello, world2!\n"
                        b"hello int 42 long 84 long long 168\n"
-                       b"hello " + NIL + b"\n")
+                       b"hello (nil)\n")
 
     def test_must_specify_type_of_vararg(self):
         ffi = FFI(backend=self.Backend())
@@ -216,17 +202,18 @@
         res = fptr(b"Hello")
         assert res == 42
         #
+        if not sys.platform.startswith('linux'):
+            py.test.skip("probably no symbol 'stderr' in the lib")
         ffi.cdef("""
-            int puts(const char *);
-            int fflush(void *);
+            int fputs(const char *, void *);
+            void *stderr;
         """)
         ffi.C = ffi.dlopen(None)
-        fptr = ffi.cast("int(*)(const char *txt)", ffi.C.puts)
-        assert fptr == ffi.C.puts
-        assert repr(fptr).startswith("<cdata 'int(*)(char *)' 0x")
-        with FdWriteCapture() as fd:
-            fptr(b"world")
-            ffi.C.fflush(ffi.NULL)
+        fptr = ffi.cast("int(*)(const char *txt, void *)", ffi.C.fputs)
+        assert fptr == ffi.C.fputs
+        assert repr(fptr).startswith("<cdata 'int(*)(char *, void *)' 0x")
+        with FdWriteCapture(2) as fd:
+            fptr(b"world\n", ffi.C.stderr)
         res = fd.getvalue()
         assert res == b'world\n'
 
diff --git a/pypy/objspace/std/marshal_impl.py b/pypy/objspace/std/marshal_impl.py
--- a/pypy/objspace/std/marshal_impl.py
+++ b/pypy/objspace/std/marshal_impl.py
@@ -405,11 +405,11 @@
 register(TYPE_CODE, unmarshal_pycode)
 
 def marshal_w__Unicode(space, w_unicode, m):
-    s = unicodehelper.PyUnicode_EncodeUTF8(space, space.unicode_w(w_unicode))
+    s = unicodehelper.encode_utf8(space, space.unicode_w(w_unicode))
     m.atom_str(TYPE_UNICODE, s)
 
 def unmarshal_Unicode(space, u, tc):
-    return space.wrap(unicodehelper.PyUnicode_DecodeUTF8(space, u.get_str()))
+    return space.wrap(unicodehelper.decode_utf8(space, u.get_str()))
 register(TYPE_UNICODE, unmarshal_Unicode)
 
 app = gateway.applevel(r'''
diff --git a/rpython/jit/backend/arm/test/test_regalloc_mov.py b/rpython/jit/backend/arm/test/test_regalloc_mov.py
--- a/rpython/jit/backend/arm/test/test_regalloc_mov.py
+++ b/rpython/jit/backend/arm/test/test_regalloc_mov.py
@@ -174,8 +174,11 @@
         s = stack(8191)
         r6 = r(6)
         expected = [
-                    mi('gen_load_int', 'helper', s.value, cond=AL),
-                    mi('LDR_rr', r6.value, fp.value, 'helper', cond=AL)]
+                   mi('PUSH', [lr.value], cond=AL),
+                   mi('gen_load_int', lr.value, 32940, cond=AL),
+                   mi('LDR_rr', r6.value, fp.value, lr.value, cond=AL),
+                   mi('POP', [lr.value], cond=AL),
+        ]
         self.mov(s, r6, expected)
 
     def test_mov_float_imm_to_vfp_reg(self):
@@ -422,8 +425,11 @@
 
     def test_push_big_stack(self):
         s = stack(1025)
-        e = [mi('gen_load_int', 'helper', s.value, cond=AL),
-            mi('LDR_rr', ip.value, fp.value, 'helper', cond=AL),
+        e = [
+            mi('PUSH', [lr.value], cond=AL),
+            mi('gen_load_int', lr.value, s.value, cond=AL),
+            mi('LDR_rr', ip.value, fp.value, lr.value, cond=AL),
+            mi('POP', [lr.value], cond=AL),
             mi('PUSH', [ip.value], cond=AL)
             ]
         self.push(s, e)
@@ -444,9 +450,11 @@
     def test_push_large_stackfloat(self):
         sf = stack_float(100)
         e = [
-            mi('gen_load_int', 'helper', sf.value, cond=AL),
-            mi('ADD_rr', 'helper', fp.value, 'helper', cond=AL),
-            mi('VLDR', vfp_ip.value, 'helper', cond=AL),
+            mi('PUSH', [ip.value], cond=AL),
+            mi('gen_load_int', ip.value, sf.value, cond=AL),
+            mi('ADD_rr', ip.value, fp.value, ip.value, cond=AL),
+            mi('VLDR', vfp_ip.value, ip.value, cond=AL),
+            mi('POP', [ip.value], cond=AL),
             mi('VPUSH', [vfp_ip.value], cond=AL),
         ]
         self.push(sf, e)


More information about the pypy-commit mailing list