-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a780018
commit 10946df
Showing
14 changed files
with
239 additions
and
229 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.10.0 | ||
0.11.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* SPDX-License-Identifier: MIT */ | ||
/* Copyright © 2020 Max Bachmann */ | ||
|
||
#define PY_SSIZE_T_CLEAN | ||
#include <Python.h> | ||
#include <nonstd/string_view.hpp> | ||
#include <variant/variant.hpp> | ||
|
||
bool valid_str(PyObject* str, const char* name) { | ||
if (!PyObject_TypeCheck(str, &PyString_Type) | ||
&& !PyObject_TypeCheck(str, &PyUnicode_Type)) | ||
{ | ||
PyErr_Format(PyExc_TypeError, "%s must be a String, Unicode or None", name); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
#define PY_INIT_MOD(name, doc, methods) \ | ||
PyMODINIT_FUNC init##name(void) { \ | ||
Py_InitModule3(#name, methods, doc); \ | ||
} | ||
|
||
using python_string_view = mpark::variant< | ||
nonstd::basic_string_view<uint8_t>, | ||
nonstd::basic_string_view<Py_UNICODE> | ||
>; | ||
|
||
python_string_view decode_python_string(PyObject* py_str) { | ||
if (PyObject_TypeCheck(py_str, &PyString_Type)) { | ||
Py_ssize_t len = PyString_GET_SIZE(py_str); | ||
uint8_t* str = reinterpret_cast<uint8_t*>(PyString_AS_STRING(py_str)); | ||
return nonstd::basic_string_view<uint8_t>(str, len); | ||
|
||
} else { | ||
Py_ssize_t len = PyUnicode_GET_SIZE(py_str); | ||
Py_UNICODE* str = PyUnicode_AS_UNICODE(py_str); | ||
return nonstd::basic_string_view<Py_UNICODE>(str, len); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* SPDX-License-Identifier: MIT */ | ||
/* Copyright © 2020 Max Bachmann */ | ||
|
||
#define PY_SSIZE_T_CLEAN | ||
#include <Python.h> | ||
#include <nonstd/string_view.hpp> | ||
#include <variant/variant.hpp> | ||
|
||
#if PY_VERSION_HEX < 0x030C0000 | ||
#define PY_BELOW_3_12 | ||
#endif | ||
|
||
bool valid_str(PyObject* str, const char* name) { | ||
if (!PyUnicode_Check(str)) { | ||
PyErr_Format(PyExc_TypeError, "%s must be a String or None", name); | ||
return false; | ||
} | ||
|
||
// PyUnicode_READY deprecated in Python 3.10 removed in Python 3.12 | ||
#ifdef PY_BELOW_3_12 | ||
if (PyUnicode_READY(str)) { | ||
return false; | ||
} | ||
#endif | ||
|
||
return true; | ||
} | ||
|
||
#define PY_INIT_MOD(name, doc, methods) \ | ||
static struct PyModuleDef moduledef = { \ | ||
PyModuleDef_HEAD_INIT, #name, doc, -1, methods, NULL, NULL, NULL, NULL}; \ | ||
PyMODINIT_FUNC PyInit_##name(void) { \ | ||
return PyModule_Create(&moduledef); \ | ||
} | ||
|
||
using python_string_view = mpark::variant< | ||
nonstd::basic_string_view<uint8_t>, | ||
nonstd::basic_string_view<uint16_t>, | ||
nonstd::basic_string_view<uint32_t> | ||
>; | ||
|
||
python_string_view decode_python_string(PyObject* py_str) { | ||
Py_ssize_t len = PyUnicode_GET_LENGTH(py_str); | ||
void* str = PyUnicode_DATA(py_str); | ||
|
||
int str_kind = PyUnicode_KIND(py_str); | ||
|
||
switch (str_kind) { | ||
case PyUnicode_1BYTE_KIND: | ||
return nonstd::basic_string_view<uint8_t>(static_cast<uint8_t*>(str), len); | ||
case PyUnicode_2BYTE_KIND: | ||
return nonstd::basic_string_view<uint16_t>(static_cast<uint16_t*>(str), len); | ||
default: | ||
return nonstd::basic_string_view<uint32_t>(static_cast<uint32_t*>(str), len); | ||
} | ||
} |
Oops, something went wrong.