[pypy-commit] pypy default: Add space.text0_w() and space.bytes0_w(). Start using it

arigo pypy.commits at gmail.com
Tue Feb 21 08:14:18 EST 2017


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r90263:79bd45d55155
Date: 2017-02-21 14:12 +0100
http://bitbucket.org/pypy/pypy/changeset/79bd45d55155/

Log:	Add space.text0_w() and space.bytes0_w(). Start using it in the imp
	and posix modules.

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -1622,9 +1622,12 @@
         """For tests only."""
         return self.bytes_w(w_obj)
 
+    #@not_rpython    BACKCOMPAT
+    def str0_w(self, w_obj):
+        return self.bytes0_w(w_obj)
 
-    def str0_w(self, w_obj):
-        "Like str_w, but rejects strings with NUL bytes."
+    def bytes0_w(self, w_obj):
+        "Like bytes_w, but rejects strings with NUL bytes."
         from rpython.rlib import rstring
         result = w_obj.str_w(self)
         if '\x00' in result:
@@ -1632,6 +1635,10 @@
                         "argument must be a string without NUL characters")
         return rstring.assert_str0(result)
 
+    def text0_w(self, w_obj):
+        "Like text_w, but rejects strings with NUL bytes."
+        return self.bytes0_w(w_obj)
+
     def int_w(self, w_obj, allow_conversion=True):
         """
         Unwrap an app-level int object into an interpret-level int.
diff --git a/pypy/interpreter/gateway.py b/pypy/interpreter/gateway.py
--- a/pypy/interpreter/gateway.py
+++ b/pypy/interpreter/gateway.py
@@ -154,9 +154,15 @@
     def visit_bytes(self, el, app_sig):
         self.checked_space_method(el, app_sig)
 
+    def visit_bytes0(self, el, app_sig):
+        self.checked_space_method(el, app_sig)
+
     def visit_text(self, el, app_sig):
         self.checked_space_method(el, app_sig)
 
+    def visit_text0(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)
 
@@ -289,9 +295,15 @@
     def visit_bytes(self, typ):
         self.run_args.append("space.bytes_w(%s)" % (self.scopenext(),))
 
+    def visit_bytes0(self, typ):
+        self.run_args.append("space.bytes0_w(%s)" % (self.scopenext(),))
+
     def visit_text(self, typ):
         self.run_args.append("space.text_w(%s)" % (self.scopenext(),))
 
+    def visit_text0(self, typ):
+        self.run_args.append("space.text0_w(%s)" % (self.scopenext(),))
+
     def visit_nonnegint(self, typ):
         self.run_args.append("space.gateway_nonnegint_w(%s)" % (
             self.scopenext(),))
@@ -445,9 +457,15 @@
     def visit_bytes(self, typ):
         self.unwrap.append("space.bytes_w(%s)" % (self.nextarg(),))
 
+    def visit_bytes0(self, typ):
+        self.unwrap.append("space.bytes0_w(%s)" % (self.nextarg(),))
+
     def visit_text(self, typ):
         self.unwrap.append("space.text_w(%s)" % (self.nextarg(),))
 
+    def visit_text0(self, typ):
+        self.unwrap.append("space.text0_w(%s)" % (self.nextarg(),))
+
     def visit_nonnegint(self, typ):
         self.unwrap.append("space.gateway_nonnegint_w(%s)" % (self.nextarg(),))
 
diff --git a/pypy/module/imp/importing.py b/pypy/module/imp/importing.py
--- a/pypy/module/imp/importing.py
+++ b/pypy/module/imp/importing.py
@@ -172,7 +172,7 @@
     ctxt_package = None
     if ctxt_w_package is not None and ctxt_w_package is not space.w_None:
         try:
-            ctxt_package = space.str0_w(ctxt_w_package)
+            ctxt_package = space.text0_w(ctxt_w_package)
         except OperationError as e:
             if not e.match(space, space.w_TypeError):
                 raise
@@ -220,7 +220,7 @@
         ctxt_name = None
         if ctxt_w_name is not None:
             try:
-                ctxt_name = space.str0_w(ctxt_w_name)
+                ctxt_name = space.text0_w(ctxt_w_name)
             except OperationError as e:
                 if not e.match(space, space.w_TypeError):
                     raise
@@ -263,7 +263,7 @@
     return rel_modulename, rel_level
 
 
- at unwrap_spec(name='str0', level=int)
+ at unwrap_spec(name='text0', level=int)
 def importhook(space, name, w_globals=None,
                w_locals=None, w_fromlist=None, level=-1):
     modulename = name
@@ -431,7 +431,7 @@
                 for i in range(length):
                     w_name = space.getitem(w_fromlist, space.newint(i))
                     if try_getattr(space, w_mod, w_name) is None:
-                        load_part(space, w_path, prefix, space.str0_w(w_name),
+                        load_part(space, w_path, prefix, space.text0_w(w_name),
                                   w_mod, tentative=1)
         return w_mod
     else:
@@ -728,7 +728,7 @@
         raise oefmt(space.w_TypeError, "reload() argument must be module")
 
     w_modulename = space.getattr(w_module, space.newtext("__name__"))
-    modulename = space.str0_w(w_modulename)
+    modulename = space.text0_w(w_modulename)
     if not space.is_w(check_sys_modules(space, w_modulename), w_module):
         raise oefmt(space.w_ImportError,
                     "reload(): module %s not in sys.modules", modulename)
diff --git a/pypy/module/imp/interp_imp.py b/pypy/module/imp/interp_imp.py
--- a/pypy/module/imp/interp_imp.py
+++ b/pypy/module/imp/interp_imp.py
@@ -49,7 +49,7 @@
         return space.interp_w(W_File, w_file).stream
 
 def find_module(space, w_name, w_path=None):
-    name = space.str0_w(w_name)
+    name = space.text0_w(w_name)
     if space.is_none(w_path):
         w_path = None
 
@@ -143,7 +143,7 @@
     return Module(space, w_name, add_package=False)
 
 def init_builtin(space, w_name):
-    name = space.str0_w(w_name)
+    name = space.text0_w(w_name)
     if name not in space.builtin_modules:
         return
     if space.finditem(space.sys.get('modules'), w_name) is not None:
@@ -155,7 +155,7 @@
     return None
 
 def is_builtin(space, w_name):
-    name = space.str0_w(w_name)
+    name = space.text0_w(w_name)
     if name not in space.builtin_modules:
         return space.newint(0)
     if space.finditem(space.sys.get('modules'), w_name) is not None:
diff --git a/pypy/module/posix/interp_posix.py b/pypy/module/posix/interp_posix.py
--- a/pypy/module/posix/interp_posix.py
+++ b/pypy/module/posix/interp_posix.py
@@ -38,9 +38,9 @@
 
 def fsencode_w(space, w_obj):
     if space.isinstance_w(w_obj, space.w_unicode):
-        w_obj = space.call_method(w_obj, 'encode',
+        w_obj = space.call_method(space.w_unicode, 'encode', w_obj,
                                   getfilesystemencoding(space))
-    return space.str0_w(w_obj)
+    return space.bytes0_w(w_obj)
 
 class FileEncoder(object):
     is_unicode = True
@@ -63,7 +63,7 @@
         self.w_obj = w_obj
 
     def as_bytes(self):
-        return self.space.str0_w(self.w_obj)
+        return self.space.bytes0_w(self.w_obj)
 
     def as_unicode(self):
         space = self.space
@@ -78,7 +78,7 @@
             fname = FileEncoder(space, w_fname)
             return func(fname, *args)
         else:
-            fname = space.str0_w(w_fname)
+            fname = space.bytes0_w(w_fname)
             return func(fname, *args)
     return dispatch
 
@@ -398,7 +398,7 @@
                                space.newfloat(times[3]),
                                space.newfloat(times[4])])
 
- at unwrap_spec(cmd='str0')
+ at unwrap_spec(cmd='text0')
 def system(space, cmd):
     """Execute the command (a string) in a subshell."""
     try:
@@ -430,7 +430,7 @@
             fullpath = rposix.getfullpathname(path)
             w_fullpath = space.newunicode(fullpath)
         else:
-            path = space.str0_w(w_path)
+            path = space.bytes0_w(w_path)
             fullpath = rposix.getfullpathname(path)
             w_fullpath = space.newbytes(fullpath)
     except OSError as e:
@@ -534,7 +534,7 @@
     for key, value in os.environ.items():
         space.setitem(w_env, space.newtext(key), space.newtext(value))
 
- at unwrap_spec(name='str0', value='str0')
+ at unwrap_spec(name='text0', value='text0')
 def putenv(space, name, value):
     """Change or add an environment variable."""
     if _WIN32 and len(name) > _MAX_ENV:
@@ -554,7 +554,7 @@
     except OSError as e:
         raise wrap_oserror(space, e)
 
- at unwrap_spec(name='str0')
+ at unwrap_spec(name='text0')
 def unsetenv(space, name):
     """Delete an environment variable."""
     try:
@@ -600,7 +600,7 @@
                 result_w[i] = w_res
             return space.newlist(result_w)
         else:
-            dirname = space.str0_w(w_dirname)
+            dirname = space.bytes0_w(w_dirname)
             result = rposix.listdir(dirname)
             # The list comprehension is a workaround for an obscure translation
             # bug.
@@ -820,7 +820,7 @@
     w_keys = space.call_method(w_env, 'keys')
     for w_key in space.unpackiterable(w_keys):
         w_value = space.getitem(w_env, w_key)
-        env[space.str0_w(w_key)] = space.str0_w(w_value)
+        env[space.text0_w(w_key)] = space.text0_w(w_value)
     return env
 
 def execve(space, w_command, w_args, w_env):


More information about the pypy-commit mailing list