|
|
2ff057 |
#ifndef H_SYSTEM_PYTHON
|
|
|
2ff057 |
#define H_SYSTEM_PYTHON
|
|
|
2ff057 |
|
|
|
2ff057 |
#if defined(__APPLE__)
|
|
|
2ff057 |
#include <sys/types.h>
|
|
|
2ff057 |
#endif
|
|
|
2ff057 |
|
|
|
2ff057 |
#define PY_SSIZE_T_CLEAN
|
|
|
2ff057 |
#include <Python.h>
|
|
|
2ff057 |
#include <structmember.h>
|
|
|
2ff057 |
|
|
|
2ff057 |
/* For Python 3, use the PyLong type throughout in place of PyInt */
|
|
|
2ff057 |
#if PY_MAJOR_VERSION >= 3
|
|
|
2ff057 |
#define PyInt_Check PyLong_Check
|
|
|
2ff057 |
#define PyInt_AsLong PyLong_AsLong
|
|
|
2ff057 |
#define PyInt_FromLong PyLong_FromLong
|
|
|
2ff057 |
#define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask
|
|
|
2ff057 |
#define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask
|
|
|
2ff057 |
#define PyInt_AsSsize_t PyLong_AsSsize_t
|
|
|
2ff057 |
#endif
|
|
|
2ff057 |
|
|
Panu Matilainen |
8416ed |
PyObject * fakedecode;
|
|
Panu Matilainen |
8416ed |
|
|
Panu Matilainen |
1bf03d |
static inline PyObject * utf8FromString(const char *s)
|
|
Panu Matilainen |
1bf03d |
{
|
|
Panu Matilainen |
420bed |
/* In Python 3, we return all strings as surrogate-escaped utf-8 */
|
|
Panu Matilainen |
420bed |
#if PY_MAJOR_VERSION >= 3
|
|
Panu Matilainen |
8416ed |
if (s != NULL) {
|
|
Panu Matilainen |
8416ed |
PyObject *o = PyUnicode_DecodeUTF8(s, strlen(s), "surrogateescape");
|
|
Panu Matilainen |
8416ed |
/* fish the fake decode function from python side if not done yet */
|
|
Panu Matilainen |
8416ed |
if (fakedecode == NULL) {
|
|
Panu Matilainen |
8416ed |
PyObject *n = PyUnicode_FromString("rpm");
|
|
Panu Matilainen |
8416ed |
PyObject *m = PyImport_Import(n);
|
|
Panu Matilainen |
8416ed |
PyObject *md = PyModule_GetDict(m);
|
|
Panu Matilainen |
8416ed |
fakedecode = PyDict_GetItemString(md, "_fakedecode");
|
|
Panu Matilainen |
8416ed |
Py_DECREF(m);
|
|
Panu Matilainen |
8416ed |
Py_DECREF(n);
|
|
Panu Matilainen |
8416ed |
}
|
|
Panu Matilainen |
8416ed |
if (fakedecode && o) {
|
|
Panu Matilainen |
8416ed |
/* monkey-patch it into the string object as "decode" */
|
|
Panu Matilainen |
8416ed |
PyDict_SetItemString(Py_TYPE(o)->tp_dict, "decode", fakedecode);
|
|
Panu Matilainen |
8416ed |
}
|
|
Panu Matilainen |
8416ed |
return o;
|
|
Panu Matilainen |
8416ed |
}
|
|
Panu Matilainen |
420bed |
#else
|
|
Panu Matilainen |
1bf03d |
if (s != NULL)
|
|
Panu Matilainen |
1bf03d |
return PyBytes_FromString(s);
|
|
Panu Matilainen |
420bed |
#endif
|
|
Panu Matilainen |
1bf03d |
Py_RETURN_NONE;
|
|
Panu Matilainen |
1bf03d |
}
|
|
Panu Matilainen |
420bed |
|
|
|
2ff057 |
#endif /* H_SYSTEM_PYTHON */
|