[pypy-commit] pypy space-newtext: add a way to express "bytes" unwrapping via unwrap_spec

cfbolz pypy.commits at gmail.com
Tue Dec 6 06:42:50 EST 2016


Author: Carl Friedrich Bolz <cfbolz at gmx.de>
Branch: space-newtext
Changeset: r88899:4a061011e8e3
Date: 2016-11-10 17:38 +0100
http://bitbucket.org/pypy/pypy/changeset/4a061011e8e3/

Log:	add a way to express "bytes" unwrapping via unwrap_spec

	(graft of 8d2718505c72)

diff --git a/pypy/interpreter/gateway.py b/pypy/interpreter/gateway.py
--- a/pypy/interpreter/gateway.py
+++ b/pypy/interpreter/gateway.py
@@ -148,6 +148,9 @@
     def visit_str0(self, el, app_sig):
         self.checked_space_method(el, app_sig)
 
+    def visit_bytes(self, el, app_sig):
+        self.checked_space_method(el, app_sig)
+
     def visit_nonnegint(self, el, app_sig):
         self.checked_space_method(el, app_sig)
 
@@ -277,6 +280,9 @@
     def visit_str0(self, typ):
         self.run_args.append("space.str0_w(%s)" % (self.scopenext(),))
 
+    def visit_bytes(self, typ):
+        self.run_args.append("space.bytes_w(%s)" % (self.scopenext(),))
+
     def visit_nonnegint(self, typ):
         self.run_args.append("space.gateway_nonnegint_w(%s)" % (
             self.scopenext(),))
@@ -427,6 +433,9 @@
     def visit_str0(self, typ):
         self.unwrap.append("space.str0_w(%s)" % (self.nextarg(),))
 
+    def visit_bytes(self, typ):
+        self.unwrap.append("space.bytes_w(%s)" % (self.nextarg(),))
+
     def visit_nonnegint(self, typ):
         self.unwrap.append("space.gateway_nonnegint_w(%s)" % (self.nextarg(),))
 
diff --git a/pypy/interpreter/test/test_gateway.py b/pypy/interpreter/test/test_gateway.py
--- a/pypy/interpreter/test/test_gateway.py
+++ b/pypy/interpreter/test/test_gateway.py
@@ -249,6 +249,20 @@
         assert self.space.eq_w(space.call_function(w_app_g, space.wrap(True)),
                                space.wrap(True))
 
+    def test_interp2app_unwrap_spec_bytes(self):
+        # we can't use the "bytes" object for the unwrap_spec, because that's
+        # an alias for "str" on the underlying Python2
+        space = self.space
+        w = space.wrap
+        def g(space, b):
+            return space.newbytes(b)
+        app_g = gateway.interp2app(g, unwrap_spec=[gateway.ObjSpace, 'bytes'])
+        app_g2 = gateway.interp2app(g, unwrap_spec=[gateway.ObjSpace, 'bytes'])
+        assert app_g is app_g2
+        w_app_g = space.wrap(app_g)
+        assert self.space.eq_w(space.call_function(w_app_g, space.newbytes("abc")),
+                               space.newbytes("abc"))
+
     def test_caching_methods(self):
         class Base(gateway.W_Root):
             def f(self):


More information about the pypy-commit mailing list