forked from albertz/music-player-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyUtils.h
59 lines (49 loc) · 1.26 KB
/
PyUtils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef MP_PYUTILS_HPP
#define MP_PYUTILS_HPP
#include <Python.h>
#ifdef __cplusplus
extern "C" {
#endif
// this is mostly safe to call.
// returns a newly allocated c-string.
// doesn't need PyGIL
char* objStrDup(PyObject* obj);
// returns a newly allocated c-string.
// doesn't need PyGIL
char* objAttrStrDup(PyObject* obj, const char* attrStr);
#ifdef __cplusplus
}
#include <string>
// mostly safe, for debugging, dont need PyGIL
std::string objAttrStr(PyObject* obj, const std::string& attrStr);
std::string objStr(PyObject* obj);
// more correct. needs PyGIL
static inline
bool pyStr(PyObject* obj, std::string& str) {
if(!obj) return false;
if(PyString_Check(obj)) {
str = std::string(PyString_AS_STRING(obj), PyString_GET_SIZE(obj));
return true;
}
else if(PyUnicode_Check(obj)) {
PyObject* strObj = PyUnicode_AsUTF8String(obj);
if(!strObj) return false;
bool res = false;
if(PyString_Check(strObj))
res = pyStr(strObj, str);
Py_DECREF(strObj);
return res;
}
else {
PyObject* unicodeObj = PyObject_Unicode(obj);
if(!unicodeObj) return false;
bool res = false;
if(PyString_Check(unicodeObj) || PyUnicode_Check(unicodeObj))
res = pyStr(unicodeObj, str);
Py_DECREF(unicodeObj);
return res;
}
return false;
}
#endif
#endif // PYUTILS_HPP