[pypy-commit] pypy win32-cleanup2: add more validate_fd
mattip
noreply at buildbot.pypy.org
Sun Apr 8 13:08:56 CEST 2012
Author: Matti Picus <matti.picus at gmail.com>
Branch: win32-cleanup2
Changeset: r54244:10ccfa6466f5
Date: 2012-04-08 13:02 +0300
http://bitbucket.org/pypy/pypy/changeset/10ccfa6466f5/
Log: add more validate_fd
diff --git a/pypy/rpython/module/ll_os.py b/pypy/rpython/module/ll_os.py
--- a/pypy/rpython/module/ll_os.py
+++ b/pypy/rpython/module/ll_os.py
@@ -942,6 +942,8 @@
rffi.INT, threadsafe=False)
def close_llimpl(fd):
+ if not rposix.validate_fd(fd):
+ raise OSError(rposix.get_errno(), 'Bad file descriptor')
error = rffi.cast(lltype.Signed, os_close(rffi.cast(rffi.INT, fd)))
if error == -1:
raise OSError(rposix.get_errno(), "close failed")
@@ -977,6 +979,8 @@
rffi.LONGLONG)
def lseek_llimpl(fd, pos, how):
+ if not rposix.validate_fd(fd):
+ raise OSError(rposix.get_errno(), 'Bad file descriptor')
how = fix_seek_arg(how)
res = os_lseek(rffi.cast(rffi.INT, fd),
rffi.cast(rffi.LONGLONG, pos),
@@ -1002,6 +1006,8 @@
[rffi.INT, rffi.LONGLONG], rffi.INT)
def ftruncate_llimpl(fd, length):
+ if not rposix.validate_fd(fd):
+ raise OSError(rposix.get_errno(), 'Bad file descriptor')
res = rffi.cast(rffi.LONG,
os_ftruncate(rffi.cast(rffi.INT, fd),
rffi.cast(rffi.LONGLONG, length)))
@@ -1020,6 +1026,8 @@
os_fsync = self.llexternal('_commit', [rffi.INT], rffi.INT)
def fsync_llimpl(fd):
+ if not rposix.validate_fd(fd):
+ raise OSError(rposix.get_errno(), 'Bad file descriptor')
res = rffi.cast(rffi.SIGNED, os_fsync(rffi.cast(rffi.INT, fd)))
if res < 0:
raise OSError(rposix.get_errno(), "fsync failed")
@@ -1032,6 +1040,8 @@
os_fdatasync = self.llexternal('fdatasync', [rffi.INT], rffi.INT)
def fdatasync_llimpl(fd):
+ if not rposix.validate_fd(fd):
+ raise OSError(rposix.get_errno(), 'Bad file descriptor')
res = rffi.cast(rffi.SIGNED, os_fdatasync(rffi.cast(rffi.INT, fd)))
if res < 0:
raise OSError(rposix.get_errno(), "fdatasync failed")
diff --git a/pypy/rpython/module/test/test_ll_os.py b/pypy/rpython/module/test/test_ll_os.py
--- a/pypy/rpython/module/test/test_ll_os.py
+++ b/pypy/rpython/module/test/test_ll_os.py
@@ -191,7 +191,7 @@
def test_os_write():
#Same as test in rpython/test/test_rbuiltin
- fname = str(udir.join('os_write_test.txt'))
+ fname = str(udir.join('os_test.txt'))
fd = os.open(fname, os.O_WRONLY|os.O_CREAT, 0777)
assert fd >= 0
os.write(fd, 'Hello world')
@@ -202,6 +202,46 @@
os.close(fd)
raises(OSError, os.write, fd, 'Hello world')
+def test_os_close():
+ fname = str(udir.join('os_test.txt'))
+ fd = os.open(fname, os.O_WRONLY|os.O_CREAT, 0777)
+ assert fd >= 0
+ os.write(fd, 'Hello world')
+ os.close(fd)
+ raises(OSError, os.close, fd)
+
+def test_os_lseek():
+ fname = str(udir.join('os_test.txt'))
+ fd = os.open(fname, os.O_WRONLY|os.O_CREAT, 0777)
+ assert fd >= 0
+ os.write(fd, 'Hello world')
+ os.lseek(fd,0,0)
+ assert os.tell(fd) == 0
+ os.close(fd)
+ raises(OSError, os.lseek, fd, 0, 0)
+
+def test_os_fsync():
+ fname = str(udir.join('os_test.txt'))
+ fd = os.open(fname, os.O_WRONLY|os.O_CREAT, 0777)
+ assert fd >= 0
+ os.write(fd, 'Hello world')
+ os.fsync(fd)
+ fid = open(fname)
+ assert fid.read() == 'Hello world'
+ os.close(fd)
+ raises(OSError, os.fsync, fd)
+
+def test_os_fdatasync():
+ fname = str(udir.join('os_test.txt'))
+ fd = os.open(fname, os.O_WRONLY|os.O_CREAT, 0777)
+ assert fd >= 0
+ os.write(fd, 'Hello world')
+ os.fdatasync(fd)
+ fid = open(fname)
+ assert fid.read() == 'Hello world'
+ os.close(fd)
+ raises(OSError, os.fdatasync, fd)
+
class ExpectTestOs:
def setup_class(cls):
if not hasattr(os, 'ttyname'):
More information about the pypy-commit
mailing list