forked from tayral/cpp2py
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring.hpp
More file actions
32 lines (22 loc) · 1020 Bytes
/
string.hpp
File metadata and controls
32 lines (22 loc) · 1020 Bytes
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
#pragma once
//#include <string>
namespace cpp2py {
template <> struct py_converter<std::string> {
static PyObject *c2py(std::string const &x) { return PyString_FromString(x.c_str()); }
static std::string py2c(PyObject *ob) { return PyString_AsString(ob); }
static bool is_convertible(PyObject *ob, bool raise_exception) {
if (PyString_Check(ob) or PyUnicode_Check(ob)) return true;
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to string"); }
return false;
}
};
template <> struct py_converter<const char *> {
static PyObject *c2py(const char *x) { return PyString_FromString(x); }
static const char *py2c(PyObject *ob) { return PyString_AsString(ob); }
static bool is_convertible(PyObject *ob, bool raise_exception) {
if (PyString_Check(ob) or PyUnicode_Check(ob)) return true;
if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Cannot convert to string"); }
return false;
}
};
} // namespace cpp2py