chiark / gitweb /
utils.pyx (_getfd): Hack around Pyrex exception-handling bugs.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 9 Sep 2017 22:17:47 +0000 (23:17 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sun, 10 Sep 2017 10:28:16 +0000 (11:28 +0100)
It seems that Pyrex has some nasty bugs here.  Most obviously, it fails
to cancel the exception when handling it.  But even then, there's a
code-generation bug around returning in an `except' block which leads to
a null-pointer dereference.

These are Debian #875284 and #875285.

defs.pxi
utils.pyx

index cec2d8654e20278c4468d881c4588c9a064a6a17..b14b8559d27cbc52687bf7c6374d34ea767206b2 100644 (file)
--- a/defs.pxi
+++ b/defs.pxi
@@ -102,6 +102,7 @@ cdef extern from 'Python.h':
   object PyLong_FromUnsignedLong(unsigned long i)
   char *PyString_AS_STRING(string)
   int _PyString_Resize(PyObject **string, int size) except -1
+  void PyErr_Clear()
 
   void Py_INCREF(PyObject *obj)
   void Py_DECREF(PyObject *obj)
index a8a440667cee471823b682cc86fa6c8912c3d568..8d4ffad73268a376eaca2fa3835ac8367e29d942 100644 (file)
--- a/utils.pyx
+++ b/utils.pyx
@@ -40,9 +40,11 @@ cdef object _tobool(int i):
 
 cdef int _getfd(object fdobj):
   try:
-    return fdobj
+    fd = int(fdobj)
   except TypeError:
-    return fdobj.fileno()
+    PyErr_Clear()
+    fd = fdobj.fileno()
+  return fd
 
 cdef object _checkcallable(f, what):
   if f is not None and not callable(f):