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 cec2d86..b14b855 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 a8a4406..8d4ffad 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):