Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions py3c/py3c/comparison.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@
return Py_INCREF(Py_NotImplemented), Py_NotImplemented
#endif

#ifndef Py_UNREACHABLE
#define Py_UNREACHABLE() abort()
#endif

#ifndef Py_RETURN_RICHCOMPARE
#define Py_RETURN_RICHCOMPARE(val1, val2, op) \
do { \
switch (op) { \
case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
default: \
Py_UNREACHABLE(); \
} \
} while (0)
#endif

#define PY3C_RICHCMP(val1, val2, op) \
((op) == Py_EQ) ? PyBool_FromLong((val1) == (val2)) : \
((op) == Py_NE) ? PyBool_FromLong((val1) != (val2)) : \
Expand Down
6 changes: 3 additions & 3 deletions py3c/py3c/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#define PyStr_InternFromString PyUnicode_InternFromString
#define PyStr_Decode PyUnicode_Decode

#define PyStr_AsUTF8String PyUnicode_AsUTF8String // returns PyBytes
#define PyStr_AsUTF8String PyUnicode_AsUTF8String /* returns PyBytes */
#define PyStr_AsUTF8 PyUnicode_AsUTF8
#define PyStr_AsUTF8AndSize PyUnicode_AsUTF8AndSize

Expand Down Expand Up @@ -74,11 +74,11 @@
#define PyStr_Decode PyString_Decode

#ifdef __GNUC__
static PyObject * PyStr_Concat(PyObject *left, PyObject *right) __attribute__ ((unused));
static PyObject *PyStr_Concat(PyObject *left, PyObject *right) __attribute__ ((unused));
#endif
static PyObject *PyStr_Concat(PyObject *left, PyObject *right) {
PyObject *str = left;
Py_INCREF(left); // reference to old left will be stolen
Py_INCREF(left); /* reference to old left will be stolen */
PyString_Concat(&str, right);
if (str) {
return str;
Expand Down
17 changes: 16 additions & 1 deletion py3c/py3c/py3shims.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

/*
* Shims for the PyMem_Raw* functions added inPython 3.3
* Shims for new functionality from in Python 3.3+
*
* See https://docs.python.org/3/c-api/memory.html#raw-memory-interface
*/
Expand All @@ -14,13 +14,28 @@
#include <stdlib.h>


/* Py_UNUSED - added in Python 3.4, documneted in 3.7 */

#ifndef Py_UNUSED
#ifdef __GNUC__
#define Py_UNUSED(name) _unused_ ## name __attribute__((unused))
#else
#define Py_UNUSED(name) _unused_ ## name
#endif
#endif


/* PyMem_Raw{Malloc,Realloc,Free} - added in Python 3.4 */

#if PY_MAJOR_VERSION < 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 4)
#define PyMem_RawMalloc(n) malloc((n) || 1)
#define PyMem_RawRealloc(p, n) realloc(p, (n) || 1)
#define PyMem_RawFree(p) free(p)
#endif /* version < 3.4 */


/* PyMem_RawCalloc - added in Python 3.5 */

#if PY_MAJOR_VERSION < 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION < 5)
#define PyMem_RawCalloc(n, s) calloc((n) || 1, (s) || 1)
#endif /* version < 3.5 */
Expand Down