Commits

Amaury Forgeot d'Arc committed b96d65a

Unicode versions of os.mkdir & os.rmdir

  • Participants
  • Parent commits a7b2ffb
  • Branches unicode_filename-2

Comments (0)

Files changed (4)

File pypy/module/posix/interp_posix.py

         raise wrap_oserror2(space, e, w_path)
 chdir.unwrap_spec = [ObjSpace, W_Root]
 
-def mkdir(space, path, mode=0777):
+def mkdir(space, w_path, mode=0777):
     """Create a directory."""
     try:
-        os.mkdir(path, mode)
-    except OSError, e: 
-        raise wrap_oserror(space, e, path)
-mkdir.unwrap_spec = [ObjSpace, str, "c_int"]
+        dispatch_filename(rposix.mkdir)(space, w_path, mode)
+    except OSError, e:
+        raise wrap_oserror2(space, e, w_path)
+mkdir.unwrap_spec = [ObjSpace, W_Root, "c_int"]
 
-def rmdir(space, path):
+def rmdir(space, w_path):
     """Remove a directory."""
     try:
-        os.rmdir(path)
-    except OSError, e: 
-        raise wrap_oserror(space, e, path)
-rmdir.unwrap_spec = [ObjSpace, str]
+        dispatch_filename(rposix.rmdir)(space, w_path)
+    except OSError, e:
+        raise wrap_oserror2(space, e, w_path)
+rmdir.unwrap_spec = [ObjSpace, W_Root]
 
 def strerror(space, errno):
     """Translate an error code to a message string."""

File pypy/rlib/rposix.py

         return os.chdir(path)
     else:
         return os.chdir(path.encode())
+
+@specialize.argtype(0)
+def mkdir(path, mode=0777):
+    if isinstance(path, str):
+        return os.mkdir(path, mode)
+    else:
+        return os.mkdir(path.encode(), mode)
+
+@specialize.argtype(0)
+def rmdir(path):
+    if isinstance(path, str):
+        return os.rmdir(path)
+    else:
+        return os.rmdir(path.encode())

File pypy/rlib/test/test_rposix.py

 
     def test_chdir(self):
         os.unlink(self.ufilename)
-        os.mkdir(self.ufilename)
 
         def f():
+            rposix.mkdir(self.path, 0777)
             rposix.chdir(self.path)
 
         curdir = os.getcwd()
             assert os.getcwdu() == self.ufilename
         finally:
             os.chdir(curdir)
+
+        def g():
+            rposix.rmdir(self.path)
+
+        try:
+            interpret(g, [])
+        finally:
+            try:
+                os.rmdir(self.ufilename)
+            except Exception:
+                pass

File pypy/rpython/module/ll_os.py

         return extdef([str, int], s_None, llimpl=mkdir_llimpl,
                       export_name="ll_os.ll_os_mkdir")
 
+    @registering_unicode_version(os.mkdir, 2, [0], sys.platform=='win32')
+    def register_os_mkdir_unicode(self):
+        if os.name == 'nt':
+            ARG2 = []         # no 'mode' argument on Windows - just ignored
+        else:
+            ARG2 = [rffi.MODE_T]
+        os_wmkdir = self.llexternal(underscore_on_windows+'wmkdir',
+                                    [rffi.CWCHARP]+ARG2, rffi.INT)
+        IGNORE_MODE = len(ARG2) == 0
+
+        def mkdir_llimpl(pathname, mode):
+            if IGNORE_MODE:
+                res = os_wmkdir(pathname)
+            else:
+                res = os_wmkdir(pathname, mode)
+            res = rffi.cast(lltype.Signed, res)
+            if res < 0:
+                raise OSError(rposix.get_errno(), "os_mkdir failed")
+
+        return extdef([unicode, int], s_None, llimpl=mkdir_llimpl,
+                      export_name="ll_os.ll_os_wmkdir")
+
     @registering(os.rmdir)
     def register_os_rmdir(self):
         os_rmdir = self.llexternal(underscore_on_windows+'rmdir', [rffi.CCHARP], rffi.INT)
         return extdef([str], s_None, llimpl=rmdir_llimpl,
                       export_name="ll_os.ll_os_rmdir")
 
+    @registering_unicode_version(os.rmdir, 1, [0], sys.platform=='win32')
+    def register_os_rmdir_unicode(self):
+        os_wrmdir = self.llexternal(underscore_on_windows+'wrmdir', [rffi.CWCHARP], rffi.INT)
+
+        def rmdir_llimpl(pathname):
+            res = rffi.cast(lltype.Signed, os_wrmdir(pathname))
+            if res < 0:
+                raise OSError(rposix.get_errno(), "os_rmdir failed")
+
+        return extdef([unicode], s_None, llimpl=rmdir_llimpl,
+                      export_name="ll_os.ll_os_wrmdir")
+
     @registering(os.chmod)
     def register_os_chmod(self):
         os_chmod = self.llexternal(underscore_on_windows+'chmod', [rffi.CCHARP, rffi.MODE_T],