*.c: Use the new `Py_TYPE' and `Py_SIZE' macros; define them if necessary.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 20 Oct 2019 23:19:44 +0000 (00:19 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 11 Apr 2020 11:44:14 +0000 (12:44 +0100)
Python 2.6 introduced macros to access object header fields, which is
handy because Python 3 changes the way object headers are
structured.  (I think the binary layout is unchanged, but the C-level
structuring is definitely different.)

This is the start of Python 3 porting work.

pyke.h

diff --git a/pyke.h b/pyke.h
index 2934543..e6d465e 100644 (file)
--- a/pyke.h
+++ b/pyke.h
 
 PRIVATE_SYMBOLS;
 
+/*----- Python version compatibility hacks --------------------------------*/
+
+/* The handy `Py_TYPE' and `Py_SIZE' macros turned up in 2.6.  Define them if
+ * they're not already here.
+ */
+#ifndef Py_TYPE
+#  define Py_TYPE(obj) (((PyObject *)(obj))->ob_type)
+#endif
+#ifndef Py_SIZE
+#  define Py_SIZE(obj) (((PyVarObject *)(obj))->ob_size)
+#endif
+
 /*----- Utilities for returning values and exceptions ---------------------*/
 
 /* Returning values. */
@@ -153,8 +165,7 @@ extern PyObject *getulong(unsigned long); /* any kind of unsigned integer */
 
 /*----- Miscellaneous utilities -------------------------------------------*/
 
-#define FREEOBJ(obj)                                                   \
-  (((PyObject *)(obj))->ob_type->tp_free((PyObject *)(obj)))
+#define FREEOBJ(obj) (Py_TYPE(obj)->tp_free((PyObject *)(obj)))
   /* Actually free OBJ, e.g., in a deallocation function. */
 
 extern PyObject *abstract_pynew(PyTypeObject *, PyObject *, PyObject *);