fix: submodules creation and registration

- Add special case handling when submodule has the same name as parent
- `PyDict_SetItemString` doesn't steal reference, so reference count
  should be explicitly decremented to transfer object life-time
  ownership
- Add sanity checks for module registration input
- Add Python 2 and Python 3 reference counting handling
This commit is contained in:
Vadim Levin
2022-01-19 00:07:50 +03:00
parent 6ae8103022
commit d88730685e
3 changed files with 200 additions and 36 deletions
+27
View File
@@ -1,6 +1,7 @@
#!/usr/bin/env python
from __future__ import print_function
import sys
import ctypes
from functools import partial
from collections import namedtuple
@@ -607,6 +608,32 @@ class Arguments(NewOpenCVTests):
self.assertTrue(isinstance(rr, tuple), msg=type(rrv))
self.assertEqual(len(rr), 3)
def test_nested_function_availability(self):
self.assertTrue(hasattr(cv.utils, "nested"),
msg="Module is not generated for nested namespace")
self.assertTrue(hasattr(cv.utils.nested, "testEchoBooleanFunction"),
msg="Function in nested module is not available")
if sys.version_info[0] < 3:
# Nested submodule is managed only by the global submodules dictionary
# and parent native module
expected_ref_count = 2
else:
# Nested submodule is managed by the global submodules dictionary,
# parent native module and Python part of the submodule
expected_ref_count = 3
# `getrefcount` temporary increases reference counter by 1
actual_ref_count = sys.getrefcount(cv.utils.nested) - 1
self.assertEqual(actual_ref_count, expected_ref_count,
msg="Nested submodule reference counter has wrong value\n"
"Expected: {}. Actual: {}".format(expected_ref_count, actual_ref_count))
for flag in (True, False):
self.assertEqual(flag, cv.utils.nested.testEchoBooleanFunction(flag),
msg="Function in nested module returns wrong result")
class CanUsePurePythonModuleFunction(NewOpenCVTests):
def test_can_get_ocv_version(self):
import sys