Author: Armin Rigo <ar...@tunes.org>
Branch: 
Changeset: r69713:4c5d9fd4c40d
Date: 2014-03-05 10:35 +0100
http://bitbucket.org/pypy/pypy/changeset/4c5d9fd4c40d/

Log:    Adding the missing "assert path is not None" to *all* *other*
        *functions* would be too much of a pain. Instead, refactor.

diff --git a/rpython/rlib/rposix.py b/rpython/rlib/rposix.py
--- a/rpython/rlib/rposix.py
+++ b/rpython/rlib/rposix.py
@@ -136,141 +136,92 @@
 # - but rpython.rtyper.module.ll_os.py on Windows will replace these functions
 #   with other wrappers that directly handle unicode strings.
 @specialize.argtype(0)
-def open(path, flags, mode):
+def _as_bytes(path):
     assert path is not None
     if isinstance(path, str):
-        return os.open(path, flags, mode)
+        return path
     else:
-        return os.open(path.as_bytes(), flags, mode)
+        return path.as_bytes()
+
+@specialize.argtype(0)
+def open(path, flags, mode):
+    return os.open(_as_bytes(path), flags, mode)
 
 @specialize.argtype(0)
 def stat(path):
-    if isinstance(path, str):
-        return os.stat(path)
-    else:
-        return os.stat(path.as_bytes())
+    return os.stat(_as_bytes(path))
 
 @specialize.argtype(0)
 def lstat(path):
-    if isinstance(path, str):
-        return os.lstat(path)
-    else:
-        return os.lstat(path.as_bytes())
+    return os.lstat(_as_bytes(path))
 
 
 @specialize.argtype(0)
 def statvfs(path):
-    if isinstance(path, str):
-        return os.statvfs(path)
-    else:
-        return os.statvfs(path.as_bytes())
+    return os.statvfs(_as_bytes(path))
 
 
 @specialize.argtype(0)
 def unlink(path):
-    if isinstance(path, str):
-        return os.unlink(path)
-    else:
-        return os.unlink(path.as_bytes())
+    return os.unlink(_as_bytes(path))
 
 @specialize.argtype(0, 1)
 def rename(path1, path2):
-    if isinstance(path1, str):
-        return os.rename(path1, path2)
-    else:
-        return os.rename(path1.as_bytes(), path2.as_bytes())
+    return os.rename(_as_bytes(path1), _as_bytes(path2))
 
 @specialize.argtype(0)
 def listdir(dirname):
-    if isinstance(dirname, str):
-        return os.listdir(dirname)
-    else:
-        return os.listdir(dirname.as_bytes())
+    return os.listdir(_as_bytes(dirname))
 
 @specialize.argtype(0)
 def access(path, mode):
-    if isinstance(path, str):
-        return os.access(path, mode)
-    else:
-        return os.access(path.as_bytes(), mode)
+    return os.access(_as_bytes(path), mode)
 
 @specialize.argtype(0)
 def chmod(path, mode):
-    if isinstance(path, str):
-        return os.chmod(path, mode)
-    else:
-        return os.chmod(path.as_bytes(), mode)
+    return os.chmod(_as_bytes(path), mode)
 
 @specialize.argtype(0, 1)
 def utime(path, times):
-    if isinstance(path, str):
-        return os.utime(path, times)
-    else:
-        return os.utime(path.as_bytes(), times)
+    return os.utime(_as_bytes(path), times)
 
 @specialize.argtype(0)
 def chdir(path):
-    if isinstance(path, str):
-        return os.chdir(path)
-    else:
-        return os.chdir(path.as_bytes())
+    return os.chdir(_as_bytes(path))
 
 @specialize.argtype(0)
 def mkdir(path, mode=0777):
-    if isinstance(path, str):
-        return os.mkdir(path, mode)
-    else:
-        return os.mkdir(path.as_bytes(), mode)
+    return os.mkdir(_as_bytes(path), mode)
 
 @specialize.argtype(0)
 def rmdir(path):
-    if isinstance(path, str):
-        return os.rmdir(path)
-    else:
-        return os.rmdir(path.as_bytes())
+    return os.rmdir(_as_bytes(path))
 
 @specialize.argtype(0)
 def mkfifo(path, mode):
-    if isinstance(path, str):
-        os.mkfifo(path, mode)
-    else:
-        os.mkfifo(path.as_bytes(), mode)
+    os.mkfifo(_as_bytes(path), mode)
 
 @specialize.argtype(0)
 def mknod(path, mode, device):
-    if isinstance(path, str):
-        os.mknod(path, mode, device)
-    else:
-        os.mknod(path.as_bytes(), mode, device)
+    os.mknod(_as_bytes(path), mode, device)
 
 @specialize.argtype(0, 1)
 def symlink(src, dest):
-    if isinstance(src, str):
-        os.symlink(src, dest)
-    else:
-        os.symlink(src.as_bytes(), dest.as_bytes())
+    os.symlink(_as_bytes(src), _as_bytes(dest))
 
 if os.name == 'nt':
     import nt
+    @specialize.argtype(0)
     def _getfullpathname(path):
-        if isinstance(path, str):
-            return nt._getfullpathname(path)
-        else:
-            return nt._getfullpathname(path.as_bytes())
+        return nt._getfullpathname(_as_bytes(path))
 
 @specialize.argtype(0, 1)
 def putenv(name, value):
-    if isinstance(name, str):
-        os.environ[name] = value
-    else:
-        os.environ[name.as_bytes()] = value.as_bytes()
+    os.environ[_as_bytes(name)] = _as_bytes(value)
 
 @specialize.argtype(0)
 def unsetenv(name):
-    if isinstance(name, str):
-        del os.environ[name]
-    else:
-        del os.environ[name.as_bytes()]
+    del os.environ[_as_bytes(name)]
 
 if os.name == 'nt':
     from rpython.rlib import rwin32
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to