Skip to content

Commit fc260b8

Browse files
friedfacebook-github-bot
authored andcommitted
If possible don't required linking libpython
Summary: libpython linking has crept into many C++ binaries that were believed to be pure C++. This has made for some confusing errors as we upgrade from python 3.10 -> 3.12. This also causes the blast radius for python upgrades to affect countless C++ binaries that would otherwise not be affected. Reviewed By: Gownta Differential Revision: D72362668 fbshipit-source-id: 75b72552e91a71c656ea31c7e3474d42e8caab25
1 parent 8f5afcd commit fc260b8

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

third-party/folly/src/folly/python/error.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <stdexcept>
2020
#include <string>
2121

22-
#include <Python.h>
22+
#include <folly/python/Weak.h>
2323

2424
#include <folly/Conv.h>
2525
#include <folly/ScopeGuard.h>
@@ -32,8 +32,8 @@ std::string pyObjectToString(PyObject* obj) {
3232
constexpr StringPiece kConversionFail = "Error conversion failed";
3333
PyObject *pyStr, *pyBytes;
3434
SCOPE_EXIT {
35-
Py_XDECREF(pyStr);
36-
Py_XDECREF(pyBytes);
35+
Py_DecRef(pyStr);
36+
Py_DecRef(pyBytes);
3737
// Swallow any errors that arise in this function
3838
PyErr_Clear();
3939
};
@@ -44,16 +44,12 @@ std::string pyObjectToString(PyObject* obj) {
4444
}
4545

4646
char* cStr = nullptr;
47-
#if PY_VERSION_HEX < 0x0300000
48-
cStr = PyString_AsString(pyStr);
49-
#else
5047
pyBytes = PyUnicode_AsEncodedString(pyStr, "utf-8", "strict");
5148
if (pyBytes == nullptr) {
5249
return std::string(kConversionFail);
5350
}
5451

5552
cStr = PyBytes_AsString(pyBytes);
56-
#endif
5753

5854
if (cStr == nullptr) {
5955
return std::string(kConversionFail);
@@ -67,9 +63,9 @@ std::string pyObjectToString(PyObject* obj) {
6763
void handlePythonError(StringPiece errPrefix) {
6864
PyObject *ptype, *pvalue, *ptraceback;
6965
SCOPE_EXIT {
70-
Py_XDECREF(ptype);
71-
Py_XDECREF(pvalue);
72-
Py_XDECREF(ptraceback);
66+
Py_DecRef(ptype);
67+
Py_DecRef(pvalue);
68+
Py_DecRef(ptraceback);
7369
};
7470
/**
7571
* PyErr_Fetch will clear the error indicator (which *should* be set here, but

0 commit comments

Comments
 (0)