summaryrefslogtreecommitdiff
path: root/SWIG/_py3k_compat.i
diff options
context:
space:
mode:
Diffstat (limited to 'SWIG/_py3k_compat.i')
-rw-r--r--SWIG/_py3k_compat.i43
1 files changed, 43 insertions, 0 deletions
diff --git a/SWIG/_py3k_compat.i b/SWIG/_py3k_compat.i
new file mode 100644
index 0000000..7c90bb4
--- /dev/null
+++ b/SWIG/_py3k_compat.i
@@ -0,0 +1,43 @@
+%{
+#if PY_MAJOR_VERSION >= 3
+
+FILE* PyFile_AsFile(PyObject *pyfile) {
+ FILE* fp;
+ int fd;
+ const char *mode_str = NULL;
+ PyObject *mode_obj;
+
+ if ((fd = PyObject_AsFileDescriptor(pyfile)) == -1) {
+ PyErr_SetString(PyExc_BlockingIOError,
+ "Cannot find file handler for the Python file!");
+ return NULL;
+ }
+
+ if ((mode_obj = PyObject_GetAttrString(pyfile, "mode")) == NULL) {
+ mode_str = "rb";
+ PyErr_Clear();
+ }
+ else {
+ /* convert to plain string
+ * note that error checking is embedded in the function
+ */
+ mode_str = PyUnicode_AsUTF8AndSize(mode_obj, NULL);
+ }
+
+ if((fp = fdopen(fd, mode_str)) == NULL) {
+ PyErr_SetFromErrno(PyExc_IOError);
+ }
+
+ Py_XDECREF(mode_obj);
+ return fp;
+}
+
+#else /* PY2K */
+
+#define PyLong_FromLong(x) PyInt_FromLong(x)
+#define PyUnicode_AsUTF8(x) PyString_AsString(x)
+#define PyUnicode_FromString(x) PyString_FromString(x)
+#define PyUnicode_Format(x, y) PyString_Format(x, y)
+
+#endif /* PY_MAJOR_VERSION */
+%}