Merge pull request #983 from HapeMask:python3-compatibility

pull/1003/merge
Roman Donchenko 12 years ago committed by OpenCV Buildbot
commit d998e38a12
  1. 6
      cmake/OpenCVDetectPython.cmake
  2. 2
      modules/python/CMakeLists.txt
  3. 32
      modules/python/src2/cv2.cpp
  4. 81
      modules/python/src2/gen2.py
  5. 51
      modules/python/src2/hdr_parser.py
  6. 64
      modules/python/src2/pycompat.hpp

@ -49,7 +49,7 @@ if(PYTHON_EXECUTABLE)
if(NOT ANDROID AND NOT IOS)
if(CMAKE_HOST_UNIX)
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import *; print get_python_lib()"
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import *; print(get_python_lib())"
RESULT_VARIABLE PYTHON_CVPY_PROCESS
OUTPUT_VARIABLE PYTHON_STD_PACKAGES_PATH
OUTPUT_STRIP_TRAILING_WHITESPACE)
@ -80,7 +80,7 @@ if(PYTHON_EXECUTABLE)
if(NOT PYTHON_NUMPY_INCLUDE_DIR)
# Attempt to discover the NumPy include directory. If this succeeds, then build python API with NumPy
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import os; os.environ['DISTUTILS_USE_SDK']='1'; import numpy.distutils; print numpy.distutils.misc_util.get_numpy_include_dirs()[0]"
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import os; os.environ['DISTUTILS_USE_SDK']='1'; import numpy.distutils; print(numpy.distutils.misc_util.get_numpy_include_dirs()[0])"
RESULT_VARIABLE PYTHON_NUMPY_PROCESS
OUTPUT_VARIABLE PYTHON_NUMPY_INCLUDE_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE)
@ -92,7 +92,7 @@ if(PYTHON_EXECUTABLE)
endif()
if(PYTHON_NUMPY_INCLUDE_DIR)
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import numpy; print numpy.version.version"
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import numpy; print(numpy.version.version)"
RESULT_VARIABLE PYTHON_NUMPY_PROCESS
OUTPUT_VARIABLE PYTHON_NUMPY_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE)

@ -67,7 +67,7 @@ else()
endif()
target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS})
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import distutils.sysconfig; print distutils.sysconfig.get_config_var('SO')"
execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import distutils.sysconfig; print(distutils.sysconfig.get_config_var('SO'))"
RESULT_VARIABLE PYTHON_CVPY_PROCESS
OUTPUT_VARIABLE CVPY_SUFFIX
OUTPUT_STRIP_TRAILING_WHITESPACE)

@ -23,6 +23,8 @@
# include "opencv2/nonfree.hpp"
#endif
#include "pycompat.hpp"
using cv::flann::IndexParams;
using cv::flann::SearchParams;
@ -1176,7 +1178,11 @@ static int convert_to_char(PyObject *o, char *dst, const char *name = "no_name")
}
}
#if PY_MAJOR_VERSION >= 3
#define MKTYPE2(NAME) pyopencv_##NAME##_specials(); if (!to_ok(&pyopencv_##NAME##_Type)) return NULL;
#else
#define MKTYPE2(NAME) pyopencv_##NAME##_specials(); if (!to_ok(&pyopencv_##NAME##_Type)) return
#endif
#ifdef __GNUC__
# pragma GCC diagnostic ignored "-Wunused-parameter"
@ -1190,7 +1196,7 @@ static PyMethodDef methods[] = {
#include "pyopencv_generated_func_tab.h"
{"createTrackbar", pycvCreateTrackbar, METH_VARARGS, "createTrackbar(trackbarName, windowName, value, count, onChange) -> None"},
{"setMouseCallback", (PyCFunction)pycvSetMouseCallback, METH_KEYWORDS, "setMouseCallback(windowName, onMouse [, param]) -> None"},
{"setMouseCallback", (PyCFunction)pycvSetMouseCallback, METH_VARARGS | METH_KEYWORDS, "setMouseCallback(windowName, onMouse [, param]) -> None"},
{NULL, NULL},
};
@ -1205,15 +1211,35 @@ static int to_ok(PyTypeObject *to)
return (PyType_Ready(to) == 0);
}
#if PY_MAJOR_VERSION >= 3
extern "C" CV_EXPORTS PyObject* PyInit_cv2();
static struct PyModuleDef cv2_moduledef =
{
PyModuleDef_HEAD_INIT,
MODULESTR,
"Python wrapper for OpenCV.",
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. */
methods
};
PyObject* PyInit_cv2()
#else
extern "C" CV_EXPORTS void initcv2();
void initcv2()
#endif
{
import_array();
#include "pyopencv_generated_type_reg.h"
#if PY_MAJOR_VERSION >= 3
PyObject* m = PyModule_Create(&cv2_moduledef);
#else
PyObject* m = Py_InitModule(MODULESTR, methods);
#endif
PyObject* d = PyModule_GetDict(m);
PyDict_SetItemString(d, "__version__", PyString_FromString(CV_VERSION));
@ -1262,5 +1288,7 @@ void initcv2()
PUBLISH(CV_64FC4);
#include "pyopencv_generated_const_reg.h"
#if PY_MAJOR_VERSION >= 3
return m;
#endif
}

@ -1,8 +1,14 @@
#!/usr/bin/env python
import hdr_parser, sys, re, os, cStringIO
from __future__ import print_function
import hdr_parser, sys, re, os
from string import Template
if sys.version_info[0] >= 3:
from io import StringIO
else:
from cStringIO import StringIO
ignored_arg_types = ["RNG*"]
gen_template_check_self = Template(""" if(!PyObject_TypeCheck(self, &pyopencv_${name}_Type))
@ -33,6 +39,13 @@ gen_template_func_body = Template("""$code_decl
}
""")
py_major_version = sys.version_info[0]
if py_major_version >= 3:
head_init_str = "PyVarObject_HEAD_INIT(&PyType_Type, 0)"
else:
head_init_str = """PyObject_HEAD_INIT(&PyType_Type)
0,"""
gen_template_simple_type_decl = Template("""
struct pyopencv_${name}_t
{
@ -42,8 +55,7 @@ struct pyopencv_${name}_t
static PyTypeObject pyopencv_${name}_Type =
{
PyObject_HEAD_INIT(&PyType_Type)
0,
%s
MODULESTR".$wname",
sizeof(pyopencv_${name}_t),
};
@ -66,13 +78,13 @@ template<> bool pyopencv_to(PyObject* src, ${cname}& dst, const char* name)
return true;
if(!PyObject_TypeCheck(src, &pyopencv_${name}_Type))
{
failmsg("Expected ${cname} for argument '%s'", name);
failmsg("Expected ${cname} for argument '%%s'", name);
return false;
}
dst = ((pyopencv_${name}_t*)src)->v;
return true;
}
""")
""" % head_init_str)
gen_template_type_decl = Template("""
@ -84,8 +96,7 @@ struct pyopencv_${name}_t
static PyTypeObject pyopencv_${name}_Type =
{
PyObject_HEAD_INIT(&PyType_Type)
0,
%s
MODULESTR".$wname",
sizeof(pyopencv_${name}_t),
};
@ -110,14 +121,14 @@ template<> bool pyopencv_to(PyObject* src, Ptr<${cname}>& dst, const char* name)
return true;
if(!PyObject_TypeCheck(src, &pyopencv_${name}_Type))
{
failmsg("Expected ${cname} for argument '%s'", name);
failmsg("Expected ${cname} for argument '%%s'", name);
return false;
}
dst = ((pyopencv_${name}_t*)src)->v;
return true;
}
""")
""" % head_init_str)
gen_template_map_type_cvt = Template("""
template<> bool pyopencv_to(PyObject* src, ${cname}& dst, const char* name);
@ -245,9 +256,9 @@ class ClassInfo(object):
if decl:
self.bases = decl[1].split()[1:]
if len(self.bases) > 1:
print "Note: Class %s has more than 1 base class (not supported by Python C extensions)" % (self.name,)
print " Bases: ", " ".join(self.bases)
print " Only the first base class will be used"
print("Note: Class %s has more than 1 base class (not supported by Python C extensions)" % (self.name,))
print(" Bases: ", " ".join(self.bases))
print(" Only the first base class will be used")
self.bases = [self.bases[0].strip(",")]
#return sys.exit(-1)
if self.bases and self.bases[0].startswith("cv::"):
@ -280,8 +291,8 @@ class ClassInfo(object):
if self.ismap:
return self.gen_map_code(all_classes)
getset_code = cStringIO.StringIO()
getset_inits = cStringIO.StringIO()
getset_code = StringIO()
getset_inits = StringIO()
sorted_props = [(p.name, p) for p in self.props]
sorted_props.sort()
@ -304,10 +315,10 @@ class ClassInfo(object):
getset_code.write(gen_template_set_prop.substitute(name=self.name, member=pname, membertype=p.tp, access=access_op))
getset_inits.write(gen_template_rw_prop_init.substitute(name=self.name, member=pname))
methods_code = cStringIO.StringIO()
methods_inits = cStringIO.StringIO()
methods_code = StringIO()
methods_inits = StringIO()
sorted_methods = self.methods.items()
sorted_methods = list(self.methods.items())
sorted_methods.sort()
for mname, m in sorted_methods:
@ -315,7 +326,7 @@ class ClassInfo(object):
methods_inits.write(m.get_tab_entry())
baseptr = "NULL"
if self.bases and all_classes.has_key(self.bases[0]):
if self.bases and self.bases[0] in all_classes:
baseptr = "&pyopencv_" + all_classes[self.bases[0]].name + "_Type"
code = gen_template_type_impl.substitute(name=self.name, wname=self.wname, cname=self.cname,
@ -532,7 +543,7 @@ class FuncInfo(object):
p2 = s.rfind(")")
docstring_list = [s[:p1+1] + "[" + s[p1+1:p2] + "]" + s[p2:]]
return Template(' {"$py_funcname", (PyCFunction)$wrap_funcname, METH_KEYWORDS, "$py_docstring"},\n'
return Template(' {"$py_funcname", (PyCFunction)$wrap_funcname, METH_VARARGS | METH_KEYWORDS, "$py_docstring"},\n'
).substitute(py_funcname = self.variants[0].wname, wrap_funcname=self.get_wrapper_name(),
py_docstring = " or ".join(docstring_list))
@ -609,7 +620,7 @@ class FuncInfo(object):
defval0 = "0"
tp1 = tp.replace("*", "_ptr")
if tp1.endswith("*"):
print "Error: type with star: a.tp=%s, tp=%s, tp1=%s" % (a.tp, tp, tp1)
print("Error: type with star: a.tp=%s, tp=%s, tp1=%s" % (a.tp, tp, tp1))
sys.exit(-1)
amapping = simple_argtype_mapping.get(tp, (tp, "O", defval0))
@ -715,11 +726,11 @@ class PythonWrapperGenerator(object):
self.classes = {}
self.funcs = {}
self.consts = {}
self.code_types = cStringIO.StringIO()
self.code_funcs = cStringIO.StringIO()
self.code_func_tab = cStringIO.StringIO()
self.code_type_reg = cStringIO.StringIO()
self.code_const_reg = cStringIO.StringIO()
self.code_types = StringIO()
self.code_funcs = StringIO()
self.code_func_tab = StringIO()
self.code_type_reg = StringIO()
self.code_const_reg = StringIO()
self.class_idx = 0
def add_class(self, stype, name, decl):
@ -727,9 +738,9 @@ class PythonWrapperGenerator(object):
classinfo.decl_idx = self.class_idx
self.class_idx += 1
if self.classes.has_key(classinfo.name):
print "Generator error: class %s (cname=%s) already exists" \
% (classinfo.name, classinfo.cname)
if classinfo.name in self.classes:
print("Generator error: class %s (cname=%s) already exists" \
% (classinfo.name, classinfo.cname))
sys.exit(-1)
self.classes[classinfo.name] = classinfo
if classinfo.bases and not classinfo.isalgorithm:
@ -738,9 +749,9 @@ class PythonWrapperGenerator(object):
def add_const(self, name, decl):
constinfo = ConstInfo(name, decl[1])
if self.consts.has_key(constinfo.name):
print "Generator error: constant %s (cname=%s) already exists" \
% (constinfo.name, constinfo.cname)
if constinfo.name in self.consts:
print("Generator error: constant %s (cname=%s) already exists" \
% (constinfo.name, constinfo.cname))
sys.exit(-1)
self.consts[constinfo.name] = constinfo
@ -779,7 +790,7 @@ class PythonWrapperGenerator(object):
else:
classinfo = self.classes.get(classname, ClassInfo(""))
if not classinfo.name:
print "Generator error: the class for method %s is missing" % (name,)
print("Generator error: the class for method %s is missing" % (name,))
sys.exit(-1)
func_map = classinfo.methods
@ -819,7 +830,7 @@ class PythonWrapperGenerator(object):
self.add_func(decl)
# step 2: generate code for the classes and their methods
classlist = self.classes.items()
classlist = list(self.classes.items())
classlist.sort()
for name, classinfo in classlist:
if classinfo.ismap:
@ -844,7 +855,7 @@ class PythonWrapperGenerator(object):
self.code_type_reg.write("MKTYPE2(%s);\n" % (classinfo.name,) )
# step 3: generate the code for all the global functions
funclist = self.funcs.items()
funclist = list(self.funcs.items())
funclist.sort()
for name, func in funclist:
code = func.gen_code(self.classes)
@ -852,7 +863,7 @@ class PythonWrapperGenerator(object):
self.code_func_tab.write(func.get_tab_entry())
# step 4: generate the code for constants
constlist = self.consts.items()
constlist = list(self.consts.items())
constlist.sort()
for name, constinfo in constlist:
self.gen_const_reg(constinfo)

@ -1,5 +1,6 @@
#!/usr/bin/env python
from __future__ import print_function
import os, sys, re, string
# the list only for debugging. The real list, used in the real OpenCV build, is specified in CMakeLists.txt
@ -43,13 +44,13 @@ class CppHeaderParser(object):
def get_macro_arg(self, arg_str, npos):
npos2 = npos3 = arg_str.find("(", npos)
if npos2 < 0:
print "Error: no arguments for the macro at %d" % (self.lineno,)
print("Error: no arguments for the macro at %d" % (self.lineno,))
sys.exit(-1)
balance = 1
while 1:
t, npos3 = self.find_next_token(arg_str, ['(', ')'], npos3+1)
if npos3 < 0:
print "Error: no matching ')' in the macro call at %d" % (self.lineno,)
print("Error: no matching ')' in the macro call at %d" % (self.lineno,))
sys.exit(-1)
if t == '(':
balance += 1
@ -143,13 +144,13 @@ class CppHeaderParser(object):
angle_stack.append(0)
elif w == "," or w == '>':
if not angle_stack:
print "Error at %d: argument contains ',' or '>' not within template arguments" % (self.lineno,)
print("Error at %d: argument contains ',' or '>' not within template arguments" % (self.lineno,))
sys.exit(-1)
if w == ",":
arg_type += "_and_"
elif w == ">":
if angle_stack[0] == 0:
print "Error at %s:%d: template has no arguments" % (self.hname, self.lineno)
print("Error at %s:%d: template has no arguments" % (self.hname, self.lineno))
sys.exit(-1)
if angle_stack[0] > 1:
arg_type += "_end_"
@ -173,7 +174,7 @@ class CppHeaderParser(object):
p1 = arg_name.find("[")
p2 = arg_name.find("]",p1+1)
if p2 < 0:
print "Error at %d: no closing ]" % (self.lineno,)
print("Error at %d: no closing ]" % (self.lineno,))
sys.exit(-1)
counter_str = arg_name[p1+1:p2].strip()
if counter_str == "":
@ -358,7 +359,7 @@ class CppHeaderParser(object):
if bool(re.match(r".*\)\s*const(\s*=\s*0)?", decl_str)):
decl[2].append("/C")
if "virtual" in decl_str:
print decl_str
print(decl_str)
return decl
def parse_func_decl(self, decl_str):
@ -412,12 +413,12 @@ class CppHeaderParser(object):
if decl_str.startswith("CVAPI"):
rtype_end = decl_str.find(")", args_begin+1)
if rtype_end < 0:
print "Error at %d. no terminating ) in CVAPI() macro: %s" % (self.lineno, decl_str)
print("Error at %d. no terminating ) in CVAPI() macro: %s" % (self.lineno, decl_str))
sys.exit(-1)
decl_str = decl_str[args_begin+1:rtype_end] + " " + decl_str[rtype_end+1:]
args_begin = decl_str.find("(")
if args_begin < 0:
print "Error at %d: no args in '%s'" % (self.lineno, decl_str)
print("Error at %d: no args in '%s'" % (self.lineno, decl_str))
sys.exit(-1)
decl_start = decl_str[:args_begin].strip()
@ -425,7 +426,7 @@ class CppHeaderParser(object):
if decl_start.endswith("operator"):
args_begin = decl_str.find("(", args_begin+1)
if args_begin < 0:
print "Error at %d: no args in '%s'" % (self.lineno, decl_str)
print("Error at %d: no args in '%s'" % (self.lineno, decl_str))
sys.exit(-1)
decl_start = decl_str[:args_begin].strip()
# TODO: normalize all type of operators
@ -455,7 +456,7 @@ class CppHeaderParser(object):
return [] # exotic - dynamic 2d array
else:
#print rettype, funcname, modlist, argno
print "Error at %s:%d the function/method name is missing: '%s'" % (self.hname, self.lineno, decl_start)
print("Error at %s:%d the function/method name is missing: '%s'" % (self.hname, self.lineno, decl_start))
sys.exit(-1)
if self.wrap_mode and (("::" in funcname) or funcname.startswith("~")):
@ -486,9 +487,9 @@ class CppHeaderParser(object):
npos += 1
t, npos = self.find_next_token(decl_str, ["(", ")", ",", "<", ">"], npos)
if not t:
print "Error: no closing ')' at %d" % (self.lineno,)
print decl_str
print decl_str[arg_start:]
print("Error: no closing ')' at %d" % (self.lineno,))
print(decl_str)
print(decl_str[arg_start:])
sys.exit(-1)
if t == "<":
angle_balance += 1
@ -583,7 +584,7 @@ class CppHeaderParser(object):
if block_type in ["file", "enum"]:
continue
if block_type not in ["struct", "class", "namespace"]:
print "Error at %d: there are non-valid entries in the current block stack " % (self.lineno, self.block_stack)
print("Error at %d: there are non-valid entries in the current block stack " % (self.lineno, self.block_stack))
sys.exit(-1)
if block_name:
n += block_name + "."
@ -605,7 +606,7 @@ class CppHeaderParser(object):
stmt_type = "block"
if context == "block":
print "Error at %d: should not call parse_stmt inside blocks" % (self.lineno,)
print("Error at %d: should not call parse_stmt inside blocks" % (self.lineno,))
sys.exit(-1)
if context == "class" or context == "struct":
@ -632,7 +633,7 @@ class CppHeaderParser(object):
try:
classname, bases, modlist = self.parse_class_decl(stmt[len("typedef "):])
except:
print "Error at %s:%d" % (self.hname, self.lineno)
print("Error at %s:%d" % (self.hname, self.lineno))
exit(1)
if classname.startswith("_Ipl"):
classname = classname[1:]
@ -647,7 +648,7 @@ class CppHeaderParser(object):
try:
classname, bases, modlist = self.parse_class_decl(stmt)
except:
print "Error at %s:%d" % (self.hname, self.lineno)
print("Error at %s:%d" % (self.hname, self.lineno))
exit(1)
decl = []
if ("CV_EXPORTS_W" in stmt) or ("CV_EXPORTS_AS" in stmt) or (not self.wrap_mode):# and ("CV_EXPORTS" in stmt)):
@ -767,7 +768,7 @@ class CppHeaderParser(object):
state = SCAN
if state != SCAN:
print "Error at %d: invlid state = %d" % (self.lineno, state)
print("Error at %d: invlid state = %d" % (self.lineno, state))
sys.exit(-1)
while 1:
@ -795,7 +796,7 @@ class CppHeaderParser(object):
while 1:
t2, pos2 = self.find_next_token(l, ["\\", "\""], pos2)
if t2 == "":
print "Error at %d: no terminating '\"'" % (self.lineno,)
print("Error at %d: no terminating '\"'" % (self.lineno,))
sys.exit(-1)
if t2 == "\"":
break
@ -836,7 +837,7 @@ class CppHeaderParser(object):
if token == "}":
if not self.block_stack:
print "Error at %d: the block stack is empty" % (self.lineno,)
print("Error at %d: the block stack is empty" % (self.lineno,))
self.block_stack[-1:] = []
if pos+1 < len(l) and l[pos+1] == ';':
pos += 1
@ -851,13 +852,13 @@ class CppHeaderParser(object):
Prints the list of declarations, retrieived by the parse() method
"""
for d in decls:
print d[0], d[1], ";".join(d[2])
print(d[0], d[1], ";".join(d[2]))
for a in d[3]:
print " ", a[0], a[1], a[2],
print(" ", a[0], a[1], a[2], end="")
if a[3]:
print "; ".join(a[3])
print("; ".join(a[3]))
else:
print
print()
if __name__ == '__main__':
parser = CppHeaderParser()
@ -867,4 +868,4 @@ if __name__ == '__main__':
#for hname in sys.argv[1:]:
#decls += parser.parse(hname, wmode=False)
parser.print_decls(decls)
print len(decls)
print(len(decls))

@ -0,0 +1,64 @@
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
// Defines for Python 2/3 compatibility.
#ifndef __PYCOMPAT_HPP__
#define __PYCOMPAT_HPP__
#if PY_MAJOR_VERSION >= 3
// Python3 treats all ints as longs, PyInt_X functions have been removed.
#define PyInt_Check PyLong_Check
#define PyInt_CheckExact PyLong_CheckExact
#define PyInt_AsLong PyLong_AsLong
#define PyInt_AS_LONG PyLong_AS_LONG
#define PyInt_FromLong PyLong_FromLong
#define PyNumber_Int PyNumber_Long
// Python3 strings are unicode, these defines mimic the Python2 functionality.
#define PyString_Check PyUnicode_Check
#define PyString_FromString PyUnicode_FromString
#define PyString_AsString PyUnicode_AsUTF8
#define PyString_FromStringAndSize PyUnicode_FromStringAndSize
#define PyString_Size PyUnicode_GET_SIZE
#endif
#endif // END HEADER GUARD
Loading…
Cancel
Save