Commits

Amaury Forgeot d'Arc committed 8b9d847

Unicode version of os.chmod()

Comments (0)

Files changed (4)

pypy/module/posix/interp_posix.py

     return space.newtuple([space.wrap(fd1), space.wrap(fd2)])
 pipe.unwrap_spec = [ObjSpace]
 
-def chmod(space, path, mode):
+def chmod(space, w_path, mode):
     "Change the access permissions of a file."
-    try: 
-        os.chmod(path, mode)
-    except OSError, e: 
-        raise wrap_oserror(space, e, path)
-chmod.unwrap_spec = [ObjSpace, str, "c_int"]
+    try:
+        dispatch_filename(rposix.chmod)(space, w_path, mode)
+    except OSError, e:
+        raise wrap_oserror2(space, e, w_path)
+chmod.unwrap_spec = [ObjSpace, W_Root, "c_int"]
 
 def rename(space, old, new):
     "Rename a file or directory."

pypy/rlib/rposix.py

         return os.access(path.encode(), mode)
 
 @specialize.argtype(0)
+def chmod(path, mode):
+    if isinstance(path, str):
+        return os.chmod(path, mode)
+    else:
+        return os.chmod(path.encode(), mode)
+
+@specialize.argtype(0)
 def chdir(path):
     if isinstance(path, str):
         return os.chdir(path)

pypy/rlib/test/test_rposix.py

     def __init__(self, unistr):
         self.unistr = unistr
 
-    def encode(self):
-        from pypy.rlib.runicode import unicode_encode_utf_8
-        return unicode_encode_utf_8(self.unistr, len(self.unistr),
-                                    "strict")
+    if sys.platform == 'win32':
+        def encode(self):
+            from pypy.rlib.runicode import unicode_encode_mbcs
+            return unicode_encode_mbcs(self.unistr, len(self.unistr),
+                                       "strict")
+    else:
+        def encode(self):
+            from pypy.rlib.runicode import unicode_encode_utf_8
+            return unicode_encode_utf_8(self.unistr, len(self.unistr),
+                                        "strict")
 
     def gettext(self):
         return self.unistr
 
         assert interpret(f, []) == 1
 
+    def test_chmod(self):
+        def f():
+            return rposix.chmod(self.path, 0777)
+
+        interpret(f, []) # does not crash
+
     def test_unlink(self):
         def f():
             return rposix.unlink(self.path)

pypy/rpython/module/ll_os.py

         return extdef([str, int], s_None, llimpl=chmod_llimpl,
                       export_name="ll_os.ll_os_chmod")
 
+    @registering_unicode_version(os.chmod, 2, [0], sys.platform=='win32')
+    def register_os_chmod_unicode(self):
+        os_wchmod = self.llexternal(underscore_on_windows+'wchmod', [rffi.CWCHARP, rffi.MODE_T],
+                                    rffi.INT)
+
+        def chmod_llimpl(path, mode):
+            res = rffi.cast(lltype.Signed, os_wchmod(path, rffi.cast(rffi.MODE_T, mode)))
+            if res < 0:
+                raise OSError(rposix.get_errno(), "os_chmod failed")
+
+        return extdef([unicode, int], s_None, llimpl=chmod_llimpl,
+                      export_name="ll_os.ll_os_wchmod")
+
     @registering(os.rename)
     def register_os_rename(self):
         os_rename = self.llexternal('rename', [rffi.CCHARP, rffi.CCHARP],