helpers.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #pragma once
  2. #include <Python.h>
  3. namespace NCYson {
  4. constexpr bool PY3 = PY_MAJOR_VERSION == 3;
  5. constexpr bool PY2 = PY_MAJOR_VERSION == 2;
  6. void SetPrettyTypeError(PyObject*, const char*);
  7. PyObject* ConvertPyStringToPyBytes(PyObject*);
  8. PyObject* GetCharBufferAndOwner(PyObject*, const char**, size_t*);
  9. PyObject* ConvertPyStringToPyNativeString(PyObject*);
  10. PyObject* ConvertPyLongToPyBytes(PyObject*);
  11. inline PyObject* GetSelf(PyObject* self) {
  12. Py_INCREF(self);
  13. return self;
  14. }
  15. namespace NPrivate {
  16. class TPyObjectPtr {
  17. public:
  18. void Reset(PyObject*);
  19. PyObject* GetNew();
  20. PyObject* GetBorrowed();
  21. TPyObjectPtr();
  22. ~TPyObjectPtr();
  23. private:
  24. PyObject* Ptr_;
  25. };
  26. }
  27. }
  28. #if PY_MAJOR_VERSION >= 3
  29. #define GenericCheckBuffer PyObject_CheckBuffer
  30. #define PyFile_CheckExact(x) 0
  31. #define PyFile_AsFile(x) (FILE*)(PyErr_Format(PyExc_NotImplementedError, "PyFile_AsFile not implemented for Python3"))
  32. #else
  33. #define GenericCheckBuffer PyObject_CheckReadBuffer
  34. #endif
  35. #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_SIZE)
  36. static inline void _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)
  37. { ob->ob_size = size; }
  38. #define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject*)(ob), size)
  39. #endif