From e2f1bf29b0896332d3987cd21940e9c0b64abe4f Mon Sep 17 00:00:00 2001 From: A Aswanth Raj Date: Tue, 6 Oct 2026 12:10:10 +0530 Subject: [PATCH] Merge pull request #30135 from aswanth-07:fix/python-function-module fix(python): set module metadata on generated functions - #30135 Fixes #19349. Generated Python functions such as `cv2.GaussianBlur` expose `__module__ = None`, so `inspect.getmodule()` returns `None`. Pass the defining module name to `PyCFunction_NewEx` during registration. The existing `PySafeObject` wrapper manages the string reference. Function definitions, call flags, `__self__` and namespace ownership stay unchanged. The tests cover both expressions from the ticket and module lookup, native-callable classification and pickle identity for the root, `cv2.utils` and `cv2.utils.nested`. The expected ticket output is: ```text cv2 ``` CPython represents native extension functions as `BuiltinFunctionType`, including `math.sin`. This patch preserves that representation while supplying the missing module metadata. See the [Python C API documentation](https://docs.python.org/3.13/c-api/structures.html#c.PyCFunction_Type). ### Validation - Compiled isolated checks using the pristine and patched `createSubmodule` and `init_submodule` code and the unchanged `PySafeObject` wrapper. Root and nested call, constant, native type, representation and pickle controls pass on both. The pristine module assertion fails with `None`; patched module metadata and `inspect.getmodule()` pass. These checks use an echo callback and validate registration, rather than an OpenCV kernel. - The ticket regression fails with `None != 'cv2'` against the installed OpenCV 4.12.0 binary. Three existing namespace and class-alias tests and four native-function representation controls pass against that binary and Python's standard library. - `py -3.13 -m py_compile modules/python/test/test_misc.py` and `git diff --check` pass. I have not built the full checkout locally. The [current-head CI workflow](https://github.com/opencv/opencv/actions/runs/37276602842) is awaiting maintainer approval (`action_required`) with no jobs yet. The updated binding tests have not run in CI. On the previous head, `8600aa69`, the Python suite reported 291 tests with 18 skips and `test_function_module` passed. The new ticket regression was added afterward, so those results do not validate it. An earlier macOS x64 native stitching test aborted with exit 134 outside this patch; its maintainer-triggered rerun was queued before this update. ### Pull Request Readiness Checklist See https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request. - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable The regressions use existing Python functions and require no external test data or performance benchmark. - The feature-documentation and sample-build item does not apply to this metadata bug fix. ### AI assistance disclosure AI assistance disclosure: AI-assisted development tools were used during investigation and implementation. The reported tests were run against the final diff. --- modules/python/src2/cv2.cpp | 11 ++++++++++- modules/python/test/test_misc.py | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index 306559f0e3..2a19611344 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -255,9 +255,18 @@ static bool init_submodule(PyObject * root, const char * name, PyMethodDef * met } // populate module's dict PyObject * d = PyModule_GetDict(submodule); + PySafeObject module_name(PyString_FromString(name)); + if (!module_name) + { + return false; + } for (PyMethodDef * m = methods; m->ml_name != NULL; ++m) { - PyObject * method_obj = PyCFunction_NewEx(m, NULL, NULL); + PyObject * method_obj = PyCFunction_NewEx(m, NULL, module_name); + if (!method_obj) + { + return false; + } if (PyDict_SetItemString(d, m->ml_name, method_obj) < 0) { PyErr_Format(PyExc_ImportError, diff --git a/modules/python/test/test_misc.py b/modules/python/test/test_misc.py index da2f9a448d..7a36870c9b 100644 --- a/modules/python/test/test_misc.py +++ b/modules/python/test/test_misc.py @@ -795,6 +795,23 @@ class Arguments(NewOpenCVTests): self.assertTrue(isinstance(rr, tuple), msg=type(rrv)) self.assertEqual(len(rr), 3) + def test_function_module_issue_19349(self): + self.assertEqual(cv.GaussianBlur.__module__, "cv2") + self.assertEqual(str(cv.GaussianBlur), "") + + def test_function_module(self): + import inspect + import pickle + + for module, function in ((cv, cv.GaussianBlur), + (cv.utils, cv.utils.dumpInputArray), + (cv.utils.nested, cv.utils.nested.testEchoBooleanFunction)): + self.assertEqual(function.__module__, module.__name__) + self.assertIs(inspect.getmodule(function), module) + self.assertTrue(inspect.isbuiltin(function)) + self.assertIsNone(function.__self__) + self.assertIs(pickle.loads(pickle.dumps(function)), function) + def test_nested_function_availability(self): self.assertTrue(hasattr(cv.utils, "nested"), msg="Module is not generated for nested namespace")