Skip to content

Commit 6d1c02f

Browse files
Alexander MordvintsevAlexander Mordvintsev
authored andcommitted
unified namespace population with 'init_submodule'
1 parent 4a519a2 commit 6d1c02f

File tree

3 files changed

+17
-21
lines changed

3 files changed

+17
-21
lines changed

modules/python/common.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ ocv_list_filterout(opencv_hdrs "opencv2/optim.hpp")
4848
set(cv2_generated_hdrs
4949
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_include.h"
5050
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_funcs.h"
51-
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_func_tab.h"
5251
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_types.h"
5352
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_type_reg.h"
5453
"${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_const_reg.h"

modules/python/src2/cv2.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,8 +1193,6 @@ static int convert_to_char(PyObject *o, char *dst, const char *name = "no_name")
11931193
#include "pyopencv_generated_funcs.h"
11941194

11951195
static PyMethodDef methods[] = {
1196-
1197-
#include "pyopencv_generated_func_tab.h"
11981196
{"createTrackbar", pycvCreateTrackbar, METH_VARARGS, "createTrackbar(trackbarName, windowName, value, count, onChange) -> None"},
11991197
{"setMouseCallback", (PyCFunction)pycvSetMouseCallback, METH_VARARGS | METH_KEYWORDS, "setMouseCallback(windowName, onMouse [, param]) -> None"},
12001198
{NULL, NULL},
@@ -1205,9 +1203,10 @@ static PyMethodDef methods[] = {
12051203

12061204
static void init_submodule(PyObject * root, const char * name, PyMethodDef * methods)
12071205
{
1206+
// traverse and create nested submodules
12081207
std::string s = name;
1209-
size_t i = s.find('.')+1; // assume, that name is cv2.<name>...
1210-
while (i < s.length())
1208+
size_t i = s.find('.');
1209+
while (i < s.length() && i != std::string::npos)
12111210
{
12121211
size_t j = s.find('.', i);
12131212
if (j == std::string::npos)
@@ -1226,6 +1225,7 @@ static void init_submodule(PyObject * root, const char * name, PyMethodDef * met
12261225
root = submod;
12271226
}
12281227

1228+
// populate module's dict
12291229
PyObject * d = PyModule_GetDict(root);
12301230
for (PyMethodDef * m = methods; m->ml_name != NULL; ++m)
12311231
{

modules/python/src2/gen2.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -731,18 +731,23 @@ def gen_code(self, all_classes):
731731
return code
732732

733733

734+
class Namespace(object):
735+
def __init__(self):
736+
self.funcs = {}
737+
self.consts = {}
738+
739+
734740
class PythonWrapperGenerator(object):
735741
def __init__(self):
736742
self.clear()
737743

738744
def clear(self):
739745
self.classes = {}
740-
self.ns_funcs = {}
746+
self.namespaces = {}
741747
self.consts = {}
742748
self.code_include = StringIO()
743749
self.code_types = StringIO()
744750
self.code_funcs = StringIO()
745-
self.code_func_tab = StringIO()
746751
self.code_type_reg = StringIO()
747752
self.code_const_reg = StringIO()
748753
self.code_ns_reg = StringIO()
@@ -802,7 +807,7 @@ def add_func(self, decl):
802807
cname = chunks[-1]
803808
func_map = self.classes[classname].methods
804809
else:
805-
func_map = self.ns_funcs.setdefault(namespace, {})
810+
func_map = self.namespaces.setdefault(namespace, Namespace()).funcs
806811

807812
func = func_map.setdefault(name, FuncInfo(classname, name, cname, isconstructor, namespace))
808813
func.add_variant(decl)
@@ -813,16 +818,14 @@ def gen_const_reg(self, constinfo):
813818
def gen_namespace(self, ns_name):
814819
wname = normalize_class_name(ns_name)
815820
self.code_ns_reg.write('static PyMethodDef methods_%s[] = {\n'%wname)
816-
funclist = sorted(self.ns_funcs[ns_name].items())
821+
funclist = sorted(self.namespaces[ns_name].funcs.items())
817822
for name, func in funclist:
818823
self.code_ns_reg.write(func.get_tab_entry())
819824
self.code_ns_reg.write(' {NULL, NULL}\n};\n\n')
820825

821826
def gen_namespaces_reg(self):
822827
self.code_ns_reg.write('static void init_submodules(PyObject * root) \n{\n')
823-
for ns_name in sorted(self.ns_funcs):
824-
if ns_name == 'cv':
825-
continue
828+
for ns_name in sorted(self.namespaces):
826829
wname = normalize_class_name(ns_name)
827830
self.code_ns_reg.write(' init_submodule(root, MODULESTR"%s", methods_%s);\n' % (ns_name[2:], wname))
828831
self.code_ns_reg.write('};\n')
@@ -884,16 +887,11 @@ def gen(self, srcfiles, output_path):
884887
self.code_type_reg.write("MKTYPE2(%s);\n" % (classinfo.name,) )
885888

886889
# step 3: generate the code for all the global functions
887-
for ns in self.ns_funcs:
888-
funclist = self.ns_funcs[ns].items()
889-
funclist.sort()
890-
for name, func in funclist:
890+
for ns_name, ns in sorted(self.namespaces.items()):
891+
for name, func in sorted(ns.funcs.items()):
891892
code = func.gen_code(self.classes)
892893
self.code_funcs.write(code)
893-
if ns == 'cv':
894-
self.code_func_tab.write(func.get_tab_entry())
895-
if ns != 'cv':
896-
self.gen_namespace(ns)
894+
self.gen_namespace(ns_name)
897895
self.gen_namespaces_reg()
898896

899897
# step 4: generate the code for constants
@@ -905,7 +903,6 @@ def gen(self, srcfiles, output_path):
905903
# That's it. Now save all the files
906904
self.save(output_path, "pyopencv_generated_include.h", self.code_include)
907905
self.save(output_path, "pyopencv_generated_funcs.h", self.code_funcs)
908-
self.save(output_path, "pyopencv_generated_func_tab.h", self.code_func_tab)
909906
self.save(output_path, "pyopencv_generated_const_reg.h", self.code_const_reg)
910907
self.save(output_path, "pyopencv_generated_types.h", self.code_types)
911908
self.save(output_path, "pyopencv_generated_type_reg.h", self.code_type_reg)

0 commit comments

Comments
 (0)