| 1 |
--- a/plugins/cfpython/cfpython.c |
| 2 |
+++ b/plugins/cfpython/cfpython.c |
| 3 |
@@ -60,7 +60,6 @@ |
| 4 |
#include <cfpython.h> |
| 5 |
#include <fcntl.h> |
| 6 |
#include <stdarg.h> |
| 7 |
-#include <node.h> |
| 8 |
#include <svnversion.h> |
| 9 |
|
| 10 |
CF_PLUGIN char SvnRevPlugin[] = SVN_REV; |
| 11 |
@@ -790,6 +789,12 @@ |
| 12 |
static PyObject *catcher = NULL; |
| 13 |
|
| 14 |
/** |
| 15 |
+ * A Python object so that we only need to import the io module once. |
| 16 |
+ * The recommended way to load a file to compile in 3.10 and later involves importing the io module, so we really just do that once. |
| 17 |
+ */ |
| 18 |
+static PyObject *io_module = NULL; |
| 19 |
+ |
| 20 |
+/** |
| 21 |
* Trace a Python error to the Crossfire log. |
| 22 |
* This uses code from: |
| 23 |
* http://stackoverflow.com/questions/4307187/how-to-catch-python-stdout-in-c-code |
| 24 |
@@ -877,28 +882,35 @@ |
| 25 |
replace->file = cf_add_string(sh_path); |
| 26 |
} |
| 27 |
|
| 28 |
- /* Load, parse and compile. Note: because Pyhon may have been built with a |
| 29 |
- * different library than Crossfire, the FILE* it uses may be incompatible. |
| 30 |
- * Therefore we use PyFile to open the file, then convert to FILE* and get |
| 31 |
- * Python's own structure. Messy, but can't be helped... */ |
| 32 |
- if (!(scriptfile = cfpython_openpyfile(filename))) { |
| 33 |
- cf_log(llevDebug, "cfpython - The Script file %s can't be opened\n", filename); |
| 34 |
+ /* With the new parser in 3.10, we need to read the file contents into a buffer, and then pass that string to compile it. |
| 35 |
+ * The new parser removes the PyNode functions as well as PyParser_SimpleParseFile, |
| 36 |
+ * so the code needed to be completely rewritten to work. |
| 37 |
+ * |
| 38 |
+ * Python's solution to these changes is to import the io module and use Python's read method to read in the file, |
| 39 |
+ * and then convert the bytes object into a c-string for Py_CompileString |
| 40 |
+ * |
| 41 |
+ * Though, if it is more performant than the previous code, Py_CompileString is |
| 42 |
+ * available for all Python 3, so it is possible to simplify all of them to this if we need to. |
| 43 |
+ */ |
| 44 |
+ if (!io_module) |
| 45 |
+ io_module = PyImport_ImportModule("io"); |
| 46 |
+ scriptfile = PyObject_CallMethod(io_module, "open", "ss", filename, "rb"); |
| 47 |
+ if (!scriptfile) { |
| 48 |
+ cf_log(llevDebug, "CFPython: script file %s can't be opened\n", filename); |
| 49 |
cf_free_string(sh_path); |
| 50 |
return NULL; |
| 51 |
- } else { |
| 52 |
- /* Note: FILE* being opaque, it works, but the actual structure may be different! */ |
| 53 |
- FILE* pyfile = cfpython_pyfile_asfile(scriptfile); |
| 54 |
- if ((n = PyParser_SimpleParseFile(pyfile, filename, Py_file_input))) { |
| 55 |
- replace->code = PyNode_Compile(n, filename); |
| 56 |
- PyNode_Free(n); |
| 57 |
- } |
| 58 |
- |
| 59 |
- if (PyErr_Occurred()) |
| 60 |
- log_python_error(); |
| 61 |
- else |
| 62 |
- replace->cached_time = stat_buf.st_mtime; |
| 63 |
- run = replace; |
| 64 |
} |
| 65 |
+ PyObject *source_bytes = PyObject_CallMethod(scriptfile, "read", ""); |
| 66 |
+ (void)PyObject_CallMethod(scriptfile, "close", ""); |
| 67 |
+ PyObject *code = Py_CompileString(PyBytes_AsString(source_bytes), filename, Py_file_input); |
| 68 |
+ if (code) { |
| 69 |
+ replace->code = (PyCodeObject *)code; |
| 70 |
+ } |
| 71 |
+ if (PyErr_Occurred()) |
| 72 |
+ log_python_error(); |
| 73 |
+ else |
| 74 |
+ replace->cached_time = stat_buf.st_mtime; |
| 75 |
+ run = replace; |
| 76 |
} |
| 77 |
|
| 78 |
cf_free_string(sh_path); |