5#define PY_ONLY(args...) args
7#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
9#define PY_ARRAY_UNIQUE_SYMBOL _pcbenv2_module_PyArray_API
10#include <numpy/ndarrayobject.h>
12#include "Geometry.hpp"
19template<
typename Ty = PyObject>
20inline Ty *ValueError(
const char *s)
22 PyErr_SetString(PyExc_ValueError, s);
25template<
typename Ty = PyObject>
26inline Ty *MemoryError(
const char *s)
28 PyErr_SetString(PyExc_MemoryError, s);
31template<
typename Ty = PyObject>
32inline Ty *Exception(PyObject *exc,
const char *s)
34 PyErr_SetString(exc, s);
37inline PyObject *Exception(
const std::exception &e)
39 PyErr_SetString(PyExc_Exception, e.what());
46inline PyObject *String(
const char *s)
48 return PyUnicode_FromString(s);
50inline PyObject *String(
const std::string &s)
52 return PyUnicode_FromStringAndSize(s.c_str(), s.size());
58inline bool TupleN_Check(PyObject *py, uint n)
60 return PyTuple_Check(py) && PyTuple_Size(py) == n;
62inline bool TupleMin_Check(PyObject *py, uint n)
64 return PyTuple_Check(py) && PyTuple_Size(py) >= n;
67inline double Number_AsDouble(PyObject *py)
69 if (PyFloat_Check(py))
70 return PyFloat_AsDouble(py);
71 auto F = PyNumber_Float(py);
74 return std::numeric_limits<double>::quiet_NaN();
75 auto d = PyFloat_AsDouble(F);
80inline bool String_Check(PyObject *py)
82 return py && PyUnicode_Check(py);
84inline std::string String_AsStdString(PyObject *py)
86#if PY_VERSION_HEX >= 0x03030000
87 return std::string(PyUnicode_AsUTF8(py));
89#error "Python version < 3.3 not supported, go upgrade!"
92inline std::string_view String_AsStringView(PyObject *py)
94#if PY_VERSION_HEX >= 0x03030000
96 const char *ucs = PyUnicode_AsUTF8AndSize(py, &size);
97 return std::string_view(ucs, size);
99#error "Python version < 3.3 not supported, go upgrade!"
103inline std::string String_AsStdString(PyObject *py,
const char *error)
105 if (!String_Check(py))
106 throw std::invalid_argument(error);
107 return String_AsStdString(py);
110inline void Dict_StealItemString(PyObject *dict,
const char *key, PyObject *v)
112 PyDict_SetItemString(dict, key, v);
115inline void Dict_StealItem(PyObject *dict, PyObject *key, PyObject *v)
117 PyDict_SetItem(dict, key, v);
121inline void DECREF_safe(PyObject *v)
127inline PyObject *None()
133inline PyObject *NewRef(PyObject *py)
148 state = PyGILState_Ensure();
152 PyGILState_Release(state);
154 GIL_guard(GIL_guard&& moved) noexcept : state(moved.state)
156 moved.state = PyGILState_UNLOCKED;
158 GIL_guard& operator=(GIL_guard&& moved)
noexcept
160 if (
this != &moved) {
161 PyGILState_Release(state);
163 moved.state = PyGILState_UNLOCKED;
167 GIL_guard(
const GIL_guard&) =
delete;
170 PyGILState_STATE state;
175#include "PyObject.hpp"