From fc968c9e70271507368dc5df5a50321cc00aef0d Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Tue, 12 May 2020 20:05:26 -0400 Subject: [PATCH] Handle changes to `PyTypeObject` in Python 3.8. --- .github/workflows/linux-ci.yml | 10 +++++ .../FeatureLib/pfPython/cyPythonInterface.cpp | 40 +------------------ Sources/Plasma/FeatureLib/pfPython/pyEnum.cpp | 6 ++- .../FeatureLib/pfPython/pyGlueHelpers.h | 18 +++++++-- 4 files changed, 30 insertions(+), 44 deletions(-) diff --git a/.github/workflows/linux-ci.yml b/.github/workflows/linux-ci.yml index a27199ed12..1a3a99e7aa 100644 --- a/.github/workflows/linux-ci.yml +++ b/.github/workflows/linux-ci.yml @@ -4,8 +4,18 @@ on: [push, pull_request] jobs: build: runs-on: ubuntu-latest + strategy: + matrix: + python-version: [3.7, 3.8] + steps: - uses: actions/checkout@v2 + + - name: Setup Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies run: | sudo apt update diff --git a/Sources/Plasma/FeatureLib/pfPython/cyPythonInterface.cpp b/Sources/Plasma/FeatureLib/pfPython/cyPythonInterface.cpp index 993d103620..f1c6b2d1c9 100644 --- a/Sources/Plasma/FeatureLib/pfPython/cyPythonInterface.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/cyPythonInterface.cpp @@ -946,45 +946,7 @@ PYTHON_START_METHODS_TABLE(ptImportHook) PYTHON_METHOD(ptImportHook, load_module, "Params: module_name \\nReturns the module given by module_name, if it exists in python.pak"), PYTHON_END_METHODS_TABLE; -PYTHON_TYPE_START(ptImportHook) - "Plasma.ptImportHook", - sizeof(ptImportHook), /* tp_basicsize */ - 0, /* tp_itemsize */ - (destructor)ptImportHook_dealloc, /* tp_dealloc */ - 0, /* tp_print */ - 0, /* tp_getattr */ - 0, /* tp_setattr */ - 0, /* tp_as_async */ - 0, /* tp_repr */ - 0, /* tp_as_number */ - 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ - 0, /* tp_hash */ - 0, /* tp_call */ - 0, /* tp_str */ - 0, /* tp_getattro */ - 0, /* tp_setattro */ - 0, /* tp_as_buffer */ - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ - "PEP 302 Import Hook", /* tp_doc */ - 0, /* tp_traverse */ - 0, /* tp_clear */ - 0, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - 0, /* tp_iter */ - 0, /* tp_iternext */ - PYTHON_DEFAULT_METHODS_TABLE(ptImportHook), /* tp_methods */ - 0, /* tp_members */ - 0, /* tp_getset */ - 0, /* tp_base */ - 0, /* tp_dict */ - 0, /* tp_descr_get */ - 0, /* tp_descr_set */ - 0, /* tp_dictoffset */ - PYTHON_DEFAULT_INIT(ptImportHook), /* tp_init */ - 0, /* tp_alloc */ - ptImportHook_new /* tp_new */ -PYTHON_TYPE_END; +PLASMA_DEFAULT_TYPE(ptImportHook, "PEP 302 Import Hook"); void ptImportHook_AddPlasmaClasses(PyObject* m) { diff --git a/Sources/Plasma/FeatureLib/pfPython/pyEnum.cpp b/Sources/Plasma/FeatureLib/pfPython/pyEnum.cpp index 4cf4aa8c15..114b3a8657 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pyEnum.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/pyEnum.cpp @@ -372,7 +372,7 @@ PYTHON_TYPE_START(EnumValue) sizeof(EnumValue), /* tp_basicsize */ 0, /* tp_itemsize */ (destructor)EnumValue_dealloc, /* tp_dealloc */ - EnumValue_print, /* tp_print */ + PYTHON_TP_PRINT_OR_VECTORCALL_OFFSET(EnumValue_print), 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_as_async */ @@ -416,6 +416,7 @@ PYTHON_TYPE_START(EnumValue) 0, /* tp_del */ 0, /* tp_version_tag */ 0, /* tp_finalize */ + PYTHON_TP_VECTORCALL_PRINT(EnumValue_print) PYTHON_TYPE_END; bool IsEnumValue(PyObject *obj) @@ -582,7 +583,7 @@ PYTHON_TYPE_START(Enum) sizeof(Enum), /* tp_basicsize */ 0, /* tp_itemsize */ (destructor)Enum_dealloc, /* tp_dealloc */ - 0, /* tp_print */ + PYTHON_TP_PRINT_OR_VECTORCALL_OFFSET(0), 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_compare */ @@ -627,6 +628,7 @@ PYTHON_TYPE_START(Enum) 0, /* tp_del */ 0, /* tp_version_tag */ 0, /* tp_finalize */ + PYTHON_TP_VECTORCALL_PRINT(0) PYTHON_TYPE_END; // creates and sets up the enum base class diff --git a/Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.h b/Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.h index 080a4f4ef2..c594924a38 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.h +++ b/Sources/Plasma/FeatureLib/pfPython/pyGlueHelpers.h @@ -176,6 +176,15 @@ int pythonClassName##___init__(pythonClassName *self, PyObject *args, PyObject * // message table default name #define PYTHON_DEFAULT_METHODS_TABLE(pythonClassName) pythonClassName##_methods +// tp_print moved to the end, and two new vectorcall fields inserted in Python 3.8... +#if (PY_MAJOR_VERSION >= 4) || ((PY_MAJOR_VERSION == 3) && (PY_MINOR_VERSION >= 8)) +# define PYTHON_TP_PRINT_OR_VECTORCALL_OFFSET(tp_print) 0 +# define PYTHON_TP_VECTORCALL_PRINT(tp_print) 0, tp_print, +#else +# define PYTHON_TP_PRINT_OR_VECTORCALL_OFFSET(tp_print) tp_print +# define PYTHON_TP_VECTORCALL_PRINT(tp_print) +#endif + // most glue classes can get away with this default structure #define PLASMA_DEFAULT_TYPE(pythonClassName, docString) \ PYTHON_TYPE_START(pythonClassName) \ @@ -183,7 +192,7 @@ PYTHON_TYPE_START(pythonClassName) \ sizeof(pythonClassName), /* tp_basicsize */ \ 0, /* tp_itemsize */ \ PYTHON_DEFAULT_DEALLOC(pythonClassName), /* tp_dealloc */ \ - 0, /* tp_print */ \ + PYTHON_TP_PRINT_OR_VECTORCALL_OFFSET(0), \ 0, /* tp_getattr */ \ 0, /* tp_setattr */ \ 0, /* tp_as_async */ \ @@ -226,6 +235,7 @@ PYTHON_TYPE_START(pythonClassName) \ 0, /* tp_del */ \ 0, /* tp_version_tag */ \ 0, /* tp_finalize */ \ + PYTHON_TP_VECTORCALL_PRINT(0) \ PYTHON_TYPE_END // default compare/rich compare function name @@ -261,7 +271,7 @@ PYTHON_TYPE_END sizeof(pythonClassName), /* tp_basicsize */ \ 0, /* tp_itemsize */ \ PYTHON_DEFAULT_DEALLOC(pythonClassName), /* tp_dealloc */ \ - 0, /* tp_print */ \ + PYTHON_TP_PRINT_OR_VECTORCALL_OFFSET(0), \ 0, /* tp_getattr */ \ 0, /* tp_setattr */ \ pythonClassName##_COMPARE, /* tp_compare */ \ @@ -304,6 +314,7 @@ PYTHON_TYPE_END 0, /* tp_del */ \ 0, /* tp_version_tag */ \ 0, /* tp_finalize */ \ + PYTHON_TP_VECTORCALL_PRINT(0) \ PYTHON_TYPE_END // for conviencence when we just need a base class @@ -313,7 +324,7 @@ PYTHON_TYPE_START(pythonClassName) \ sizeof(pythonClassName), /* tp_basicsize */ \ 0, /* tp_itemsize */ \ PYTHON_DEFAULT_DEALLOC(pythonClassName), /* tp_dealloc */ \ - 0, /* tp_print */ \ + PYTHON_TP_PRINT_OR_VECTORCALL_OFFSET(0), \ 0, /* tp_getattr */ \ 0, /* tp_setattr */ \ 0, /* tp_compare */ \ @@ -356,6 +367,7 @@ PYTHON_TYPE_START(pythonClassName) \ 0, /* tp_del */ \ 0, /* tp_version_tag */ \ 0, /* tp_finalize */ \ + PYTHON_TP_VECTORCALL_PRINT(0) \ PYTHON_TYPE_END // small macros so that the type object can be accessed outside the glue file (for subclassing)