feature: submodule or a class scope for exported classes

All classes are registered in the scope that corresponds to C++
namespace or exported class.

Example:
`cv::ml::Boost` is exported as `cv.ml.Boost`
`cv::SimpleBlobDetector::Params` is exported as
`cv.SimpleBlobDetector.Params`

For backward compatibility all classes are registered in the global
module with their mangling name containing scope information.
Example:
`cv::ml::Boost` has `cv.ml_Boost` alias to `cv.ml.Boost` type
This commit is contained in:
Vadim Levin
2022-01-20 15:21:47 +03:00
parent 5cc27fd3b5
commit ccebbbc0ac
5 changed files with 484 additions and 93 deletions
+85
View File
@@ -618,6 +618,91 @@ class Arguments(NewOpenCVTests):
self.assertEqual(flag, cv.utils.nested.testEchoBooleanFunction(flag),
msg="Function in nested module returns wrong result")
def test_class_from_submodule_has_global_alias(self):
self.assertTrue(hasattr(cv.ml, "Boost"),
msg="Class is not registered in the submodule")
self.assertTrue(hasattr(cv, "ml_Boost"),
msg="Class from submodule doesn't have alias in the "
"global module")
self.assertEqual(cv.ml.Boost, cv.ml_Boost,
msg="Classes from submodules and global module don't refer "
"to the same type")
def test_inner_class_has_global_alias(self):
self.assertTrue(hasattr(cv.SimpleBlobDetector, "Params"),
msg="Class is not registered as inner class")
self.assertEqual(cv.SimpleBlobDetector.Params, cv.SimpleBlobDetector_Params,
msg="Inner class and class in global module don't refer "
"to the same type")
self.assertTrue(hasattr(cv, "SimpleBlobDetector_Params"),
msg="Inner class doesn't have alias in the global module")
def test_class_from_submodule_has_global_alias(self):
self.assertTrue(hasattr(cv.ml, "Boost"),
msg="Class is not registered in the submodule")
self.assertTrue(hasattr(cv, "ml_Boost"),
msg="Class from submodule doesn't have alias in the "
"global module")
self.assertEqual(cv.ml.Boost, cv.ml_Boost,
msg="Classes from submodules and global module don't refer "
"to the same type")
def test_inner_class_has_global_alias(self):
self.assertTrue(hasattr(cv.SimpleBlobDetector, "Params"),
msg="Class is not registered as inner class")
self.assertTrue(hasattr(cv, "SimpleBlobDetector_Params"),
msg="Inner class doesn't have alias in the global module")
self.assertEqual(cv.SimpleBlobDetector.Params, cv.SimpleBlobDetector_Params,
msg="Inner class and class in global module don't refer "
"to the same type")
self.assertTrue(hasattr(cv, "SimpleBlobDetector_Params"),
msg="Inner class doesn't have alias in the global module")
def test_export_class_with_different_name(self):
self.assertTrue(hasattr(cv.utils.nested, "ExportClassName"),
msg="Class with export alias is not registered in the submodule")
self.assertTrue(hasattr(cv, "utils_nested_ExportClassName"),
msg="Class with export alias doesn't have alias in the "
"global module")
self.assertEqual(cv.utils.nested.ExportClassName.originalName(), "OriginalClassName")
instance = cv.utils.nested.ExportClassName.create()
self.assertTrue(isinstance(instance, cv.utils.nested.ExportClassName),
msg="Factory function returns wrong class instance: {}".format(type(instance)))
self.assertTrue(hasattr(cv.utils.nested, "ExportClassName_create"),
msg="Factory function should have alias in the same module as the class")
# self.assertFalse(hasattr(cv.utils.nested, "OriginalClassName_create"),
# msg="Factory function should not be registered with original class name, "\
# "when class has different export name")
def test_export_inner_class_of_class_exported_with_different_name(self):
if not hasattr(cv.utils.nested, "ExportClassName"):
raise unittest.SkipTest("Outer class with export alias is not registered in the submodule")
self.assertTrue(hasattr(cv.utils.nested.ExportClassName, "Params"),
msg="Inner class with export alias is not registered in "
"the outer class")
self.assertTrue(hasattr(cv, "utils_nested_ExportClassName_Params"),
msg="Inner class with export alias is not registered in "
"global module")
params = cv.utils.nested.ExportClassName.Params()
params.int_value = 45
params.float_value = 4.5
instance = cv.utils.nested.ExportClassName.create(params)
self.assertTrue(isinstance(instance, cv.utils.nested.ExportClassName),
msg="Factory function returns wrong class instance: {}".format(type(instance)))
self.assertEqual(
params.int_value, instance.getIntParam(),
msg="Class initialized with wrong integer parameter. Expected: {}. Actual: {}".format(
params.int_value, instance.getIntParam()
))
self.assertEqual(
params.float_value, instance.getFloatParam(),
msg="Class initialized with wrong integer parameter. Expected: {}. Actual: {}".format(
params.float_value, instance.getFloatParam()
))
class SamplesFindFile(NewOpenCVTests):