[pypy-commit] pypy default: test for valid fd earlier

mattip noreply at buildbot.pypy.org
Mon Oct 6 07:03:02 CEST 2014


Author: mattip <matti.picus at gmail.com>
Branch: 
Changeset: r73821:ab66c5140bd6
Date: 2014-10-06 00:09 +0300
http://bitbucket.org/pypy/pypy/changeset/ab66c5140bd6/

Log:	test for valid fd earlier

diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py
--- a/pypy/module/cpyext/api.py
+++ b/pypy/module/cpyext/api.py
@@ -116,6 +116,8 @@
     validate_fd(fileno(fp))
     return _feof(fp)
 
+def is_valid_fp(fp):
+    return is_valid_fd(fileno(fp))
 
 constant_names = """
 Py_TPFLAGS_READY Py_TPFLAGS_READYING Py_TPFLAGS_HAVE_GETCHARBUFFER
diff --git a/pypy/module/cpyext/eval.py b/pypy/module/cpyext/eval.py
--- a/pypy/module/cpyext/eval.py
+++ b/pypy/module/cpyext/eval.py
@@ -3,7 +3,7 @@
 from rpython.rtyper.lltypesystem import rffi, lltype
 from pypy.module.cpyext.api import (
     cpython_api, CANNOT_FAIL, CONST_STRING, FILEP, fread, feof, Py_ssize_tP,
-    cpython_struct)
+    cpython_struct, is_valid_fp)
 from pypy.module.cpyext.pyobject import PyObject, borrow_from
 from pypy.module.cpyext.pyerrors import PyErr_SetFromErrno
 from pypy.module.cpyext.funcobject import PyCodeObject
@@ -154,6 +154,10 @@
     source = ""
     filename = rffi.charp2str(filename)
     buf = lltype.malloc(rffi.CCHARP.TO, BUF_SIZE, flavor='raw')
+    if not is_valid_fp(fp):
+        PyErr_SetFromErrno(space, space.w_IOError)
+        lltype.free(buf, flavor='raw')
+        return None
     try:
         while True:
             count = fread(buf, 1, BUF_SIZE, fp)
diff --git a/pypy/module/cpyext/test/test_eval.py b/pypy/module/cpyext/test/test_eval.py
--- a/pypy/module/cpyext/test/test_eval.py
+++ b/pypy/module/cpyext/test/test_eval.py
@@ -89,12 +89,12 @@
                 rffi.free_charp(buf)
 
         assert 0 == run("42 * 43")
-        
+
         assert -1 == run("4..3 * 43")
-        
+
         assert api.PyErr_Occurred()
         api.PyErr_Clear()
-        
+
     def test_run_string(self, space, api):
         def run(code, start, w_globals, w_locals):
             buf = rffi.str2charp(code)


More information about the pypy-commit mailing list