|
16 | 16 | #include "NanobindUtils.h"
|
17 | 17 | #include "mlir-c/BuiltinAttributes.h"
|
18 | 18 | #include "mlir-c/BuiltinTypes.h"
|
19 |
| -#include "mlir/Bindings/Python/NanobindAdaptors.h" |
20 | 19 | #include "mlir/Bindings/Python/Nanobind.h"
|
| 20 | +#include "mlir/Bindings/Python/NanobindAdaptors.h" |
21 | 21 | #include "llvm/ADT/ScopeExit.h"
|
22 | 22 | #include "llvm/Support/raw_ostream.h"
|
23 | 23 |
|
@@ -1428,6 +1428,12 @@ class PyDenseIntElementsAttribute
|
1428 | 1428 | }
|
1429 | 1429 | };
|
1430 | 1430 |
|
| 1431 | +// Check if the python version is less than 3.13. Py_IsFinalizing is a part |
| 1432 | +// of stable ABI since 3.13 and before it was available as _Py_IsFinalizing. |
| 1433 | +#if PY_VERSION_HEX < 0x030d0000 |
| 1434 | +#define Py_IsFinalizing _Py_IsFinalizing |
| 1435 | +#endif |
| 1436 | + |
1431 | 1437 | class PyDenseResourceElementsAttribute
|
1432 | 1438 | : public PyConcreteAttribute<PyDenseResourceElementsAttribute> {
|
1433 | 1439 | public:
|
@@ -1474,8 +1480,9 @@ class PyDenseResourceElementsAttribute
|
1474 | 1480 | // The userData is a Py_buffer* that the deleter owns.
|
1475 | 1481 | auto deleter = [](void *userData, const void *data, size_t size,
|
1476 | 1482 | size_t align) {
|
1477 |
| - if (!Py_IsInitialized()) |
1478 |
| - Py_Initialize(); |
| 1483 | + if (Py_IsFinalizing()) |
| 1484 | + return; |
| 1485 | + assert(Py_IsInitialized() && "expected interpreter to be initialized"); |
1479 | 1486 | Py_buffer *ownedView = static_cast<Py_buffer *>(userData);
|
1480 | 1487 | nb::gil_scoped_acquire gil;
|
1481 | 1488 | PyBuffer_Release(ownedView);
|
|
0 commit comments