[pypy-commit] pypy default: Move fsencode_w() as a space method, and add a 'fsencode' unwrap_spec.

arigo pypy.commits at gmail.com
Tue Feb 21 08:42:15 EST 2017


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r90265:be3d0e6c4201
Date: 2017-02-21 14:41 +0100
http://bitbucket.org/pypy/pypy/changeset/be3d0e6c4201/

Log:	Move fsencode_w() as a space method, and add a 'fsencode'
	unwrap_spec.

diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -1639,6 +1639,14 @@
         "Like text_w, but rejects strings with NUL bytes."
         return self.bytes0_w(w_obj)
 
+    def fsencode_w(self, w_obj):
+        "Like text0_w, but unicodes are encoded with the filesystem encoding."
+        if self.isinstance_w(w_obj, self.w_unicode):
+            from pypy.module.sys.interp_encoding import getfilesystemencoding
+            w_obj = self.call_method(self.w_unicode, 'encode', w_obj,
+                                     getfilesystemencoding(self))
+        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
@@ -163,6 +163,9 @@
     def visit_text0(self, el, app_sig):
         self.checked_space_method(el, app_sig)
 
+    def visit_fsencode(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)
 
@@ -304,6 +307,9 @@
     def visit_text0(self, typ):
         self.run_args.append("space.text0_w(%s)" % (self.scopenext(),))
 
+    def visit_fsencode(self, typ):
+        self.run_args.append("space.fsencode_w(%s)" % (self.scopenext(),))
+
     def visit_nonnegint(self, typ):
         self.run_args.append("space.gateway_nonnegint_w(%s)" % (
             self.scopenext(),))
@@ -466,6 +472,9 @@
     def visit_text0(self, typ):
         self.unwrap.append("space.text0_w(%s)" % (self.nextarg(),))
 
+    def visit_fsencode(self, typ):
+        self.unwrap.append("space.fsencode_w(%s)" % (self.nextarg(),))
+
     def visit_nonnegint(self, typ):
         self.unwrap.append("space.gateway_nonnegint_w(%s)" % (self.nextarg(),))
 
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
@@ -36,12 +36,6 @@
         return space.newint(uid)     # an unsigned number
 wrap_gid = wrap_uid
 
-def fsencode_w(space, w_obj):
-    if space.isinstance_w(w_obj, space.w_unicode):
-        w_obj = space.call_method(space.w_unicode, 'encode', w_obj,
-                                  getfilesystemencoding(space))
-    return space.bytes0_w(w_obj)
-
 class FileEncoder(object):
     is_unicode = True
 
@@ -50,7 +44,7 @@
         self.w_obj = w_obj
 
     def as_bytes(self):
-        return fsencode_w(self.space, self.w_obj)
+        return self.space.fsencode_w(self.w_obj)
 
     def as_unicode(self):
         return self.space.unicode0_w(self.w_obj)
@@ -698,10 +692,9 @@
     import signal
     rposix.kill(os.getpid(), signal.SIGABRT)
 
-def link(space, w_src, w_dst):
+ at unwrap_spec(src='fsencode', dst='fsencode')
+def link(space, src, dst):
     "Create a hard link to a file."
-    src = fsencode_w(space, w_src)
-    dst = fsencode_w(space, w_dst)
     try:
         os.link(src, dst)
     except OSError as e:
@@ -714,9 +707,9 @@
     except OSError as e:
         raise wrap_oserror(space, e)
 
-def readlink(space, w_path):
+ at unwrap_spec(path='fsencode')
+def readlink(space, path):
     "Return a string representing the path to which the symbolic link points."
-    path = fsencode_w(space, w_path)
     try:
         result = os.readlink(path)
     except OSError as e:
@@ -805,7 +798,8 @@
 def _exit(space, status):
     os._exit(status)
 
-def execv(space, w_command, w_args):
+ at unwrap_spec(command='fsencode')
+def execv(space, command, w_args):
     """ execv(path, args)
 
 Execute an executable path with arguments, replacing current process.
@@ -813,7 +807,7 @@
         path: path of executable file
         args: iterable of strings
     """
-    execve(space, w_command, w_args, None)
+    execve(space, command, w_args, None)
 
 def _env2interp(space, w_env):
     env = {}
@@ -823,7 +817,8 @@
         env[space.text0_w(w_key)] = space.text0_w(w_value)
     return env
 
-def execve(space, w_command, w_args, w_env):
+ at unwrap_spec(command='fsencode')
+def execve(space, command, w_args, w_env):
     """ execve(path, args, env)
 
 Execute a path with arguments and environment, replacing current process.
@@ -832,13 +827,12 @@
         args: iterable of arguments
         env: dictionary of strings mapping to strings
     """
-    command = fsencode_w(space, w_command)
     try:
         args_w = space.unpackiterable(w_args)
         if len(args_w) < 1:
             raise oefmt(space.w_ValueError,
                         "execv() must have at least one argument")
-        args = [fsencode_w(space, w_arg) for w_arg in args_w]
+        args = [space.fsencode_w(w_arg) for w_arg in args_w]
     except OperationError as e:
         if not e.match(space, space.w_TypeError):
             raise
@@ -857,20 +851,18 @@
         except OSError as e:
             raise wrap_oserror(space, e)
 
- at unwrap_spec(mode=int)
-def spawnv(space, mode, w_path, w_args):
-    path = fsencode_w(space, w_path)
-    args = [fsencode_w(space, w_arg) for w_arg in space.unpackiterable(w_args)]
+ at unwrap_spec(mode=int, path='fsencode')
+def spawnv(space, mode, path, w_args):
+    args = [space.fsencode_w(w_arg) for w_arg in space.unpackiterable(w_args)]
     try:
         ret = os.spawnv(mode, path, args)
     except OSError as e:
         raise wrap_oserror(space, e)
     return space.newint(ret)
 
- at unwrap_spec(mode=int)
-def spawnve(space, mode, w_path, w_args, w_env):
-    path = fsencode_w(space, w_path)
-    args = [fsencode_w(space, w_arg) for w_arg in space.unpackiterable(w_args)]
+ at unwrap_spec(mode=int, path='fsencode')
+def spawnve(space, mode, path, w_args, w_env):
+    args = [space.fsencode_w(w_arg) for w_arg in space.unpackiterable(w_args)]
     env = _env2interp(space, w_env)
     try:
         ret = os.spawnve(mode, path, args, env)
@@ -969,12 +961,12 @@
     except OSError as e:
         raise wrap_oserror(space, e)
 
-def chroot(space, w_path):
+ at unwrap_spec(path='fsencode')
+def chroot(space, path):
     """ chroot(path)
 
     Change root directory to path.
     """
-    path = fsencode_w(space, w_path)
     try:
         os.chroot(path)
     except OSError as e:
@@ -1259,8 +1251,8 @@
         raise wrap_oserror(space, e)
     return space.newint(res)
 
-def pathconf(space, w_path, w_name):
-    path = fsencode_w(space, w_path)
+ at unwrap_spec(path='fsencode')
+def pathconf(space, path, w_name):
     num = confname_w(space, w_name, os.pathconf_names)
     try:
         res = os.pathconf(path, num)
@@ -1276,20 +1268,18 @@
         raise wrap_oserror(space, e)
     return space.newtext(res)
 
- at unwrap_spec(uid=c_uid_t, gid=c_gid_t)
-def chown(space, w_path, uid, gid):
+ at unwrap_spec(path='fsencode', uid=c_uid_t, gid=c_gid_t)
+def chown(space, path, uid, gid):
     """Change the owner and group id of path to the numeric uid and gid."""
-    path = fsencode_w(space, w_path)
     try:
         os.chown(path, uid, gid)
     except OSError as e:
         raise wrap_oserror(space, e, path)
 
- at unwrap_spec(uid=c_uid_t, gid=c_gid_t)
-def lchown(space, w_path, uid, gid):
+ at unwrap_spec(path='fsencode', uid=c_uid_t, gid=c_gid_t)
+def lchown(space, path, uid, gid):
     """Change the owner and group id of path to the numeric uid and gid.
 This function will not follow symbolic links."""
-    path = fsencode_w(space, w_path)
     try:
         os.lchown(path, uid, gid)
     except OSError as e:


More information about the pypy-commit mailing list