Adam Cozzette | 5aca728 | 2023-08-07 10:01:08 -0700 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2023 Google LLC. All rights reserved. |
Adam Cozzette | 5aca728 | 2023-08-07 10:01:08 -0700 | [diff] [blame] | 3 | // |
Protobuf Team Bot | 0fab773 | 2023-11-20 13:38:15 -0800 | [diff] [blame] | 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 |
Joshua Haberman | deddc20 | 2021-12-01 15:11:12 -0800 | [diff] [blame] | 7 | |
| 8 | #ifndef PYUPB_PYTHON_H__ |
| 9 | #define PYUPB_PYTHON_H__ |
| 10 | |
Protobuf Team | ee6b1ab | 2022-04-13 09:06:17 -0700 | [diff] [blame] | 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. |
Joshua Haberman | deddc20 | 2021-12-01 15:11:12 -0800 | [diff] [blame] | 18 | |
Protobuf Team | ee6b1ab | 2022-04-13 09:06:17 -0700 | [diff] [blame] | 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. |
Joshua Haberman | deddc20 | 2021-12-01 15:11:12 -0800 | [diff] [blame] | 30 | // https://bugs.python.org/issue41784 |
Protobuf Team | ee6b1ab | 2022-04-13 09:06:17 -0700 | [diff] [blame] | 31 | // |
Joshua Haberman | cb723e4 | 2022-05-04 16:04:13 -0700 | [diff] [blame] | 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. |
Protobuf Team | ee6b1ab | 2022-04-13 09:06:17 -0700 | [diff] [blame] | 34 | |
Joshua Haberman | cb723e4 | 2022-05-04 16:04:13 -0700 | [diff] [blame] | 35 | #if (defined(__linux__) || defined(__APPLE__)) && defined(Py_LIMITED_API) && \ |
| 36 | Py_LIMITED_API < 0x03100000 |
Joshua Haberman | 1c955f3 | 2022-01-12 07:19:28 -0800 | [diff] [blame] | 37 | PyAPI_FUNC(const char*) |
| 38 | PyUnicode_AsUTF8AndSize(PyObject* unicode, Py_ssize_t* size); |
Protobuf Team | ee6b1ab | 2022-04-13 09:06:17 -0700 | [diff] [blame] | 39 | #endif |
Joshua Haberman | deddc20 | 2021-12-01 15:11:12 -0800 | [diff] [blame] | 40 | |
| 41 | #endif // PYUPB_PYTHON_H__ |