Author: Ronan Lamy <ronan.l...@gmail.com>
Branch: follow_symlinks
Changeset: r83677:c353cff42cfd
Date: 2016-04-15 04:36 +0100
http://bitbucket.org/pypy/pypy/changeset/c353cff42cfd/

Log:    match CPython error messages better

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
@@ -134,6 +134,9 @@
 
 class _PathOrFd(Unwrapper):
     def unwrap(self, space, w_value):
+        if space.is_none(w_value):
+            raise oefmt(space.w_TypeError,
+                "can't specify None for path argument")
         if _WIN32:
             try:
                 path_u = space.unicode_w(w_value)
@@ -145,10 +148,7 @@
             return Path(-1, path_b, None, w_value)
         except OperationError:
             pass
-        if not space.isinstance_w(w_value, space.w_int):
-            raise oefmt(space.w_TypeError,
-                "argument should be string, bytes or integer, not %T", w_value)
-        fd = unwrap_fd(space, w_value)
+        fd = unwrap_fd(space, w_value, "string, bytes or integer")
         return Path(fd, None, None, w_value)
 
 class _JustPath(Unwrapper):
@@ -175,8 +175,16 @@
     DEFAULT_DIR_FD = -100
 DIR_FD_AVAILABLE = False
 
-def unwrap_fd(space, w_value):
-    return space.c_int_w(w_value)
+@specialize.arg(2)
+def unwrap_fd(space, w_value, allowed_types='integer'):
+    try:
+        return space.c_int_w(w_value)
+    except OperationError as e:
+        if not e.match(space, space.w_OverflowError):
+            raise oefmt(space.w_TypeError,
+                "argument should be %s, not %T", allowed_types, w_value)
+        else:
+            raise
 
 def _unwrap_dirfd(space, w_value):
     if space.is_none(w_value):
diff --git a/pypy/module/posix/test/test_posix2.py 
b/pypy/module/posix/test/test_posix2.py
--- a/pypy/module/posix/test/test_posix2.py
+++ b/pypy/module/posix/test/test_posix2.py
@@ -174,6 +174,10 @@
         import stat
         st = self.posix.stat(".")
         assert stat.S_ISDIR(st.st_mode)
+        st = self.posix.stat(b".")
+        assert stat.S_ISDIR(st.st_mode)
+        st = self.posix.stat(bytearray(b"."))
+        assert stat.S_ISDIR(st.st_mode)
         st = self.posix.lstat(".")
         assert stat.S_ISDIR(st.st_mode)
 
@@ -185,6 +189,11 @@
             assert exc.value.errno == errno.ENOENT
             assert exc.value.filename == "nonexistentdir/nonexistentfile"
 
+        excinfo = raises(TypeError, self.posix.stat, None)
+        assert "can't specify None" in str(excinfo.value)
+        excinfo = raises(TypeError, self.posix.stat, 2.)
+        assert "should be string, bytes or integer, not float" in 
str(excinfo.value)
+
     if hasattr(__import__(os.name), "statvfs"):
         def test_statvfs(self):
             st = self.posix.statvfs(".")
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to