Skip to content

Commit c0712ec

Browse files
committed
reformat sources
1 parent 10946df commit c0712ec

File tree

6 files changed

+646
-605
lines changed

6 files changed

+646
-605
lines changed

src/py2_utils.hpp

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,34 @@
66
#include <nonstd/string_view.hpp>
77
#include <variant/variant.hpp>
88

9-
bool valid_str(PyObject* str, const char* name) {
10-
if (!PyObject_TypeCheck(str, &PyString_Type)
11-
&& !PyObject_TypeCheck(str, &PyUnicode_Type))
12-
{
13-
PyErr_Format(PyExc_TypeError, "%s must be a String, Unicode or None", name);
14-
return false;
15-
}
16-
return true;
9+
bool valid_str(PyObject* str, const char* name)
10+
{
11+
if (!PyObject_TypeCheck(str, &PyString_Type) && !PyObject_TypeCheck(str, &PyUnicode_Type)) {
12+
PyErr_Format(PyExc_TypeError, "%s must be a String, Unicode or None", name);
13+
return false;
14+
}
15+
return true;
1716
}
1817

19-
#define PY_INIT_MOD(name, doc, methods) \
20-
PyMODINIT_FUNC init##name(void) { \
21-
Py_InitModule3(#name, methods, doc); \
22-
}
18+
#define PY_INIT_MOD(name, doc, methods) \
19+
PyMODINIT_FUNC init##name(void) \
20+
{ \
21+
Py_InitModule3(#name, methods, doc); \
22+
}
2323

24-
using python_string_view = mpark::variant<
25-
nonstd::basic_string_view<uint8_t>,
26-
nonstd::basic_string_view<Py_UNICODE>
27-
>;
24+
using python_string_view =
25+
mpark::variant<nonstd::basic_string_view<uint8_t>, nonstd::basic_string_view<Py_UNICODE>>;
2826

29-
python_string_view decode_python_string(PyObject* py_str) {
30-
if (PyObject_TypeCheck(py_str, &PyString_Type)) {
31-
Py_ssize_t len = PyString_GET_SIZE(py_str);
32-
uint8_t* str = reinterpret_cast<uint8_t*>(PyString_AS_STRING(py_str));
33-
return nonstd::basic_string_view<uint8_t>(str, len);
34-
35-
} else {
36-
Py_ssize_t len = PyUnicode_GET_SIZE(py_str);
37-
Py_UNICODE* str = PyUnicode_AS_UNICODE(py_str);
38-
return nonstd::basic_string_view<Py_UNICODE>(str, len);
39-
}
27+
python_string_view decode_python_string(PyObject* py_str)
28+
{
29+
if (PyObject_TypeCheck(py_str, &PyString_Type)) {
30+
Py_ssize_t len = PyString_GET_SIZE(py_str);
31+
uint8_t* str = reinterpret_cast<uint8_t*>(PyString_AS_STRING(py_str));
32+
return nonstd::basic_string_view<uint8_t>(str, len);
33+
}
34+
else {
35+
Py_ssize_t len = PyUnicode_GET_SIZE(py_str);
36+
Py_UNICODE* str = PyUnicode_AS_UNICODE(py_str);
37+
return nonstd::basic_string_view<Py_UNICODE>(str, len);
38+
}
4039
}

src/py3_utils.hpp

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,51 @@
66
#include <nonstd/string_view.hpp>
77
#include <variant/variant.hpp>
88

9-
#if PY_VERSION_HEX < 0x030C0000
10-
#define PY_BELOW_3_12
9+
// PEP 623 deprecates legacy strings and therefor
10+
// deprecates e.g. PyUnicode_READY in Python 3.10
11+
#if PY_VERSION_HEX < 0x030A0000
12+
#define PY_BELOW_3_10
1113
#endif
1214

13-
bool valid_str(PyObject* str, const char* name) {
14-
if (!PyUnicode_Check(str)) {
15-
PyErr_Format(PyExc_TypeError, "%s must be a String or None", name);
16-
return false;
17-
}
18-
19-
// PyUnicode_READY deprecated in Python 3.10 removed in Python 3.12
20-
#ifdef PY_BELOW_3_12
21-
if (PyUnicode_READY(str)) {
22-
return false;
23-
}
15+
bool valid_str(PyObject* str, const char* name)
16+
{
17+
if (!PyUnicode_Check(str)) {
18+
PyErr_Format(PyExc_TypeError, "%s must be a String or None", name);
19+
return false;
20+
}
21+
22+
#ifdef PY_BELOW_3_10
23+
if (PyUnicode_READY(str)) {
24+
return false;
25+
}
2426
#endif
2527

26-
return true;
28+
return true;
2729
}
2830

29-
#define PY_INIT_MOD(name, doc, methods) \
30-
static struct PyModuleDef moduledef = { \
31-
PyModuleDef_HEAD_INIT, #name, doc, -1, methods, NULL, NULL, NULL, NULL}; \
32-
PyMODINIT_FUNC PyInit_##name(void) { \
33-
return PyModule_Create(&moduledef); \
34-
}
35-
36-
using python_string_view = mpark::variant<
37-
nonstd::basic_string_view<uint8_t>,
38-
nonstd::basic_string_view<uint16_t>,
39-
nonstd::basic_string_view<uint32_t>
40-
>;
41-
42-
python_string_view decode_python_string(PyObject* py_str) {
43-
Py_ssize_t len = PyUnicode_GET_LENGTH(py_str);
44-
void* str = PyUnicode_DATA(py_str);
45-
46-
int str_kind = PyUnicode_KIND(py_str);
47-
48-
switch (str_kind) {
49-
case PyUnicode_1BYTE_KIND:
50-
return nonstd::basic_string_view<uint8_t>(static_cast<uint8_t*>(str), len);
51-
case PyUnicode_2BYTE_KIND:
52-
return nonstd::basic_string_view<uint16_t>(static_cast<uint16_t*>(str), len);
53-
default:
54-
return nonstd::basic_string_view<uint32_t>(static_cast<uint32_t*>(str), len);
55-
}
31+
#define PY_INIT_MOD(name, doc, methods) \
32+
static struct PyModuleDef moduledef = { \
33+
PyModuleDef_HEAD_INIT, #name, doc, -1, methods, NULL, NULL, NULL, NULL}; \
34+
PyMODINIT_FUNC PyInit_##name(void) \
35+
{ \
36+
return PyModule_Create(&moduledef); \
37+
}
38+
39+
using python_string_view =
40+
mpark::variant<nonstd::basic_string_view<uint8_t>, nonstd::basic_string_view<uint16_t>,
41+
nonstd::basic_string_view<uint32_t>>;
42+
43+
python_string_view decode_python_string(PyObject* py_str)
44+
{
45+
Py_ssize_t len = PyUnicode_GET_LENGTH(py_str);
46+
void* str = PyUnicode_DATA(py_str);
47+
48+
switch (PyUnicode_KIND(py_str)) {
49+
case PyUnicode_1BYTE_KIND:
50+
return nonstd::basic_string_view<uint8_t>(static_cast<uint8_t*>(str), len);
51+
case PyUnicode_2BYTE_KIND:
52+
return nonstd::basic_string_view<uint16_t>(static_cast<uint16_t*>(str), len);
53+
default:
54+
return nonstd::basic_string_view<uint32_t>(static_cast<uint32_t*>(str), len);
55+
}
5656
}

0 commit comments

Comments
 (0)