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