blob: e94a6101efb582b3158aafad4a9127f98c001822 [file] [log] [blame]
Adam Cozzette5aca7282023-08-07 10:01:08 -07001// Protocol Buffers - Google's data interchange format
2// Copyright 2023 Google LLC. All rights reserved.
Adam Cozzette5aca7282023-08-07 10:01:08 -07003//
Protobuf Team Bot0fab7732023-11-20 13:38:15 -08004// 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 Habermandeddc202021-12-01 15:11:12 -08007
8#ifndef PYUPB_PYTHON_H__
9#define PYUPB_PYTHON_H__
10
Protobuf Teamee6b1ab2022-04-13 09:06:17 -070011// 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 Habermandeddc202021-12-01 15:11:12 -080018
Protobuf Teamee6b1ab2022-04-13 09:06:17 -070019// #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 Habermandeddc202021-12-01 15:11:12 -080030// https://bugs.python.org/issue41784
Protobuf Teamee6b1ab2022-04-13 09:06:17 -070031//
Joshua Habermancb723e42022-05-04 16:04:13 -070032// 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 Teamee6b1ab2022-04-13 09:06:17 -070034
Joshua Habermancb723e42022-05-04 16:04:13 -070035#if (defined(__linux__) || defined(__APPLE__)) && defined(Py_LIMITED_API) && \
36 Py_LIMITED_API < 0x03100000
Joshua Haberman1c955f32022-01-12 07:19:28 -080037PyAPI_FUNC(const char*)
38 PyUnicode_AsUTF8AndSize(PyObject* unicode, Py_ssize_t* size);
Protobuf Teamee6b1ab2022-04-13 09:06:17 -070039#endif
Joshua Habermandeddc202021-12-01 15:11:12 -080040
41#endif // PYUPB_PYTHON_H__