revert-pr3688-drop-python-2.patch 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. --- contrib/libs/pybind11/include/pybind11/cast.h (54a9461a4d6f0255e1b2e17444ac91a53daeb018)
  2. +++ contrib/libs/pybind11/include/pybind11/cast.h (index)
  3. @@ -384,14 +384,35 @@ struct string_caster {
  4. static constexpr size_t UTF_N = 8 * sizeof(CharT);
  5. bool load(handle src, bool) {
  6. +#if PY_MAJOR_VERSION < 3
  7. + object temp;
  8. +#endif
  9. handle load_src = src;
  10. if (!src) {
  11. return false;
  12. }
  13. if (!PyUnicode_Check(load_src.ptr())) {
  14. +#if PY_MAJOR_VERSION >= 3
  15. return load_raw(load_src);
  16. +#else
  17. + if (std::is_same<CharT, char>::value) {
  18. + return load_raw(load_src);
  19. + }
  20. +
  21. + // The below is a guaranteed failure in Python 3 when PyUnicode_Check returns false
  22. + if (!PYBIND11_BYTES_CHECK(load_src.ptr()))
  23. + return false;
  24. +
  25. + temp = reinterpret_steal<object>(PyUnicode_FromObject(load_src.ptr()));
  26. + if (!temp) {
  27. + PyErr_Clear();
  28. + return false;
  29. + }
  30. + load_src = temp;
  31. +#endif
  32. }
  33. +#if PY_VERSION_HEX >= 0x03030000
  34. // For UTF-8 we avoid the need for a temporary `bytes` object by using
  35. // `PyUnicode_AsUTF8AndSize`.
  36. if (UTF_N == 8) {
  37. @@ -405,6 +426,7 @@ struct string_caster {
  38. value = StringType(buffer, static_cast<size_t>(size));
  39. return true;
  40. }
  41. +#endif
  42. auto utfNbytes
  43. = reinterpret_steal<object>(PyUnicode_AsEncodedString(load_src.ptr(),
  44. @@ -933,6 +955,18 @@ struct pyobject_caster {
  45. template <typename T = type, enable_if_t<std::is_base_of<object, T>::value, int> = 0>
  46. bool load(handle src, bool /* convert */) {
  47. +#if PY_MAJOR_VERSION < 3 && !defined(PYBIND11_STR_LEGACY_PERMISSIVE)
  48. + // For Python 2, without this implicit conversion, Python code would
  49. + // need to be cluttered with six.ensure_text() or similar, only to be
  50. + // un-cluttered later after Python 2 support is dropped.
  51. + if ((std::is_same<T, str>::value) && isinstance<bytes>(src)) {
  52. + PyObject *str_from_bytes = PyUnicode_FromEncodedObject(src.ptr(), "utf-8", nullptr);
  53. + if (!str_from_bytes)
  54. + throw error_already_set();
  55. + value = reinterpret_steal<type>(str_from_bytes);
  56. + return true;
  57. + }
  58. +#endif
  59. if (!isinstance<type>(src)) {
  60. return false;
  61. }
  62. @@ -1666,7 +1666,7 @@ unpacking_collector<policy> collect_arguments(Args &&...args) {
  63. template <typename Derived>
  64. template <return_value_policy policy, typename... Args>
  65. object object_api<Derived>::operator()(Args &&...args) const {
  66. -#ifndef NDEBUG
  67. +#if !defined(NDEBUG) && PY_VERSION_HEX >= 0x03060000
  68. if (!PyGILState_Check()) {
  69. pybind11_fail("pybind11::object_api<>::operator() PyGILState_Check() failure.");
  70. }
  71. --- contrib/libs/pybind11/include/pybind11/embed.h (54a9461a4d6f0255e1b2e17444ac91a53daeb018)
  72. +++ contrib/libs/pybind11/include/pybind11/embed.h (index)
  73. @@ -19,9 +19,15 @@
  74. # error Embedding the interpreter is not supported with PyPy
  75. #endif
  76. -#define PYBIND11_EMBEDDED_MODULE_IMPL(name) \
  77. - extern "C" PyObject *pybind11_init_impl_##name(); \
  78. - extern "C" PyObject *pybind11_init_impl_##name() { return pybind11_init_wrapper_##name(); }
  79. +#if PY_MAJOR_VERSION >= 3
  80. +# define PYBIND11_EMBEDDED_MODULE_IMPL(name) \
  81. + extern "C" PyObject *pybind11_init_impl_##name(); \
  82. + extern "C" PyObject *pybind11_init_impl_##name() { return pybind11_init_wrapper_##name(); }
  83. +#else
  84. +# define PYBIND11_EMBEDDED_MODULE_IMPL(name) \
  85. + extern "C" void pybind11_init_impl_##name(); \
  86. + extern "C" void pybind11_init_impl_##name() { pybind11_init_wrapper_##name(); }
  87. +#endif
  88. /** \rst
  89. Add a new module to the table of builtins for the interpreter. Must be
  90. @@ -61,7 +67,11 @@ PYBIND11_NAMESPACE_BEGIN(detail)
  91. /// Python 2.7/3.x compatible version of `PyImport_AppendInittab` and error checks.
  92. struct embedded_module {
  93. +#if PY_MAJOR_VERSION >= 3
  94. using init_t = PyObject *(*) ();
  95. +#else
  96. + using init_t = void (*)();
  97. +#endif
  98. embedded_module(const char *name, init_t init) {
  99. if (Py_IsInitialized() != 0) {
  100. pybind11_fail("Can't add new modules after the interpreter has been initialized");
  101. @@ -76,13 +86,42 @@ struct embedded_module {
  102. struct wide_char_arg_deleter {
  103. void operator()(wchar_t *ptr) const {
  104. +#if PY_VERSION_HEX >= 0x030500f0
  105. // API docs: https://docs.python.org/3/c-api/sys.html#c.Py_DecodeLocale
  106. PyMem_RawFree(ptr);
  107. +#else
  108. + delete[] ptr;
  109. +#endif
  110. }
  111. };
  112. inline wchar_t *widen_chars(const char *safe_arg) {
  113. +#if PY_VERSION_HEX >= 0x030500f0
  114. wchar_t *widened_arg = Py_DecodeLocale(safe_arg, nullptr);
  115. +#else
  116. + wchar_t *widened_arg = nullptr;
  117. +
  118. +// warning C4996: 'mbstowcs': This function or variable may be unsafe.
  119. +# if defined(_MSC_VER)
  120. +# pragma warning(push)
  121. +# pragma warning(disable : 4996)
  122. +# endif
  123. +
  124. +# if defined(HAVE_BROKEN_MBSTOWCS) && HAVE_BROKEN_MBSTOWCS
  125. + size_t count = std::strlen(safe_arg);
  126. +# else
  127. + size_t count = std::mbstowcs(nullptr, safe_arg, 0);
  128. +# endif
  129. + if (count != static_cast<size_t>(-1)) {
  130. + widened_arg = new wchar_t[count + 1];
  131. + std::mbstowcs(widened_arg, safe_arg, count + 1);
  132. + }
  133. +
  134. +# if defined(_MSC_VER)
  135. +# pragma warning(pop)
  136. +# endif
  137. +
  138. +#endif
  139. return widened_arg;
  140. }
  141. @@ -118,6 +157,7 @@ inline void initialize_interpreter_pre_pyconfig(bool init_signal_handlers,
  142. }
  143. auto argv_size = static_cast<size_t>(argc);
  144. +#if PY_MAJOR_VERSION >= 3
  145. // SetArgv* on python 3 takes wchar_t, so we have to convert.
  146. std::unique_ptr<wchar_t *[]> widened_argv(new wchar_t *[argv_size]);
  147. std::vector<std::unique_ptr<wchar_t[], detail::wide_char_arg_deleter>> widened_argv_entries;
  148. @@ -133,6 +173,14 @@ inline void initialize_interpreter_pre_pyconfig(bool init_signal_handlers,
  149. }
  150. auto *pysys_argv = widened_argv.get();
  151. +#else
  152. + // python 2.x
  153. + std::vector<std::string> strings{safe_argv, safe_argv + argv_size};
  154. + std::vector<char *> char_strings{argv_size};
  155. + for (std::size_t i = 0; i < argv_size; ++i)
  156. + char_strings[i] = &strings[i][0];
  157. + char **pysys_argv = char_strings.data();
  158. +#endif
  159. PySys_SetArgvEx(argc, pysys_argv, static_cast<int>(add_program_dir_to_path));
  160. }
  161. --- contrib/libs/pybind11/include/pybind11/eval.h (54a9461a4d6f0255e1b2e17444ac91a53daeb018)
  162. +++ contrib/libs/pybind11/include/pybind11/eval.h (index)
  163. @@ -94,7 +94,7 @@ void exec(const char (&s)[N], object global = globals(), object local = object()
  164. eval<eval_statements>(s, std::move(global), std::move(local));
  165. }
  166. -#if defined(PYPY_VERSION)
  167. +#if defined(PYPY_VERSION) && PY_VERSION_HEX >= 0x03000000
  168. template <eval_mode mode = eval_statements>
  169. object eval_file(str, object, object) {
  170. pybind11_fail("eval_file not supported in PyPy3. Use eval");
  171. @@ -133,18 +133,38 @@ object eval_file(str fname, object global = globals(), object local = object())
  172. int closeFile = 1;
  173. std::string fname_str = (std::string) fname;
  174. +# if PY_VERSION_HEX >= 0x03040000
  175. FILE *f = _Py_fopen_obj(fname.ptr(), "r");
  176. +# else
  177. + /* No unicode support in open() :( */
  178. + auto fobj = reinterpret_steal<object>(
  179. + PyFile_FromString(const_cast<char *>(fname_str.c_str()), const_cast<char *>("r")));
  180. + FILE *f = nullptr;
  181. + if (fobj)
  182. + f = PyFile_AsFile(fobj.ptr());
  183. + closeFile = 0;
  184. +# endif
  185. if (!f) {
  186. PyErr_Clear();
  187. pybind11_fail("File \"" + fname_str + "\" could not be opened!");
  188. }
  189. + // In Python2, this should be encoded by getfilesystemencoding.
  190. + // We don't boher setting it since Python2 is past EOL anyway.
  191. + // See PR#3233
  192. +# if PY_VERSION_HEX >= 0x03000000
  193. if (!global.contains("__file__")) {
  194. global["__file__"] = std::move(fname);
  195. }
  196. +# endif
  197. +# if PY_VERSION_HEX < 0x03000000 && defined(PYPY_VERSION)
  198. + PyObject *result = PyRun_File(f, fname_str.c_str(), start, global.ptr(), local.ptr());
  199. + (void) closeFile;
  200. +# else
  201. PyObject *result
  202. = PyRun_FileEx(f, fname_str.c_str(), start, global.ptr(), local.ptr(), closeFile);
  203. +# endif
  204. if (!result) {
  205. throw error_already_set();
  206. --- contrib/libs/pybind11/include/pybind11/numpy.h (54a9461a4d6f0255e1b2e17444ac91a53daeb018)
  207. +++ contrib/libs/pybind11/include/pybind11/numpy.h (index)
  208. @@ -265,7 +265,11 @@ private:
  209. static npy_api lookup() {
  210. module_ m = detail::import_numpy_core_submodule("multiarray");
  211. auto c = m.attr("_ARRAY_API");
  212. +#if PY_MAJOR_VERSION >= 3
  213. void **api_ptr = (void **) PyCapsule_GetPointer(c.ptr(), nullptr);
  214. +#else
  215. + void **api_ptr = (void **) PyCObject_AsVoidPtr(c.ptr());
  216. +#endif
  217. if (api_ptr == nullptr) {
  218. raise_from(PyExc_SystemError, "FAILURE obtaining numpy _ARRAY_API pointer.");
  219. throw error_already_set();
  220. --- contrib/libs/pybind11/include/pybind11/operators.h (54a9461a4d6f0255e1b2e17444ac91a53daeb018)
  221. +++ contrib/libs/pybind11/include/pybind11/operators.h (index)
  222. @@ -92,6 +92,16 @@ struct op_ {
  223. using R_type = conditional_t<std::is_same<R, self_t>::value, Base, R>;
  224. using op = op_impl<id, ot, Base, L_type, R_type>;
  225. cl.def(op::name(), &op::execute, is_operator(), extra...);
  226. +#if PY_MAJOR_VERSION < 3
  227. + if ((id == op_truediv)
  228. + || (id == op_itruediv))
  229. + cl.def(id == op_itruediv ? "__idiv__"
  230. + : ot == op_l ? "__div__"
  231. + : "__rdiv__",
  232. + &op::execute,
  233. + is_operator(),
  234. + extra...);
  235. +#endif
  236. }
  237. template <typename Class, typename... Extra>
  238. void execute_cast(Class &cl, const Extra &...extra) const {
  239. @@ -100,6 +110,15 @@ struct op_ {
  240. using R_type = conditional_t<std::is_same<R, self_t>::value, Base, R>;
  241. using op = op_impl<id, ot, Base, L_type, R_type>;
  242. cl.def(op::name(), &op::execute_cast, is_operator(), extra...);
  243. +#if PY_MAJOR_VERSION < 3
  244. + if (id == op_truediv || id == op_itruediv)
  245. + cl.def(id == op_itruediv ? "__idiv__"
  246. + : ot == op_l ? "__div__"
  247. + : "__rdiv__",
  248. + &op::execute,
  249. + is_operator(),
  250. + extra...);
  251. +#endif
  252. }
  253. };
  254. --- contrib/libs/pybind11/include/pybind11/pybind11.h (54a9461a4d6f0255e1b2e17444ac91a53daeb018)
  255. +++ contrib/libs/pybind11/include/pybind11/pybind11.h (index)
  256. @@ -455,6 +455,15 @@ protected:
  257. pybind11_fail("Internal error while parsing type signature (2)");
  258. }
  259. +#if PY_MAJOR_VERSION < 3
  260. + if (std::strcmp(rec->name, "__next__") == 0) {
  261. + std::free(rec->name);
  262. + rec->name = guarded_strdup("next");
  263. + } else if (std::strcmp(rec->name, "__bool__") == 0) {
  264. + std::free(rec->name);
  265. + rec->name = guarded_strdup("__nonzero__");
  266. + }
  267. +#endif
  268. rec->signature = guarded_strdup(signature.c_str());
  269. rec->args.shrink_to_fit();
  270. rec->nargs = (std::uint16_t) args;
  271. @@ -1112,12 +1121,14 @@ protected:
  272. }
  273. append_note_if_missing_header_is_suspected(msg);
  274. +#if PY_VERSION_HEX >= 0x03030000
  275. // Attach additional error info to the exception if supported
  276. if (PyErr_Occurred()) {
  277. // #HelpAppreciated: unit test coverage for this branch.
  278. raise_from(PyExc_TypeError, msg.c_str());
  279. return nullptr;
  280. }
  281. +#endif
  282. set_error(PyExc_TypeError, msg.c_str());
  283. return nullptr;
  284. }
  285. @@ -1126,11 +1137,13 @@ protected:
  286. assert(current_overload != nullptr);
  287. msg += current_overload->signature;
  288. append_note_if_missing_header_is_suspected(msg);
  289. +#if PY_VERSION_HEX >= 0x03030000
  290. // Attach additional error info to the exception if supported
  291. if (PyErr_Occurred()) {
  292. raise_from(PyExc_TypeError, msg.c_str());
  293. return nullptr;
  294. }
  295. +#endif
  296. set_error(PyExc_TypeError, msg.c_str());
  297. return nullptr;
  298. }
  299. @@ -1150,7 +1163,11 @@ public:
  300. /// Create a new top-level Python module with the given name and docstring
  301. PYBIND11_DEPRECATED("Use PYBIND11_MODULE or module_::create_extension_module instead")
  302. explicit module_(const char *name, const char *doc = nullptr) {
  303. +#if PY_MAJOR_VERSION >= 3
  304. *this = create_extension_module(name, doc, new PyModuleDef());
  305. +#else
  306. + *this = create_extension_module(name, doc, nullptr);
  307. +#endif
  308. }
  309. /** \rst
  310. @@ -1235,18 +1252,24 @@ public:
  311. PyModule_AddObject(ptr(), name, obj.inc_ref().ptr() /* steals a reference */);
  312. }
  313. +#if PY_MAJOR_VERSION >= 3
  314. using module_def = PyModuleDef; // TODO: Can this be removed (it was needed only for Python 2)?
  315. +#else
  316. + struct module_def {};
  317. +#endif
  318. /** \rst
  319. Create a new top-level module that can be used as the main module of a C extension.
  320. - ``def`` should point to a statically allocated module_def.
  321. + For Python 3, ``def`` should point to a statically allocated module_def.
  322. + For Python 2, ``def`` can be a nullptr and is completely ignored.
  323. \endrst */
  324. static module_ create_extension_module(const char *name,
  325. const char *doc,
  326. module_def *def,
  327. mod_gil_not_used gil_not_used
  328. = mod_gil_not_used(false)) {
  329. +#if PY_MAJOR_VERSION >= 3
  330. // module_def is PyModuleDef
  331. // Placement new (not an allocation).
  332. def = new (def)
  333. @@ -1256,6 +1279,12 @@ public:
  334. /* m_clear */ nullptr,
  335. /* m_free */ nullptr};
  336. auto *m = PyModule_Create(def);
  337. +#else
  338. + // Ignore module_def *def; only necessary for Python 3
  339. + (void) def;
  340. + auto m = Py_InitModule3(
  341. + name, nullptr, options::show_user_defined_docstrings() ? doc : nullptr);
  342. +#endif
  343. if (m == nullptr) {
  344. if (PyErr_Occurred()) {
  345. throw error_already_set();
  346. @@ -1282,12 +1311,14 @@ inline dict globals() {
  347. return reinterpret_borrow<dict>(p ? p : module_::import("__main__").attr("__dict__").ptr());
  348. }
  349. +#if PY_VERSION_HEX >= 0x03030000
  350. template <typename... Args, typename = detail::enable_if_t<args_are_all_keyword_or_ds<Args...>()>>
  351. PYBIND11_DEPRECATED("make_simple_namespace should be replaced with "
  352. "py::module_::import(\"types\").attr(\"SimpleNamespace\") ")
  353. object make_simple_namespace(Args &&...args_) {
  354. return module_::import("types").attr("SimpleNamespace")(std::forward<Args>(args_)...);
  355. }
  356. +#endif
  357. PYBIND11_NAMESPACE_BEGIN(detail)
  358. /// Generic support for creating new Python heap types
  359. @@ -2184,6 +2215,9 @@ public:
  360. def_property_readonly("value", [](Type value) { return (Scalar) value; });
  361. def("__int__", [](Type value) { return (Scalar) value; });
  362. def("__index__", [](Type value) { return (Scalar) value; });
  363. +#if PY_MAJOR_VERSION < 3
  364. + def("__long__", [](Type value) { return (Scalar) value; });
  365. +#endif
  366. attr("__setstate__") = cpp_function(
  367. [](detail::value_and_holder &v_h, Scalar arg) {
  368. detail::initimpl::setstate<Base>(
  369. --- contrib/libs/pybind11/include/pybind11/pytypes.h (54a9461a4d6f0255e1b2e17444ac91a53daeb018)
  370. +++ contrib/libs/pybind11/include/pybind11/pytypes.h (index)
  371. @@ -716,6 +716,8 @@ private:
  372. static void m_fetched_error_deleter(detail::error_fetch_and_normalize *raw_ptr);
  373. };
  374. +#if PY_VERSION_HEX >= 0x03030000
  375. +
  376. /// Replaces the current Python error indicator with the chosen error, performing a
  377. /// 'raise from' to indicate that the chosen error was caused by the original error.
  378. inline void raise_from(PyObject *type, const char *message) {
  379. @@ -752,6 +754,8 @@ inline void raise_from(error_already_set &err, PyObject *type, const char *messa
  380. raise_from(type, message);
  381. }
  382. +#endif
  383. +
  384. /** \defgroup python_builtins const_name
  385. Unless stated otherwise, the following C++ functions behave the same
  386. as their Python counterparts.
  387. @@ -868,9 +872,12 @@ inline ssize_t hash(handle obj) {
  388. PYBIND11_NAMESPACE_BEGIN(detail)
  389. inline handle get_function(handle value) {
  390. if (value) {
  391. +#if PY_MAJOR_VERSION >= 3
  392. if (PyInstanceMethod_Check(value.ptr())) {
  393. value = PyInstanceMethod_GET_FUNCTION(value.ptr());
  394. - } else if (PyMethod_Check(value.ptr())) {
  395. + } else
  396. +#endif
  397. + if (PyMethod_Check(value.ptr())) {
  398. value = PyMethod_GET_FUNCTION(value.ptr());
  399. }
  400. }
  401. @@ -882,6 +889,7 @@ inline handle get_function(handle value) {
  402. // copied from cpython _PyDict_GetItemStringWithError
  403. inline PyObject *dict_getitemstring(PyObject *v, const char *key) {
  404. +#if PY_MAJOR_VERSION >= 3
  405. PyObject *kv = nullptr, *rv = nullptr;
  406. kv = PyUnicode_FromString(key);
  407. if (kv == nullptr) {
  408. @@ -894,14 +902,21 @@ inline PyObject *dict_getitemstring(PyObject *v, const char *key) {
  409. throw error_already_set();
  410. }
  411. return rv;
  412. +#else
  413. + return PyDict_GetItemString(v, key);
  414. +#endif
  415. }
  416. inline PyObject *dict_getitem(PyObject *v, PyObject *key) {
  417. +#if PY_MAJOR_VERSION >= 3
  418. PyObject *rv = PyDict_GetItemWithError(v, key);
  419. if (rv == nullptr && PyErr_Occurred()) {
  420. throw error_already_set();
  421. }
  422. return rv;
  423. +#else
  424. + return PyDict_GetItem(v, key);
  425. +#endif
  426. }
  427. // Helper aliases/functions to support implicit casting of values given to python
  428. @@ -1544,6 +1559,13 @@ private:
  429. /// Return string representation -- always returns a new reference, even if already a str
  430. static PyObject *raw_str(PyObject *op) {
  431. PyObject *str_value = PyObject_Str(op);
  432. +#if PY_MAJOR_VERSION < 3
  433. + if (!str_value)
  434. + throw error_already_set();
  435. + PyObject *unicode = PyUnicode_FromEncodedObject(str_value, "utf-8", nullptr);
  436. + Py_XDECREF(str_value);
  437. + str_value = unicode;
  438. +#endif
  439. return str_value;
  440. }
  441. };
  442. @@ -1722,7 +1744,11 @@ PYBIND11_NAMESPACE_BEGIN(detail)
  443. // unsigned type: (A)-1 != (B)-1 when A and B are unsigned types of different sizes).
  444. template <typename Unsigned>
  445. Unsigned as_unsigned(PyObject *o) {
  446. - if (sizeof(Unsigned) <= sizeof(unsigned long)) {
  447. + if (sizeof(Unsigned) <= sizeof(unsigned long)
  448. +#if PY_VERSION_HEX < 0x03000000
  449. + || PyInt_Check(o)
  450. +#endif
  451. + ) {
  452. unsigned long v = PyLong_AsUnsignedLong(o);
  453. return v == (unsigned long) -1 && PyErr_Occurred() ? (Unsigned) -1 : (Unsigned) v;
  454. }
  455. @@ -2254,6 +2280,7 @@ public:
  456. const_cast<T *>(ptr), std::move(shape), std::move(strides), true);
  457. }
  458. +#if PY_MAJOR_VERSION >= 3
  459. /** \rst
  460. Creates ``memoryview`` from static memory.
  461. @@ -2279,10 +2306,12 @@ public:
  462. return memoryview::from_memory(const_cast<void *>(mem), size, true);
  463. }
  464. -#ifdef PYBIND11_HAS_STRING_VIEW
  465. +# ifdef PYBIND11_HAS_STRING_VIEW
  466. static memoryview from_memory(std::string_view mem) {
  467. return from_memory(const_cast<char *>(mem.data()), static_cast<ssize_t>(mem.size()), true);
  468. }
  469. +# endif
  470. +
  471. #endif
  472. };
  473. @@ -2337,7 +2366,11 @@ inline size_t len(handle h) {
  474. /// Get the length hint of a Python object.
  475. /// Returns 0 when this cannot be determined.
  476. inline size_t len_hint(handle h) {
  477. +#if PY_VERSION_HEX >= 0x03040000
  478. ssize_t result = PyObject_LengthHint(h.ptr(), 0);
  479. +#else
  480. + ssize_t result = PyObject_Length(h.ptr());
  481. +#endif
  482. if (result < 0) {
  483. // Sometimes a length can't be determined at all (eg generators)
  484. // In which case simply return 0
  485. @@ -2352,6 +2385,13 @@ inline str repr(handle h) {
  486. if (!str_value) {
  487. throw error_already_set();
  488. }
  489. +#if PY_MAJOR_VERSION < 3
  490. + PyObject *unicode = PyUnicode_FromEncodedObject(str_value, "utf-8", nullptr);
  491. + Py_XDECREF(str_value);
  492. + str_value = unicode;
  493. + if (!str_value)
  494. + throw error_already_set();
  495. +#endif
  496. return reinterpret_steal<str>(str_value);
  497. }
  498. --- contrib/libs/pybind11/include/pybind11/detail/class.h (54a9461a4d6f0255e1b2e17444ac91a53daeb018)
  499. +++ contrib/libs/pybind11/include/pybind11/detail/class.h (index)
  500. @@ -15,7 +15,7 @@
  501. PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
  502. PYBIND11_NAMESPACE_BEGIN(detail)
  503. -#if !defined(PYPY_VERSION)
  504. +#if PY_VERSION_HEX >= 0x03030000 && !defined(PYPY_VERSION)
  505. # define PYBIND11_BUILTIN_QUALNAME
  506. # define PYBIND11_SET_OLDPY_QUALNAME(obj, nameobj)
  507. #else
  508. @@ -165,6 +165,7 @@ extern "C" inline int pybind11_meta_setattro(PyObject *obj, PyObject *name, PyOb
  509. }
  510. }
  511. +#if PY_MAJOR_VERSION >= 3
  512. /**
  513. * Python 3's PyInstanceMethod_Type hides itself via its tp_descr_get, which prevents aliasing
  514. * methods via cls.attr("m2") = cls.attr("m1"): instead the tp_descr_get returns a plain function,
  515. @@ -179,6 +180,7 @@ extern "C" inline PyObject *pybind11_meta_getattro(PyObject *obj, PyObject *name
  516. }
  517. return PyType_Type.tp_getattro(obj, name);
  518. }
  519. +#endif
  520. /// metaclass `__call__` function that is used to create all pybind11 objects.
  521. extern "C" inline PyObject *pybind11_meta_call(PyObject *type, PyObject *args, PyObject *kwargs) {
  522. @@ -274,7 +276,9 @@ inline PyTypeObject *make_default_metaclass() {
  523. type->tp_call = pybind11_meta_call;
  524. type->tp_setattro = pybind11_meta_setattro;
  525. +#if PY_MAJOR_VERSION >= 3
  526. type->tp_getattro = pybind11_meta_getattro;
  527. +#endif
  528. type->tp_dealloc = pybind11_meta_dealloc;
  529. @@ -520,6 +524,33 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass) {
  530. return (PyObject *) heap_type;
  531. }
  532. +#if PY_MAJOR_VERSION < 3
  533. +/// dynamic_attr: Support for `d = instance.__dict__`.
  534. +extern "C" inline PyObject *pybind11_get_dict(PyObject *self, void *) {
  535. + PyObject *&dict = *_PyObject_GetDictPtr(self);
  536. + if (!dict) {
  537. + dict = PyDict_New();
  538. + }
  539. + Py_XINCREF(dict);
  540. + return dict;
  541. +}
  542. +
  543. +/// dynamic_attr: Support for `instance.__dict__ = dict()`.
  544. +extern "C" inline int pybind11_set_dict(PyObject *self, PyObject *new_dict, void *) {
  545. + if (!PyDict_Check(new_dict)) {
  546. + PyErr_Format(PyExc_TypeError,
  547. + "__dict__ must be set to a dictionary, not a '%.200s'",
  548. + get_fully_qualified_tp_name(Py_TYPE(new_dict)).c_str());
  549. + return -1;
  550. + }
  551. + PyObject *&dict = *_PyObject_GetDictPtr(self);
  552. + Py_INCREF(new_dict);
  553. + Py_CLEAR(dict);
  554. + dict = new_dict;
  555. + return 0;
  556. +}
  557. +#endif
  558. +
  559. /// dynamic_attr: Allow the garbage collector to traverse the internal instance `__dict__`.
  560. extern "C" inline int pybind11_traverse(PyObject *self, visitproc visit, void *arg) {
  561. PyObject *&dict = *_PyObject_GetDictPtr(self);
  562. @@ -557,7 +588,11 @@ inline void enable_dynamic_attributes(PyHeapTypeObject *heap_type) {
  563. type->tp_clear = pybind11_clear;
  564. static PyGetSetDef getset[]
  565. +#if PY_MAJOR_VERSION < 3
  566. + = {{"__dict__", pybind11_get_dict, pybind11_set_dict, nullptr, nullptr},
  567. +#else
  568. = {{"__dict__", PyObject_GenericGetDict, PyObject_GenericSetDict, nullptr, nullptr},
  569. +#endif
  570. {nullptr, nullptr, nullptr, nullptr, nullptr}};
  571. type->tp_getset = getset;
  572. }
  573. @@ -620,6 +656,9 @@ extern "C" inline void pybind11_releasebuffer(PyObject *, Py_buffer *view) {
  574. /// Give this type a buffer interface.
  575. inline void enable_buffer_protocol(PyHeapTypeObject *heap_type) {
  576. heap_type->ht_type.tp_as_buffer = &heap_type->as_buffer;
  577. +#if PY_MAJOR_VERSION < 3
  578. + heap_type->ht_type.tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;
  579. +#endif
  580. heap_type->as_buffer.bf_getbuffer = pybind11_getbuffer;
  581. heap_type->as_buffer.bf_releasebuffer = pybind11_releasebuffer;
  582. @@ -632,8 +671,12 @@ inline PyObject *make_new_python_type(const type_record &rec) {
  583. auto qualname = name;
  584. if (rec.scope && !PyModule_Check(rec.scope.ptr()) && hasattr(rec.scope, "__qualname__")) {
  585. +#if PY_MAJOR_VERSION >= 3
  586. qualname = reinterpret_steal<object>(
  587. PyUnicode_FromFormat("%U.%U", rec.scope.attr("__qualname__").ptr(), name.ptr()));
  588. +#else
  589. + qualname = str(rec.scope.attr("__qualname__").cast<std::string>() + "." + rec.name);
  590. +#endif
  591. }
  592. object module_;
  593. @@ -697,10 +740,15 @@ inline PyObject *make_new_python_type(const type_record &rec) {
  594. type->tp_as_number = &heap_type->as_number;
  595. type->tp_as_sequence = &heap_type->as_sequence;
  596. type->tp_as_mapping = &heap_type->as_mapping;
  597. +#if PY_VERSION_HEX >= 0x03050000
  598. type->tp_as_async = &heap_type->as_async;
  599. +#endif
  600. /* Flags */
  601. type->tp_flags |= Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE;
  602. +#if PY_MAJOR_VERSION < 3
  603. + type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
  604. +#endif
  605. if (!rec.is_final) {
  606. type->tp_flags |= Py_TPFLAGS_BASETYPE;
  607. }
  608. --- contrib/libs/pybind11/include/pybind11/detail/common.h (54a9461a4d6f0255e1b2e17444ac91a53daeb018)
  609. +++ contrib/libs/pybind11/include/pybind11/detail/common.h (index)
  610. @@ -265,3 +265,0 @@ PYBIND11_WARNING_DISABLE_MSVC(4505)
  611. -#if PY_VERSION_HEX < 0x03070000
  612. -# error "PYTHON < 3.7 IS UNSUPPORTED. pybind11 v2.12 was the last to support Python 3.6."
  613. -#endif
  614. @@ -346,33 +343,65 @@ PYBIND11_WARNING_POP
  615. // the transition from the legacy behavior to the non-permissive
  616. // behavior.
  617. -/// Compatibility macros for Python 2 / Python 3 versions TODO: remove
  618. -#define PYBIND11_INSTANCE_METHOD_NEW(ptr, class_) PyInstanceMethod_New(ptr)
  619. -#define PYBIND11_INSTANCE_METHOD_CHECK PyInstanceMethod_Check
  620. -#define PYBIND11_INSTANCE_METHOD_GET_FUNCTION PyInstanceMethod_GET_FUNCTION
  621. -#define PYBIND11_BYTES_CHECK PyBytes_Check
  622. -#define PYBIND11_BYTES_FROM_STRING PyBytes_FromString
  623. -#define PYBIND11_BYTES_FROM_STRING_AND_SIZE PyBytes_FromStringAndSize
  624. -#define PYBIND11_BYTES_AS_STRING_AND_SIZE PyBytes_AsStringAndSize
  625. -#define PYBIND11_BYTES_AS_STRING PyBytes_AsString
  626. -#define PYBIND11_BYTES_SIZE PyBytes_Size
  627. -#define PYBIND11_LONG_CHECK(o) PyLong_Check(o)
  628. -#define PYBIND11_LONG_AS_LONGLONG(o) PyLong_AsLongLong(o)
  629. -#define PYBIND11_LONG_FROM_SIGNED(o) PyLong_FromSsize_t((ssize_t) (o))
  630. -#define PYBIND11_LONG_FROM_UNSIGNED(o) PyLong_FromSize_t((size_t) (o))
  631. -#define PYBIND11_BYTES_NAME "bytes"
  632. -#define PYBIND11_STRING_NAME "str"
  633. -#define PYBIND11_SLICE_OBJECT PyObject
  634. -#define PYBIND11_FROM_STRING PyUnicode_FromString
  635. -#define PYBIND11_STR_TYPE ::pybind11::str
  636. -#define PYBIND11_BOOL_ATTR "__bool__"
  637. -#define PYBIND11_NB_BOOL(ptr) ((ptr)->nb_bool)
  638. -#define PYBIND11_BUILTINS_MODULE "builtins"
  639. +#if PY_MAJOR_VERSION >= 3 /// Compatibility macros for various Python versions
  640. +# define PYBIND11_INSTANCE_METHOD_NEW(ptr, class_) PyInstanceMethod_New(ptr)
  641. +# define PYBIND11_INSTANCE_METHOD_CHECK PyInstanceMethod_Check
  642. +# define PYBIND11_INSTANCE_METHOD_GET_FUNCTION PyInstanceMethod_GET_FUNCTION
  643. +# define PYBIND11_BYTES_CHECK PyBytes_Check
  644. +# define PYBIND11_BYTES_FROM_STRING PyBytes_FromString
  645. +# define PYBIND11_BYTES_FROM_STRING_AND_SIZE PyBytes_FromStringAndSize
  646. +# define PYBIND11_BYTES_AS_STRING_AND_SIZE PyBytes_AsStringAndSize
  647. +# define PYBIND11_BYTES_AS_STRING PyBytes_AsString
  648. +# define PYBIND11_BYTES_SIZE PyBytes_Size
  649. +# define PYBIND11_LONG_CHECK(o) PyLong_Check(o)
  650. +# define PYBIND11_LONG_AS_LONGLONG(o) PyLong_AsLongLong(o)
  651. +# define PYBIND11_LONG_FROM_SIGNED(o) PyLong_FromSsize_t((ssize_t) (o))
  652. +# define PYBIND11_LONG_FROM_UNSIGNED(o) PyLong_FromSize_t((size_t) (o))
  653. +# define PYBIND11_BYTES_NAME "bytes"
  654. +# define PYBIND11_STRING_NAME "str"
  655. +# define PYBIND11_SLICE_OBJECT PyObject
  656. +# define PYBIND11_FROM_STRING PyUnicode_FromString
  657. +# define PYBIND11_STR_TYPE ::pybind11::str
  658. +# define PYBIND11_BOOL_ATTR "__bool__"
  659. +# define PYBIND11_NB_BOOL(ptr) ((ptr)->nb_bool)
  660. +# define PYBIND11_BUILTINS_MODULE "builtins"
  661. // Providing a separate declaration to make Clang's -Wmissing-prototypes happy.
  662. // See comment for PYBIND11_MODULE below for why this is marked "maybe unused".
  663. -#define PYBIND11_PLUGIN_IMPL(name) \
  664. - extern "C" PYBIND11_MAYBE_UNUSED PYBIND11_EXPORT PyObject *PyInit_##name(); \
  665. - extern "C" PYBIND11_EXPORT PyObject *PyInit_##name()
  666. +# define PYBIND11_PLUGIN_IMPL(name) \
  667. + extern "C" PYBIND11_MAYBE_UNUSED PYBIND11_EXPORT PyObject *PyInit_##name(); \
  668. + extern "C" PYBIND11_EXPORT PyObject *PyInit_##name()
  669. +
  670. +#else
  671. +# define PYBIND11_INSTANCE_METHOD_NEW(ptr, class_) PyMethod_New(ptr, nullptr, class_)
  672. +# define PYBIND11_INSTANCE_METHOD_CHECK PyMethod_Check
  673. +# define PYBIND11_INSTANCE_METHOD_GET_FUNCTION PyMethod_GET_FUNCTION
  674. +# define PYBIND11_BYTES_CHECK PyString_Check
  675. +# define PYBIND11_BYTES_FROM_STRING PyString_FromString
  676. +# define PYBIND11_BYTES_FROM_STRING_AND_SIZE PyString_FromStringAndSize
  677. +# define PYBIND11_BYTES_AS_STRING_AND_SIZE PyString_AsStringAndSize
  678. +# define PYBIND11_BYTES_AS_STRING PyString_AsString
  679. +# define PYBIND11_BYTES_SIZE PyString_Size
  680. +# define PYBIND11_LONG_CHECK(o) (PyInt_Check(o) || PyLong_Check(o))
  681. +# define PYBIND11_LONG_AS_LONGLONG(o) \
  682. + (PyInt_Check(o) ? (long long) PyLong_AsLong(o) : PyLong_AsLongLong(o))
  683. +# define PYBIND11_LONG_FROM_SIGNED(o) PyInt_FromSsize_t((ssize_t) o) // Returns long if needed.
  684. +# define PYBIND11_LONG_FROM_UNSIGNED(o) PyInt_FromSize_t((size_t) o) // Returns long if needed.
  685. +# define PYBIND11_BYTES_NAME "str"
  686. +# define PYBIND11_STRING_NAME "unicode"
  687. +# define PYBIND11_SLICE_OBJECT PySliceObject
  688. +# define PYBIND11_FROM_STRING PyString_FromString
  689. +# define PYBIND11_STR_TYPE ::pybind11::bytes
  690. +# define PYBIND11_BOOL_ATTR "__nonzero__"
  691. +# define PYBIND11_NB_BOOL(ptr) ((ptr)->nb_nonzero)
  692. +# define PYBIND11_BUILTINS_MODULE "__builtin__"
  693. +// Providing a separate PyInit decl to make Clang's -Wmissing-prototypes happy.
  694. +// See comment for PYBIND11_MODULE below for why this is marked "maybe unused".
  695. +# define PYBIND11_PLUGIN_IMPL(name) \
  696. + static PyObject *pybind11_init_wrapper(); \
  697. + extern "C" PYBIND11_MAYBE_UNUSED PYBIND11_EXPORT void init##name(); \
  698. + extern "C" PYBIND11_EXPORT void init##name() { (void) pybind11_init_wrapper(); } \
  699. + PyObject *pybind11_init_wrapper()
  700. +#endif
  701. #define PYBIND11_TRY_NEXT_OVERLOAD ((PyObject *) 1) // special failure return code
  702. #define PYBIND11_STRINGIFY(x) #x
  703. @@ -397,15 +426,31 @@ PYBIND11_WARNING_POP
  704. } \
  705. }
  706. -#define PYBIND11_CATCH_INIT_EXCEPTIONS \
  707. - catch (pybind11::error_already_set & e) { \
  708. - pybind11::raise_from(e, PyExc_ImportError, "initialization failed"); \
  709. - return nullptr; \
  710. - } \
  711. - catch (const std::exception &e) { \
  712. - ::pybind11::set_error(PyExc_ImportError, e.what()); \
  713. - return nullptr; \
  714. - }
  715. +#if PY_VERSION_HEX >= 0x03030000
  716. +
  717. +# define PYBIND11_CATCH_INIT_EXCEPTIONS \
  718. + catch (pybind11::error_already_set & e) { \
  719. + pybind11::raise_from(e, PyExc_ImportError, "initialization failed"); \
  720. + return nullptr; \
  721. + } \
  722. + catch (const std::exception &e) { \
  723. + ::pybind11::set_error(PyExc_ImportError, e.what()); \
  724. + return nullptr; \
  725. + }
  726. +
  727. +#else
  728. +
  729. +# define PYBIND11_CATCH_INIT_EXCEPTIONS \
  730. + catch (pybind11::error_already_set & e) { \
  731. + PyErr_SetString(PyExc_ImportError, e.what()); \
  732. + return nullptr; \
  733. + } \
  734. + catch (const std::exception &e) { \
  735. + PyErr_SetString(PyExc_ImportError, e.what()); \
  736. + return nullptr; \
  737. + }
  738. +
  739. +#endif
  740. /** \rst
  741. ***Deprecated in favor of PYBIND11_MODULE***
  742. --- contrib/libs/pybind11/include/pybind11/detail/internals.h (54a9461a4d6f0255e1b2e17444ac91a53daeb018)
  743. +++ contrib/libs/pybind11/include/pybind11/detail/internals.h (index)
  744. @@ -316,6 +316,7 @@ inline internals **&get_internals_pp() {
  745. return internals_pp;
  746. }
  747. +#if PY_VERSION_HEX >= 0x03030000
  748. // forward decl
  749. inline void translate_exception(std::exception_ptr);
  750. @@ -339,11 +340,21 @@ bool handle_nested_exception(const T &exc, const std::exception_ptr &p) {
  751. return false;
  752. }
  753. +#else
  754. +
  755. +template <class T>
  756. +bool handle_nested_exception(const T &, std::exception_ptr &) {
  757. + return false;
  758. +}
  759. +#endif
  760. +
  761. inline bool raise_err(PyObject *exc_type, const char *msg) {
  762. +#if PY_VERSION_HEX >= 0x03030000
  763. if (PyErr_Occurred()) {
  764. raise_from(exc_type, msg);
  765. return true;
  766. }
  767. +#endif
  768. set_error(exc_type, msg);
  769. return false;
  770. }
  771. --- contrib/libs/pybind11/include/pybind11/detail/type_caster_base.h (54a9461a4d6f0255e1b2e17444ac91a53daeb018)
  772. +++ contrib/libs/pybind11/include/pybind11/detail/type_caster_base.h (index)
  773. @@ -446,10 +446,17 @@ PYBIND11_NOINLINE void instance::allocate_layout() {
  774. // efficient for small allocations like the one we're doing here;
  775. // for larger allocations they are just wrappers around malloc.
  776. // TODO: is this still true for pure Python 3.6?
  777. +#if PY_VERSION_HEX >= 0x03050000
  778. nonsimple.values_and_holders = (void **) PyMem_Calloc(space, sizeof(void *));
  779. if (!nonsimple.values_and_holders) {
  780. throw std::bad_alloc();
  781. }
  782. +#else
  783. + nonsimple.values_and_holders = (void **) PyMem_New(void *, space);
  784. + if (!nonsimple.values_and_holders)
  785. + throw std::bad_alloc();
  786. + std::memset(nonsimple.values_and_holders, 0, space * sizeof(void *));
  787. +#endif
  788. nonsimple.status
  789. = reinterpret_cast<std::uint8_t *>(&nonsimple.values_and_holders[flags_at]);
  790. }
  791. @@ -487,6 +494,8 @@ PYBIND11_NOINLINE handle get_object_handle(const void *ptr, const detail::type_i
  792. inline PyThreadState *get_thread_state_unchecked() {
  793. #if defined(PYPY_VERSION)
  794. return PyThreadState_GET();
  795. +#elif PY_VERSION_HEX < 0x03000000
  796. + return _PyThreadState_Current;
  797. #elif PY_VERSION_HEX < 0x030D0000
  798. return _PyThreadState_UncheckedGet();
  799. #else
  800. --- contrib/libs/pybind11/include/pybind11/detail/internals.h (index)
  801. +++ contrib/libs/pybind11/include/pybind11/detail/internals.h (working tree)
  802. @@ -460,8 +460,12 @@ inline object get_python_state_dict() {
  803. }
  804. #endif
  805. if (!state_dict) {
  806. +#if PY_VERSION_HEX >= 0x03030000
  807. raise_from(PyExc_SystemError, "pybind11::detail::get_python_state_dict() FAILED");
  808. throw error_already_set();
  809. +#else
  810. + PyErr_SetString(PyExc_SystemError, "pybind11::detail::get_python_state_dict() FAILED");
  811. +#endif
  812. }
  813. return state_dict;
  814. }
  815. @@ -472,8 +476,12 @@ inline object get_internals_obj_from_state_dict(handle state_dict) {
  816. inline internals **get_internals_pp_from_capsule(handle obj) {
  817. void *raw_ptr = PyCapsule_GetPointer(obj.ptr(), /*name=*/nullptr);
  818. if (raw_ptr == nullptr) {
  819. +#if PY_VERSION_HEX >= 0x03030000
  820. raise_from(PyExc_SystemError, "pybind11::detail::get_internals_pp_from_capsule() FAILED");
  821. throw error_already_set();
  822. +#else
  823. + PyErr_SetString(PyExc_SystemError, "pybind11::detail::get_internals_pp_from_capsule() FAILED");
  824. +#endif
  825. }
  826. return static_cast<internals **>(raw_ptr);
  827. }
  828. --- contrib/libs/pybind11/include/pybind11/gil.h (index)
  829. +++ contrib/libs/pybind11/include/pybind11/gil.h (working tree)
  830. @@ -141,7 +141,9 @@ class gil_scoped_release {
  831. public:
  832. // PRECONDITION: The GIL must be held when this constructor is called.
  833. explicit gil_scoped_release(bool disassoc = false) : disassoc(disassoc) {
  834. +#ifdef PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF
  835. assert(PyGILState_Check());
  836. +#endif
  837. // `get_internals()` must be called here unconditionally in order to initialize
  838. // `internals.tstate` for subsequent `gil_scoped_acquire` calls. Otherwise, an
  839. // initialization race could occur as multiple threads try `gil_scoped_acquire`.
  840. @@ -207,7 +209,9 @@ class gil_scoped_release {
  841. public:
  842. // PRECONDITION: The GIL must be held when this constructor is called.
  843. gil_scoped_release() {
  844. +#ifdef PYBIND11_ASSERT_GIL_HELD_INCREF_DECREF
  845. assert(PyGILState_Check());
  846. +#endif
  847. state = PyEval_SaveThread();
  848. }
  849. gil_scoped_release(const gil_scoped_release &) = delete;
  850. --- contrib/libs/pybind11/include/pybind11/detail/internals.h (index)
  851. +++ contrib/libs/pybind11/include/pybind11/detail/internals.h (working tree)
  852. @@ -92,6 +92,23 @@ inline PyObject *make_object_base_type(PyTypeObject *metaclass);
  853. # define PYBIND11_TLS_REPLACE_VALUE(key, value) PyThread_tss_set(&(key), (value))
  854. # define PYBIND11_TLS_DELETE_VALUE(key) PyThread_tss_set(&(key), nullptr)
  855. # define PYBIND11_TLS_FREE(key) PyThread_tss_delete(&(key))
  856. +#elif PY_MAJOR_VERSION < 3
  857. +// Usually an int but a long on Cygwin64 with Python 3.x
  858. +# define PYBIND11_TLS_KEY_REF decltype(PyThread_create_key())
  859. +# define PYBIND11_TLS_KEY_INIT(var) PYBIND11_TLS_KEY_REF var = 0;
  860. +# define PYBIND11_TLS_KEY_CREATE(var) (((var) = PyThread_create_key()) != -1)
  861. +# define PYBIND11_TLS_GET_VALUE(key) PyThread_get_key_value((key))
  862. +// On CPython < 3.4 and on PyPy, `PyThread_set_key_value` strangely does not set
  863. +// the value if it has already been set. Instead, it must first be deleted and
  864. +// then set again.
  865. +inline void tls_replace_value(PYBIND11_TLS_KEY_REF key, void *value) {
  866. + PyThread_delete_key_value(key);
  867. + PyThread_set_key_value(key, value);
  868. +}
  869. +# define PYBIND11_TLS_DELETE_VALUE(key) PyThread_delete_key_value(key)
  870. +# define PYBIND11_TLS_REPLACE_VALUE(key, value) \
  871. + ::pybind11::detail::tls_replace_value((key), (value))
  872. +# define PYBIND11_TLS_FREE(key) (void) key
  873. #else
  874. # define PYBIND11_TLS_KEY_REF Py_tss_t *
  875. # define PYBIND11_TLS_KEY_INIT(var) Py_tss_t *var = nullptr;