pitrou commented on code in PR #48727:
URL: https://github.com/apache/arrow/pull/48727#discussion_r2853099384
##########
python/pyarrow/src/arrow/python/helpers.cc:
##########
@@ -296,6 +296,71 @@ bool PyFloat_IsNaN(PyObject* obj) {
namespace {
+// UUID module static data - lazily initialized on first use
+// Uses a conditional initialization strategy: std::once_flag when the GIL is
+// disabled, or a simple boolean flag when the GIL is enabled.
+// See the Pandas static data section below and ARROW-10519 for more details.
+#ifdef Py_GIL_DISABLED
+static std::once_flag uuid_static_initialized;
+#else
+static bool uuid_static_initialized = false;
+#endif
+static PyObject* uuid_UUID = nullptr;
+
+void GetUuidStaticSymbols() {
+ OwnedRef uuid_module;
+
+ // Import uuid module
+ Status s = ImportModule("uuid", &uuid_module);
+ if (!s.ok()) {
+ return;
+ }
+
+#ifndef Py_GIL_DISABLED
+ // Since ImportModule can release the GIL, another thread could have
+ // already initialized the static data.
+ if (uuid_static_initialized) {
+ return;
+ }
+#endif
+
+ OwnedRef ref;
+ if (ImportFromModule(uuid_module.obj(), "UUID", &ref).ok()) {
+ uuid_UUID = ref.obj();
+ }
+}
+
+#ifdef Py_GIL_DISABLED
+void InitUuidStaticData() {
+ std::call_once(uuid_static_initialized, GetUuidStaticSymbols);
+}
+#else
+void InitUuidStaticData() {
+ // NOTE: This is called with the GIL held. We needn't (and shouldn't,
+ // to avoid deadlocks) use an additional C++ lock (ARROW-10519).
+ if (uuid_static_initialized) {
+ return;
+ }
+ GetUuidStaticSymbols();
+ uuid_static_initialized = true;
+}
+#endif
Review Comment:
We're duplicating code here between different module imports. It would be
really nice to write something like this:
```c++
struct UuidModuleData {
PyObject* UUID_class = nullptr;
};
UuidModuleData* InitUuidStaticData() {
static ModuleOnceRunner runner("uuid");
return runner.Run([&](OwnedRef module) -> UuidModuleData {
UuidModuleData data;
OwnedRef ref;
if (ImportFromModule(module.obj(), "UUID", &ref).ok()) {
data.UUID_class = ref.obj();
}
return data;
});
}
```
```c++
struct ModuleOnceRunner {
std::string module_name;
#ifdef Py_GIL_DISABLED
std::once_flag initialized;
#else
bool initialized = false;
#endif
template <typename Func>
auto Run(Func&& func) -> decltype(func(OwnedRef()) {
using RetType = decltype(func(OwnedRef());
RetType ret{};
auto wrapper_func = [&]() {
OwnerRef module;
if (ImportModule("uuid", &module).ok()) {
ret = func(std::move(module));
}
};
#ifdef Py_GIL_DISABLED
std::call_once(initialized, wrapper_func);
#else
if (!initialized) {
initialized = true;
wrapper_func();
}
#endif
return ret;
};
};
```
I think @rok can help.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]