summaryrefslogtreecommitdiff
path: root/pythonmod/pythonmod.c
diff options
context:
space:
mode:
authorDag-Erling Smørgrav <des@FreeBSD.org>2019-06-30 14:56:56 +0000
committerDag-Erling Smørgrav <des@FreeBSD.org>2019-06-30 14:56:56 +0000
commit4713c21a1ac91081e50e474d11fcec002b43a562 (patch)
tree3dee8846d50565e925146b25f6b761ceb1ea7b07 /pythonmod/pythonmod.c
parent9c9d011eed674ddd7e4a0a148691887afb9e75cd (diff)
Notes
Diffstat (limited to 'pythonmod/pythonmod.c')
-rw-r--r--pythonmod/pythonmod.c23
1 files changed, 18 insertions, 5 deletions
diff --git a/pythonmod/pythonmod.c b/pythonmod/pythonmod.c
index a668ecc23cc70..9009a28daaa06 100644
--- a/pythonmod/pythonmod.c
+++ b/pythonmod/pythonmod.c
@@ -247,6 +247,9 @@ int pythonmod_init(struct module_env* env, int id)
PyObject* py_init_arg, *res;
PyGILState_STATE gil;
int init_standard = 1;
+#if PY_MAJOR_VERSION < 3
+ PyObject* PyFileObject = NULL;
+#endif
struct pythonmod_env* pe = (struct pythonmod_env*)calloc(1, sizeof(struct pythonmod_env));
if (!pe)
@@ -307,7 +310,15 @@ int pythonmod_init(struct module_env* env, int id)
}
/* Check Python file load */
- if ((script_py = fopen(pe->fname, "r")) == NULL)
+ /* uses python to open the file, this works on other platforms,
+ * eg. Windows, to open the file in the correct mode for python */
+#if PY_MAJOR_VERSION < 3
+ PyFileObject = PyFile_FromString((char*)pe->fname, "r");
+ script_py = PyFile_AsFile(PyFileObject);
+#else
+ script_py = _Py_fopen(pe->fname, "r");
+#endif
+ if (script_py == NULL)
{
log_err("pythonmod: can't open file %s for reading", pe->fname);
PyGILState_Release(gil);
@@ -343,7 +354,11 @@ int pythonmod_init(struct module_env* env, int id)
PyGILState_Release(gil);
return 0;
}
+#if PY_MAJOR_VERSION < 3
+ Py_XDECREF(PyFileObject);
+#else
fclose(script_py);
+#endif
if ((pe->func_init = PyDict_GetItemString(pe->dict, "init_standard")) == NULL)
{
@@ -517,8 +532,7 @@ void pythonmod_clear(struct module_qstate* qstate, int id)
return;
pq = (struct pythonmod_qstate*)qstate->minfo[id];
- verbose(VERB_ALGO, "pythonmod: clear, id: %d, pq:%lX", id,
- (unsigned long int)pq);
+ verbose(VERB_ALGO, "pythonmod: clear, id: %d, pq:%p", id, pq);
if(pq != NULL)
{
PyGILState_STATE gil = PyGILState_Ensure();
@@ -534,8 +548,7 @@ void pythonmod_clear(struct module_qstate* qstate, int id)
size_t pythonmod_get_mem(struct module_env* env, int id)
{
struct pythonmod_env* pe = (struct pythonmod_env*)env->modinfo[id];
- verbose(VERB_ALGO, "pythonmod: get_mem, id: %d, pe:%lX", id,
- (unsigned long int)pe);
+ verbose(VERB_ALGO, "pythonmod: get_mem, id: %d, pe:%p", id, pe);
if(!pe)
return 0;
return sizeof(*pe);