[pypy-commit] pypy py3k: Fix most failures in posix tests

amauryfa noreply at buildbot.pypy.org
Tue Feb 7 23:53:15 CET 2012


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r52208:e86aec0bdbb3
Date: 2012-02-07 23:38 +0100
http://bitbucket.org/pypy/pypy/changeset/e86aec0bdbb3/

Log:	Fix most failures in posix tests

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
@@ -93,14 +93,14 @@
     def test_some_posix_basic_operation(self):
         path = self.path
         posix = self.posix
-        fd = posix.open(path, posix.O_RDONLY, 0777)
+        fd = posix.open(path, posix.O_RDONLY, 0o777)
         fd2 = posix.dup(fd)
         assert not posix.isatty(fd2)
         s = posix.read(fd, 1)
-        assert s == 't'
+        assert s == b't'
         posix.lseek(fd, 5, 0)
         s = posix.read(fd, 1)
-        assert s == 'i'
+        assert s == b'i'
         st = posix.fstat(fd)
         posix.close(fd2)
         posix.close(fd)
@@ -147,7 +147,7 @@
 
             posix.stat_float_times(False)
             st = posix.stat(path)
-            assert isinstance(st.st_mtime, (int, long))
+            assert isinstance(st.st_mtime, int)
             assert st[7] == st.st_atime
         finally:
             posix.stat_float_times(current)
@@ -170,7 +170,7 @@
         for fn in [self.posix.stat, self.posix.lstat]:
             try:
                 fn("nonexistentdir/nonexistentfile")
-            except OSError, e:
+            except OSError as e:
                 assert e.errno == errno.ENOENT
                 assert e.filename == "nonexistentdir/nonexistentfile"
                 # On Windows, when the parent directory does not exist,
@@ -183,9 +183,9 @@
     def test_pickle(self):
         import pickle, os
         st = self.posix.stat(os.curdir)
-        print type(st).__module__
+        print(type(st).__module__)
         s = pickle.dumps(st)
-        print repr(s)
+        print(repr(s))
         new = pickle.loads(s)
         assert new == st
         assert type(new) is type(st)
@@ -194,7 +194,7 @@
         posix = self.posix
         try:
             posix.open('qowieuqwoeiu', 0, 0)
-        except OSError, e:
+        except OSError as e:
             assert e.filename == 'qowieuqwoeiu'
         else:
             assert 0
@@ -208,7 +208,7 @@
                 func = getattr(self.posix, fname)
                 try:
                     func('qowieuqw/oeiu')
-                except OSError, e:
+                except OSError as e:
                     assert e.filename == 'qowieuqw/oeiu'
                 else:
                     assert 0
@@ -216,7 +216,7 @@
     def test_chmod_exception(self):
         try:
             self.posix.chmod('qowieuqw/oeiu', 0)
-        except OSError, e:
+        except OSError as e:
             assert e.filename == 'qowieuqw/oeiu'
         else:
             assert 0
@@ -225,7 +225,7 @@
         if hasattr(self.posix, 'chown'):
             try:
                 self.posix.chown('qowieuqw/oeiu', 0, 0)
-            except OSError, e:
+            except OSError as e:
                 assert e.filename == 'qowieuqw/oeiu'
             else:
                 assert 0
@@ -234,7 +234,7 @@
         for arg in [None, (0, 0)]:
             try:
                 self.posix.utime('qowieuqw/oeiu', arg)
-            except OSError, e:
+            except OSError as e:
                 assert e.filename == 'qowieuqw/oeiu'
             else:
                 assert 0
@@ -254,7 +254,7 @@
         ex(self.posix.lseek, UNUSEDFD, 123, 0)
         #apparently not posix-required: ex(self.posix.isatty, UNUSEDFD)
         ex(self.posix.read, UNUSEDFD, 123)
-        ex(self.posix.write, UNUSEDFD, "x")
+        ex(self.posix.write, UNUSEDFD, b"x")
         ex(self.posix.close, UNUSEDFD)
         #UMPF cpython raises IOError ex(self.posix.ftruncate, UNUSEDFD, 123)
         ex(self.posix.fstat, UNUSEDFD)
@@ -262,37 +262,6 @@
         # how can getcwd() raise?
         ex(self.posix.dup, UNUSEDFD)
 
-    def test_fdopen(self):
-        import errno
-        path = self.path
-        posix = self.posix
-        fd = posix.open(path, posix.O_RDONLY, 0777)
-        f = posix.fdopen(fd, "r")
-        f.close()
-
-        # Ensure that fcntl is not faked
-        try:
-            import fcntl
-        except ImportError:
-            pass
-        else:
-            assert fcntl.__file__.endswith('pypy/module/fcntl')
-        exc = raises(OSError, posix.fdopen, fd)
-        assert exc.value.errno == errno.EBADF
-
-    def test_fdopen_hackedbuiltins(self):
-        "Same test, with __builtins__.file removed"
-        _file = __builtins__.file
-        __builtins__.file = None
-        try:
-            path = self.path
-            posix = self.posix
-            fd = posix.open(path, posix.O_RDONLY, 0777)
-            f = posix.fdopen(fd, "r")
-            f.close()
-        finally:
-            __builtins__.file = _file
-
     def test_getcwd(self):
         assert isinstance(self.posix.getcwd(), str)
         assert isinstance(self.posix.getcwdu(), unicode)
@@ -314,8 +283,7 @@
         posix = self.posix
         result = posix.listdir(unicode_dir)
         result.sort()
-        assert result == [u'somefile']
-        assert type(result[0]) is unicode
+        assert result == ['somefile']
 
     def test_access(self):
         pdir = self.pdir + '/file1'
@@ -364,9 +332,9 @@
             master_fd, slave_fd = os.openpty()
             assert isinstance(master_fd, int)
             assert isinstance(slave_fd, int)
-            os.write(slave_fd, 'x\n')
+            os.write(slave_fd, b'x\n')
             data = os.read(master_fd, 100)
-            assert data.startswith('x')
+            assert data.startswith(b'x')
 
     if hasattr(__import__(os.name), "forkpty"):
         def test_forkpty(self):
@@ -379,11 +347,11 @@
             assert isinstance(master_fd, int)
             if childpid == 0:
                 data = os.read(0, 100)
-                if data.startswith('abc'):
+                if data.startswith(b'abc'):
                     os._exit(42)
                 else:
                     os._exit(43)
-            os.write(master_fd, 'abc\n')
+            os.write(master_fd, b'abc\n')
             _, status = os.waitpid(childpid, 0)
             assert status >> 8 == 42
 
@@ -412,7 +380,7 @@
             for n in 3, [3, "a"]:
                 try:
                     os.execv("xxx", n)
-                except TypeError,t:
+                except TypeError as t:
                     assert str(t) == "execv() arg 2 must be an iterable of strings"
                 else:
                     py.test.fail("didn't raise")
@@ -423,15 +391,15 @@
             if not hasattr(os, "fork"):
                 skip("Need fork() to test execv()")
             try:
-                output = u"caf\xe9 \u1234\n".encode(sys.getfilesystemencoding())
+                output = "caf\xe9 \u1234\n".encode(sys.getfilesystemencoding())
             except UnicodeEncodeError:
                 skip("encoding not good enough")
             pid = os.fork()
             if pid == 0:
-                os.execv(u"/bin/sh", ["sh", "-c",
-                                      u"echo caf\xe9 \u1234 > onefile"])
+                os.execv("/bin/sh", ["sh", "-c",
+                                     "echo caf\xe9 \u1234 > onefile"])
             os.waitpid(pid, 0)
-            assert open("onefile").read() == output
+            assert open("onefile", "rb").read() == output
             os.unlink("onefile")
 
         def test_execve(self):
@@ -451,16 +419,16 @@
             if not hasattr(os, "fork"):
                 skip("Need fork() to test execve()")
             try:
-                output = u"caf\xe9 \u1234\n".encode(sys.getfilesystemencoding())
+                output = "caf\xe9 \u1234\n".encode(sys.getfilesystemencoding())
             except UnicodeEncodeError:
                 skip("encoding not good enough")
             pid = os.fork()
             if pid == 0:
-                os.execve(u"/bin/sh", ["sh", "-c",
-                                      u"echo caf\xe9 \u1234 > onefile"],
+                os.execve("/bin/sh", ["sh", "-c",
+                                      "echo caf\xe9 \u1234 > onefile"],
                           {'ddd': 'xxx'})
             os.waitpid(pid, 0)
-            assert open("onefile").read() == output
+            assert open("onefile", "rb").read() == output
             os.unlink("onefile")
         pass # <- please, inspect.getsource(), don't crash
 
@@ -468,7 +436,7 @@
         def test_spawnv(self):
             os = self.posix
             import sys
-            print self.python
+            print(self.python)
             ret = os.spawnv(os.P_WAIT, self.python,
                             ['python', '-c', 'raise(SystemExit(42))'])
             assert ret == 42
@@ -477,7 +445,7 @@
         def test_spawnve(self):
             os = self.posix
             import sys
-            print self.python
+            print(self.python)
             ret = os.spawnve(os.P_WAIT, self.python,
                              ['python', '-c',
                               "raise(SystemExit(int(__import__('os').environ['FOOBAR'])))"],
@@ -654,7 +622,6 @@
             try:
                 fd = f.fileno()
                 os.fsync(fd)
-                os.fsync(long(fd))
                 os.fsync(f)     # <- should also work with a file, or anything
             finally:            #    with a fileno() method
                 f.close()
@@ -701,49 +668,48 @@
 
     def test_largefile(self):
         os = self.posix
-        fd = os.open(self.path2 + 'test_largefile', os.O_RDWR | os.O_CREAT, 0666)
-        os.ftruncate(fd, 10000000000L)
-        res = os.lseek(fd, 9900000000L, 0)
-        assert res == 9900000000L
-        res = os.lseek(fd, -5000000000L, 1)
-        assert res == 4900000000L
-        res = os.lseek(fd, -5200000000L, 2)
-        assert res == 4800000000L
+        fd = os.open(self.path2 + 'test_largefile',
+                     os.O_RDWR | os.O_CREAT, 0o666)
+        os.ftruncate(fd, 10000000000)
+        res = os.lseek(fd, 9900000000, 0)
+        assert res == 9900000000
+        res = os.lseek(fd, -5000000000, 1)
+        assert res == 4900000000
+        res = os.lseek(fd, -5200000000, 2)
+        assert res == 4800000000
         os.close(fd)
 
         st = os.stat(self.path2 + 'test_largefile')
-        assert st.st_size == 10000000000L
+        assert st.st_size == 10000000000
     test_largefile.need_sparse_files = True
 
     def test_write_buffer(self):
         os = self.posix
-        fd = os.open(self.path2 + 'test_write_buffer', os.O_RDWR | os.O_CREAT, 0666)
+        fd = os.open(self.path2 + 'test_write_buffer',
+                     os.O_RDWR | os.O_CREAT, 0o666)
         def writeall(s):
             while s:
                 count = os.write(fd, s)
                 assert count > 0
                 s = s[count:]
-        writeall('hello, ')
-        writeall(buffer('world!\n'))
+        writeall(b'hello, ')
+        writeall(buffer(b'world!\n'))
         res = os.lseek(fd, 0, 0)
         assert res == 0
-        data = ''
+        data = b''
         while True:
             s = os.read(fd, 100)
             if not s:
                 break
             data += s
-        assert data == 'hello, world!\n'
+        assert data == b'hello, world!\n'
         os.close(fd)
 
     def test_write_unicode(self):
         os = self.posix
-        fd = os.open(self.path2 + 'test_write_unicode', os.O_RDWR | os.O_CREAT, 0666)
-        os.write(fd, u'X')
-        raises(UnicodeEncodeError, os.write, fd, u'\xe9')
-        os.lseek(fd, 0, 0)
-        data = os.read(fd, 2)
-        assert data == 'X'
+        fd = os.open(self.path2 + 'test_write_unicode',
+                     os.O_RDWR | os.O_CREAT, 0o666)
+        raises(TypeError, os.write, fd, 'X')
         os.close(fd)
 
     if hasattr(__import__(os.name), "fork"):
@@ -762,7 +728,7 @@
         os = self.posix
         if not hasattr(os, 'closerange'):
             skip("missing os.closerange()")
-        fds = [os.open(self.path + str(i), os.O_CREAT|os.O_WRONLY, 0777)
+        fds = [os.open(self.path + str(i), os.O_CREAT|os.O_WRONLY, 0o777)
                for i in range(15)]
         fds.sort()
         start = fds.pop()
@@ -796,7 +762,7 @@
     if hasattr(os, 'mkfifo'):
         def test_mkfifo(self):
             os = self.posix
-            os.mkfifo(self.path2 + 'test_mkfifo', 0666)
+            os.mkfifo(self.path2 + 'test_mkfifo', 0o666)
             st = os.lstat(self.path2 + 'test_mkfifo')
             import stat
             assert stat.S_ISFIFO(st.st_mode)
@@ -809,12 +775,12 @@
             try:
                 # not very useful: os.mknod() without specifying 'mode'
                 os.mknod(self.path2 + 'test_mknod-1')
-            except OSError, e:
+            except OSError as e:
                 skip("os.mknod(): got %r" % (e,))
             st = os.lstat(self.path2 + 'test_mknod-1')
             assert stat.S_ISREG(st.st_mode)
             # os.mknod() with S_IFIFO
-            os.mknod(self.path2 + 'test_mknod-2', 0600 | stat.S_IFIFO)
+            os.mknod(self.path2 + 'test_mknod-2', 0o600 | stat.S_IFIFO)
             st = os.lstat(self.path2 + 'test_mknod-2')
             assert stat.S_ISFIFO(st.st_mode)
 
@@ -825,9 +791,9 @@
             if hasattr(os.lstat('.'), 'st_rdev'):
                 import stat
                 try:
-                    os.mknod(self.path2 + 'test_mknod-3', 0600 | stat.S_IFCHR,
+                    os.mknod(self.path2 + 'test_mknod-3', 0o600 | stat.S_IFCHR,
                              0x105)
-                except OSError, e:
+                except OSError as e:
                     skip("os.mknod() with S_IFCHR: got %r" % (e,))
                 else:
                     st = os.lstat(self.path2 + 'test_mknod-3')
@@ -855,8 +821,8 @@
             unicode_dir = self.unicode_dir
             if unicode_dir is None:
                 skip("encoding not good enough")
-            dest = u"%s/file.txt" % unicode_dir
-            posix.symlink(u"%s/somefile" % unicode_dir, dest)
+            dest = "%s/file.txt" % unicode_dir
+            posix.symlink("%s/somefile" % unicode_dir, dest)
             with open(dest) as f:
                 data = f.read()
                 assert data == "who cares?"
@@ -874,11 +840,11 @@
     def test_tmpfile(self):
         os = self.posix
         f = os.tmpfile()
-        f.write("xxx")
+        f.write(b"xxx")
         f.flush()
         f.seek(0, 0)
         assert isinstance(f, file)
-        assert f.read() == 'xxx'
+        assert f.read() == b'xxx'
 
     def test_tmpnam(self):
         import stat, os
@@ -943,9 +909,9 @@
     def test_environ(self):
         posix = self.posix
         os = self.os
-        assert posix.environ['PATH']
-        del posix.environ['PATH']
-        def fn(): posix.environ['PATH']
+        assert posix.environ[b'PATH']
+        del posix.environ[b'PATH']
+        def fn(): posix.environ[b'PATH']
         raises(KeyError, fn)
 
     if hasattr(__import__(os.name), "unsetenv"):
@@ -989,28 +955,28 @@
     def test_stat_unicode(self):
         # test that passing unicode would not raise UnicodeDecodeError
         try:
-            self.posix.stat(u"&#261;")
+            self.posix.stat("&#261;")
         except OSError:
             pass
 
     def test_open_unicode(self):
         # Ensure passing unicode doesn't raise UnicodeEncodeError
         try:
-            self.posix.open(u"&#261;", self.posix.O_WRONLY)
+            self.posix.open("&#261;", self.posix.O_WRONLY)
         except OSError:
             pass
 
     def test_remove_unicode(self):
         # See 2 above ;)
         try:
-            self.posix.remove(u"&#261;")
+            self.posix.remove("&#261;")
         except OSError:
             pass
 
 class AppTestUnicodeFilename:
     def setup_class(cls):
         ufilename = (unicode(udir.join('test_unicode_filename_')) +
-                     u'\u65e5\u672c.txt') # "Japan"
+                     '\u65e5\u672c.txt') # "Japan"
         try:
             f = file(ufilename, 'w')
         except UnicodeEncodeError:
@@ -1027,7 +993,7 @@
             content = self.posix.read(fd, 50)
         finally:
             self.posix.close(fd)
-        assert content == "test"
+        assert content == b"test"
 
 
 class TestPexpect(object):
diff --git a/pypy/module/posix/test/test_posix_libfile.py b/pypy/module/posix/test/test_posix_libfile.py
--- a/pypy/module/posix/test/test_posix_libfile.py
+++ b/pypy/module/posix/test/test_posix_libfile.py
@@ -19,7 +19,7 @@
     def test_fdopen(self):
         path = self.path 
         posix = self.posix 
-        fd = posix.open(path, posix.O_RDONLY, 0777)
+        fd = posix.open(path, posix.O_RDONLY, 0o777)
         f = posix.fdopen(fd, "r")
         result = f.read()
         assert result == "this is a test"
@@ -56,12 +56,12 @@
         if sys.platform.startswith('win'):
             skip("unix specific")
         #
-        import __builtin__
+        import builtins
         posix = self.posix
         orig_file = file
         try:
             f = posix.popen('true')
-            __builtin__.file = lambda x : explode
+            builtins.file = lambda x : explode
             f.close()
         finally:
-            __builtin__.file = orig_file
+            builtins.file = orig_file


More information about the pypy-commit mailing list