Commits

Amaury Forgeot d'Arc  committed e86aec0

Fix most failures in posix tests

  • Participants
  • Parent commits 4c6488c
  • Branches py3k

Comments (0)

Files changed (2)

File pypy/module/posix/test/test_posix2.py

     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)
 
             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)
         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,
     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)
         posix = self.posix
         try:
             posix.open('qowieuqwoeiu', 0, 0)
-        except OSError, e:
+        except OSError as e:
             assert e.filename == 'qowieuqwoeiu'
         else:
             assert 0
                 func = getattr(self.posix, fname)
                 try:
                     func('qowieuqw/oeiu')
-                except OSError, e:
+                except OSError as e:
                     assert e.filename == 'qowieuqw/oeiu'
                 else:
                     assert 0
     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
         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
         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
         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)
         # 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)
         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'
             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):
             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
 
             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")
             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):
             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
 
         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
         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'])))"],
             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()
 
     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"):
         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()
     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)
             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)
 
             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')
             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?"
     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
     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"):
     def test_stat_unicode(self):
         # test that passing unicode would not raise UnicodeDecodeError
         try:
-            self.posix.stat(u"ą")
+            self.posix.stat("ą")
         except OSError:
             pass
 
     def test_open_unicode(self):
         # Ensure passing unicode doesn't raise UnicodeEncodeError
         try:
-            self.posix.open(u"ą", self.posix.O_WRONLY)
+            self.posix.open("ą", self.posix.O_WRONLY)
         except OSError:
             pass
 
     def test_remove_unicode(self):
         # See 2 above ;)
         try:
-            self.posix.remove(u"ą")
+            self.posix.remove("ą")
         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:
             content = self.posix.read(fd, 50)
         finally:
             self.posix.close(fd)
-        assert content == "test"
+        assert content == b"test"
 
 
 class TestPexpect(object):

File pypy/module/posix/test/test_posix_libfile.py

     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"
         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