|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#pragma once |
| 18 | + |
| 19 | +#include <Python.h> // @manual=fbsource//third-party/python:python-headers |
| 20 | + |
| 21 | +extern "C" { |
| 22 | + |
| 23 | +// NOT_WINDOWS |
| 24 | +#if defined(__APPLE__) || defined(__linux__) |
| 25 | + |
| 26 | +#if defined(__APPLE__) |
| 27 | +#define Py_Weak(RTYPE) __attribute__((weak_import)) extern RTYPE |
| 28 | +#else |
| 29 | +#define Py_Weak(RTYPE) __attribute__((weak)) extern RTYPE |
| 30 | +#endif |
| 31 | + |
| 32 | +// These symbols provides our base for detecting python is loaded |
| 33 | +// See folly::python::isLinked() |
| 34 | +Py_Weak(void) Py_IncRef(PyObject*); |
| 35 | +Py_Weak(void) Py_DecRef(PyObject*); |
| 36 | +Py_Weak(const char*) Py_GetVersion(void); |
| 37 | + |
| 38 | +// Modules |
| 39 | +Py_Weak(PyObject*) PyImport_ImportModule(const char*); |
| 40 | + |
| 41 | +// Exception Handling |
| 42 | +Py_Weak(PyObject*) PyErr_Occurred(void); |
| 43 | +Py_Weak(void) PyErr_Clear(void); |
| 44 | +Py_Weak(void) PyErr_Fetch(PyObject**, PyObject**, PyObject**); |
| 45 | + |
| 46 | +// Object Handling |
| 47 | +Py_Weak(PyObject*) PyObject_Repr(PyObject*); |
| 48 | + |
| 49 | +// Unicode && Bytes Handling |
| 50 | +Py_Weak(char*) PyBytes_AsString(PyObject*); |
| 51 | +Py_Weak(PyObject*) |
| 52 | + PyUnicode_AsEncodedString(PyObject*, const char*, const char*); |
| 53 | +Py_Weak(const char*) PyUnicode_AsUTF8(PyObject*); |
| 54 | + |
| 55 | +// Basic GIL Handling |
| 56 | +Py_Weak(PyThreadState*) PyGILState_GetThisThreadState(void); |
| 57 | +Py_Weak(int) PyGILState_Check(void); |
| 58 | +Py_Weak(PyGILState_STATE) PyGILState_Ensure(void); |
| 59 | +Py_Weak(void) PyGILState_Release(PyGILState_STATE); |
| 60 | + |
| 61 | +// Some Frame and Traceback Handling |
| 62 | +Py_Weak(PyFrameObject*) PyThreadState_GetFrame(PyThreadState*); |
| 63 | +Py_Weak(int) PyFrame_GetLineNumber(PyFrameObject*); |
| 64 | +Py_Weak(void) _Py_DumpTraceback(int, PyThreadState*); |
| 65 | +Py_Weak(PyCodeObject*) PyFrame_GetCode(PyFrameObject*); |
| 66 | +Py_Weak(PyFrameObject*) PyFrame_GetBack(PyFrameObject*); |
| 67 | +#if PY_VERSION_HEX >= 0x030b0000 // >= 3.11 |
| 68 | +Py_Weak(int) PyFrame_GetLasti(PyFrameObject*); |
| 69 | +#endif |
| 70 | + |
| 71 | +// Runtime State |
| 72 | +Py_Weak(int) Py_IsInitialized(void); |
| 73 | +#if PY_VERSION_HEX >= 0x030d0000 // >= 3.13 |
| 74 | +Py_Weak(int) Py_IsFinalizing(void); |
| 75 | +#else |
| 76 | +Py_Weak(int) _Py_IsFinalizing(void); |
| 77 | +#endif |
| 78 | + |
| 79 | +#undef Py_Weak |
| 80 | +#endif // NOT_WINDOWS |
| 81 | + |
| 82 | +} // extern "C" |
| 83 | + |
| 84 | +// So windows can use these helpers |
| 85 | +#if PY_VERSION_HEX < 0x030b0000 // < 3.11 |
| 86 | +#include <frameobject.h> |
| 87 | +inline int PyFrame_GetLasti(PyFrameObject* frame) { |
| 88 | + return frame->f_lasti; |
| 89 | +} |
| 90 | +#endif |
| 91 | + |
| 92 | +#if PY_VERSION_HEX < 0x030d0000 // < 3.13 |
| 93 | +inline int Py_IsFinalizing() { |
| 94 | + return _Py_IsFinalizing(); |
| 95 | +} |
| 96 | +#endif |
| 97 | + |
| 98 | +namespace folly::python { |
| 99 | + |
| 100 | +// Lets use these symbols if they are defined we can assume python is loaded |
| 101 | +inline bool isLinked() { |
| 102 | +#if defined(__APPLE__) || defined(__linux__) |
| 103 | + return (Py_IncRef != nullptr) && (Py_DecRef != nullptr) && |
| 104 | + (Py_GetVersion != nullptr); |
| 105 | +#else |
| 106 | + return true; |
| 107 | +#endif |
| 108 | +} |
| 109 | + |
| 110 | +} // namespace folly::python |
0 commit comments