*.c: Introduce a new input conversion for binary strings.
[pyke] / pyke.h
diff --git a/pyke.h b/pyke.h
index 2934543..984b5d5 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
+
+/* Python 3 added internal structure to the various object headers, and
+ * defined a new macro `PyVarObject_HEAD_INIT' to initialize variable-length
+ * static instances correctly.  Define it if it's not already here.
+ */
+#ifndef PyVarObject_HEAD_INIT
+#  define PyVarObject_HEAD_INIT(super, sz) PyObject_HEAD_INIT(super) sz,
+#endif
+
+/* Python 3.2 changed the type of hash values, so paper over this annoying
+ * difference.
+ */
+#if PY_VERSION_HEX < 0x03020000
+  typedef long Py_hash_t;
+#endif
+
 /*----- Utilities for returning values and exceptions ---------------------*/
 
 /* Returning values. */
@@ -142,10 +169,12 @@ extern void restore_exception(struct excinfo *, const char *, ...);
 /* Input conversion functions for standard kinds of objects, with overflow
  * checking where applicable.
  */
+struct bin { const void *p; Py_ssize_t sz; };
 extern int convulong(PyObject *, void *); /* unsigned long */
 extern int convuint(PyObject *, void *); /* unsigned int */
 extern int convszt(PyObject *, void *);        /* size_t */
 extern int convbool(PyObject *, void *); /* bool */
+extern int convbin(PyObject *, void *); /* read buffer holding bytes */
 
 /* Output conversions. */
 extern PyObject *getbool(int);         /* bool */
@@ -153,8 +182,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 *);