openexr icon indicating copy to clipboard operation
openexr copied to clipboard

Python module import crash (Windows)

Open boberfly opened this issue 2 months ago • 1 comments

Hi all,

On this line: https://github.com/AcademySoftwareFoundation/openexr/blob/37ccf5c710e30f38699e1906c8021c0722899a47/src/wrappers/python/PyOpenEXR_old.cpp#L1574

We've found that OpenEXR_error returns nullptr and will crash when it is run in the next few lines in PyDict_SetItemString. I've made the following patch to bypass this:

--- a/src/wrappers/python/PyOpenEXR_old.cpp	2025-10-20 13:41:28.625622100 +1100
+++ b/src/wrappers/python/PyOpenEXR_old.cpp	2025-10-20 13:41:35.439505100 +1100
@@ -1572,8 +1572,11 @@
     PyModule_AddObject (module, "OutputFile", (PyObject*) &OutputFile_Type);
 
     OpenEXR_error = PyErr_NewException ((char*) "OpenEXR.error", NULL, NULL);
-    PyDict_SetItemString (moduleDict, "error", OpenEXR_error);
-    Py_DECREF (OpenEXR_error);
+    if (OpenEXR_error)
+    {
+        PyDict_SetItemString (moduleDict, "error", OpenEXR_error);
+        Py_DECREF (OpenEXR_error);
+    }
 
     PyObject *item;
 

But I'm not entirely familiar with the codebase to know if this is a valid workaround or it is just bypassing something else. With this workaround I'm able to import the module successfully without a crash.

I built this with the following: MSVC 14.43.x Windows 10 Python 3.11.4 Pybind11 2.9.2

Kind regards -Alex

boberfly avatar Oct 20 '25 03:10 boberfly

Apologies, just seeing this now. A little poking around says there are several things that could cause PyErr_NewException to fail on Windows, so the fix might indeed be masking a deeper problem.

However, this is using the legacy OpenEXR python API, which is deprecated and will be removed in the next major release. Please use the new API, describe here: https://pypi.org/project/OpenEXR/#description

cary-ilm avatar Nov 03 '25 17:11 cary-ilm