1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2023 Google LLC. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 #ifndef PYUPB_PYTHON_H__ 9 #define PYUPB_PYTHON_H__ 10 11 // We restrict ourselves to the limited API, so that a single build can be 12 // ABI-compatible with a wide range of Python versions. 13 // 14 // The build system will define Py_LIMITED_API as appropriate (see BUILD). We 15 // only want to define it for our distribution packages, since we can do some 16 // extra assertions when Py_LIMITED_API is not defined. Also Py_LIMITED_API is 17 // incompatible with Py_DEBUG. 18 19 // #define Py_LIMITED_API <val> // Defined by build system when appropriate. 20 21 #include "Python.h" 22 23 // Ideally we could restrict ourselves to the limited API of 3.7, but this is 24 // a very important function that was not officially added to the limited API 25 // until 3.10. Without this function, there is no way of getting data from a 26 // Python `str` object without a copy. 27 // 28 // While this function was not *officially* added to the limited API until 29 // Python 3.10, In practice it has been stable since Python 3.1. 30 // https://bugs.python.org/issue41784 31 // 32 // On Linux/ELF and macOS/Mach-O, we can get away with using this function with 33 // the limited API prior to 3.10. 34 35 #if (defined(__linux__) || defined(__APPLE__)) && defined(Py_LIMITED_API) && \ 36 Py_LIMITED_API < 0x03100000 37 PyAPI_FUNC(const char*) 38 PyUnicode_AsUTF8AndSize(PyObject* unicode, Py_ssize_t* size); 39 #endif 40 41 #endif // PYUPB_PYTHON_H__ 42