Blame SOURCES/python-nss-file-like-read.patch

4f8d33
fix read_data_from_file(), make it accept any file like object
4f8d33
4f8d33
read_data_from_file() was supposed to accept either a string
4f8d33
representing a filename to open or a file object on which it will
4f8d33
call read() to load the contents. However the test for a file object
4f8d33
was too restrictive, it literally checked for a file object which
4f8d33
excluded objects supporting the file interface
4f8d33
(e.g. StringIO). Therefore the test was changed to test if the object
4f8d33
has a read() method.
4f8d33
4f8d33
diff -r -u python-nss-0.16.0.orig/src/py_nss.c python-nss-0.16.0/src/py_nss.c
4f8d33
--- python-nss-0.16.0.orig/src/py_nss.c	2014-10-23 19:15:12.000000000 -0400
4f8d33
+++ python-nss-0.16.0/src/py_nss.c	2015-05-26 17:16:50.373886276 -0400
4f8d33
@@ -1796,6 +1796,20 @@
4f8d33
     return py_sec_item;
4f8d33
 }
4f8d33
 
4f8d33
+static bool
4f8d33
+pyobject_has_method(PyObject* obj, const char *method_name)
4f8d33
+{
4f8d33
+    PyObject *attr;
4f8d33
+    int is_callable;
4f8d33
+
4f8d33
+    if ((attr = PyObject_GetAttrString(obj, method_name)) == NULL) {
4f8d33
+        return false;
4f8d33
+    }
4f8d33
+    is_callable = PyCallable_Check(attr);
4f8d33
+    Py_DECREF(attr);
4f8d33
+    return is_callable ? true : false;
4f8d33
+}
4f8d33
+
4f8d33
 /*
4f8d33
  * read_data_from_file(PyObject *file_arg)
4f8d33
  *
4f8d33
@@ -1819,11 +1833,11 @@
4f8d33
         if ((py_file = PyFile_FromString(PyString_AsString(file_arg), "r")) == NULL) {
4f8d33
             return NULL;
4f8d33
         }
4f8d33
-    } else if (PyFile_Check(file_arg)) {
4f8d33
+    } else if (pyobject_has_method(file_arg, "read")) {
4f8d33
         py_file = file_arg;
4f8d33
 	Py_INCREF(py_file);
4f8d33
     } else {
4f8d33
-        PyErr_SetString(PyExc_TypeError, "Bad file, must be pathname or file object");
4f8d33
+        PyErr_SetString(PyExc_TypeError, "Bad file, must be pathname or file like object with read() method");
4f8d33
         return NULL;
4f8d33
     }
4f8d33
 
4f8d33
Only in python-nss-0.16.0/src: py_nss.c~