[pygccxml-commit] SF.net SVN: pygccxml: [629] pyplusplus_dev
Brought to you by:
mbaas,
roman_yakovenko
From: <rom...@us...> - 2006-10-09 06:46:41
|
Revision: 629 http://svn.sourceforge.net/pygccxml/?rev=629&view=rev Author: roman_yakovenko Date: 2006-10-08 23:46:30 -0700 (Sun, 08 Oct 2006) Log Message: ----------- switching to convenient API Modified Paths: -------------- pyplusplus_dev/pyplusplus/code_creators/calldef_transformed.py pyplusplus_dev/pyplusplus/function_transformers/arg_policies.py pyplusplus_dev/unittests/function_transformations_tester.py Modified: pyplusplus_dev/pyplusplus/code_creators/calldef_transformed.py =================================================================== --- pyplusplus_dev/pyplusplus/code_creators/calldef_transformed.py 2006-10-08 11:30:57 UTC (rev 628) +++ pyplusplus_dev/pyplusplus/code_creators/calldef_transformed.py 2006-10-09 06:46:30 UTC (rev 629) @@ -17,7 +17,7 @@ class mem_fun_transformed_t( calldef_t ): """Creates code for public non-virtual member functions. """ - + def __init__( self, function, wrapper=None ): calldef_t.__init__( self, function=function, wrapper=wrapper ) @@ -34,7 +34,7 @@ full_name = self.wrapper.full_name() else: full_name = declarations.full_name( self.declaration ) - + if use_function_alias: return '%s( &%s )' \ % ( self.function_type_alias, full_name ) @@ -93,7 +93,7 @@ """Return the name of the wrapper function. This is just the local name without any scope information. - + """ # A list with the individual components of the name components = ["_py"] @@ -103,7 +103,7 @@ components.append(self.declaration.parent.name) components.append(self.declaration.alias) return "_".join(components) - + def full_name(self): """Return the full name of the wrapper function. @@ -143,11 +143,11 @@ $DECLARATIONS $PRE_CALL - + $RESULT_VAR_ASSIGNMENT$CALL_FUNC_NAME($INPUT_PARAMS); - + $POST_CALL - + $RETURN_STMT """ @@ -170,7 +170,7 @@ return os.linesep.join( answer ) def _create_impl(self): - + answer = self.create_function() # Replace the argument list of the declaration so that in the @@ -186,7 +186,7 @@ class mem_fun_v_transformed_t( calldef_t ): """Creates code for (public) virtual member functions. """ - + def __init__( self, function, wrapper=None ): calldef_t.__init__( self, function=function, wrapper=wrapper ) self.default_function_type_alias = 'default_' + self.function_type_alias @@ -340,7 +340,7 @@ def create_virtual_body(self): thread_safe = getattr(self.declaration, "thread_safe", False) - + if thread_safe: body = """ pyplusplus::threading::gil_guard_t %(gstate_var)s; @@ -356,14 +356,14 @@ $DECLARATIONS - try { + try { $PRE_CALL - + ${RESULT_VAR_ASSIGNMENT}boost::python::call<$RESULT_TYPE>($INPUT_PARAMS); - - $POST_CALL - $RETURN_STMT + $POST_CALL + + $RETURN_STMT } catch(...) { @@ -371,9 +371,9 @@ { PyErr_Print(); } - + $CLEANUP - + $EXCEPTION_HANDLER_EXIT } } @@ -390,7 +390,7 @@ if( %(override_var)s ) { $DECLARATIONS - + $PRE_CALL ${RESULT_VAR_ASSIGNMENT}boost::python::call<$RESULT_TYPE>($INPUT_PARAMS); @@ -436,7 +436,7 @@ cls_wrapper = self._subst_manager.wrapper_func.declare_local("cls_wrapper", cls_wrapper_type); # The name of the 'self' variable (i.e. first argument) selfname = self._subst_manager.wrapper_func.arg_list[0].name - + body = """$DECLARATIONS $PRE_CALL @@ -468,7 +468,7 @@ "cls_wrapper" : cls_wrapper, "self" : selfname, "base_name" : self.base_name() } - + # function_call = declarations.call_invocation.join( self.declaration.name # , [ self.function_call_args() ] ) # body = self.wrapped_class_identifier() + '::' + function_call + ';' @@ -493,7 +493,7 @@ header = 'static $RET_TYPE %s( $ARG_LIST_DEF ) {'%self.default_name() header = self._subst_manager.subst_wrapper(header) - + answer = [ header ] answer.append( self.indent( self.create_default_body() ) ) answer.append( '}' ) @@ -501,13 +501,13 @@ def get_required_headers(self): """Return a list of required header file names.""" - res = [code_repository.gil_guard.file_name] + res = [code_repository.gil_guard.file_name, code_repository.convenience.file_name ] res += self._subst_manager.virtual_func.get_required_headers() res += self._subst_manager.wrapper_func.get_required_headers() return res def _create_impl(self): - + answer = [ self.create_function() ] answer.append( os.linesep ) answer.append( self.create_base_function() ) Modified: pyplusplus_dev/pyplusplus/function_transformers/arg_policies.py =================================================================== --- pyplusplus_dev/pyplusplus/function_transformers/arg_policies.py 2006-10-08 11:30:57 UTC (rev 628) +++ pyplusplus_dev/pyplusplus/function_transformers/arg_policies.py 2006-10-09 06:46:30 UTC (rev 629) @@ -15,7 +15,7 @@ - L{input_array_t} - L{output_array_t} """ - +import os from pygccxml import declarations # output_t @@ -27,7 +27,7 @@ void getValue(int& v) -> v = getValue() """ - + def __init__(self, idx): """Constructor. @@ -64,7 +64,7 @@ else: sm.wrapper_func.input_params[self.idx-1] = self.local_var - + def virtual_post_call(self, sm): """Extract the C++ value after the call to the Python function. """ @@ -84,7 +84,7 @@ void setValue(int& v) -> setValue(v) """ - + def __init__(self, idx): """Constructor. @@ -101,7 +101,7 @@ def init_funcs(self, sm): # Remove the specified input argument from the wrapper function arg = sm.remove_arg(self.idx) - + # Do some checks (the arg has to be a reference or a pointer) reftype = arg.type if not (isinstance(reftype, declarations.reference_t) or @@ -119,7 +119,7 @@ void foo(int& v) -> v = foo(v) """ - + def __init__(self, idx): """Constructor. @@ -137,7 +137,7 @@ def init_funcs(self, sm): # Remove the specified input argument from the wrapper function arg = sm.remove_arg(self.idx) - + # Do some checks (the arg has to be a reference or a pointer) reftype = arg.type if not (isinstance(reftype, declarations.reference_t) or @@ -160,7 +160,7 @@ else: sm.wrapper_func.input_params[self.idx-1] = self.local_var - + def virtual_post_call(self, sm): """Extract the C++ value after the call to the Python function. """ @@ -182,9 +182,9 @@ TODO: Error handling (in the wrapper function)! - + """ - + def __init__(self, idx, size): """Constructor. @@ -205,12 +205,12 @@ def __str__(self): return "InputArray(%d,%d)"%(self.idx, self.size) - + def init_funcs(self, sm): # Remove the original argument... arg = sm.remove_arg(self.idx) - + if not (isinstance(arg.type, declarations.pointer_t) or isinstance(arg.type, declarations.array_t)): raise ValueError, "%s\nArgument %d (%s) must be a pointer."%(sm.decl, self.idx, arg.name) @@ -240,21 +240,18 @@ def wrapper_pre_call(self, sm): """Wrapper function code. """ - res = "" - res += "// Assert that '%s' is really a sequence...\n"%self.argname - res += "if (!PySequence_Check(%s.ptr()))\n"%self.argname - res += "{\n" - res += ' PyErr_SetString(PyExc_ValueError, "Argument %s: sequence expected");\n'%self.argname - res += ' boost::python::throw_error_already_set();\n' - res += "}\n" - res += "// Copy the sequence '%s' into '%s'...\n"%(self.argname, self.carray) - res += 'if (%s.attr("__len__")()!=%d)\n'%(self.argname, self.size) - res += '{\n' - res += ' PyErr_SetString(PyExc_ValueError, "Invalid sequence size (expected %d)");\n'%self.size - res += ' boost::python::throw_error_already_set();\n' - res += '}\n' - res += "for(%s=0; %s<%d; %s++)\n"%(self.wrapper_ivar, self.wrapper_ivar, self.size, self.wrapper_ivar) - res += " %s[%s] = boost::python::extract< %s >(%s[%s]);"%(self.carray, self.wrapper_ivar, self.basetype, self.argname , self.wrapper_ivar) + tmpl = [] + tmpl.append( 'pyplusplus::convenience::ensure_uniform_sequence< %(type)s >( %(argname)s, %(size)d );' ) + tmpl.append( 'for(%(ivar)s=0; %(ivar)s<%(size)d; ++%(ivar)s){' ) + tmpl.append( ' %(array_name)s[ %(ivar)s ] = boost::python::extract< %(type)s >( %(argname)s[%(ivar)s] );' ) + tmpl.append( '}' ) + return os.linesep.join( tmpl ) % { + 'type' : self.basetype + , 'argname' : self.argname + , 'size' : self.size + , 'ivar' : self.wrapper_ivar + , 'array_name' : self.carray + } return res def virtual_pre_call(self, sm): @@ -272,12 +269,12 @@ """Handles an output array of a fixed size. void getVec3(double* v) -> v = getVec3() - # v will be a list with 3 floats + # v will be a list with 3 floats TODO: Error handling (in the virtual function)! - + """ - + def __init__(self, idx, size): """Constructor. @@ -289,7 +286,7 @@ self.idx = idx self.size = size - + self.argname = None self.basetype = None self.pyval = None @@ -302,7 +299,7 @@ def init_funcs(self, sm): # Remove the original argument... arg = sm.remove_arg(self.idx) - + if not (isinstance(arg.type, declarations.pointer_t) or isinstance(arg.type, declarations.array_t)): raise ValueError, "%s\nArgument %d (%s) must be a pointer."%(sm.decl, self.idx, arg.name) @@ -328,7 +325,7 @@ # Declare a variable that will receive the Python list self.virtual_pyval = sm.virtual_func.declare_local("py_"+self.argname, "boost::python::object") - + # Declare an int which is used for the loop self.virtual_ivar = sm.virtual_func.declare_local("i", "int", default=0) Modified: pyplusplus_dev/unittests/function_transformations_tester.py =================================================================== --- pyplusplus_dev/unittests/function_transformations_tester.py 2006-10-08 11:30:57 UTC (rev 628) +++ pyplusplus_dev/unittests/function_transformations_tester.py 2006-10-09 06:46:30 UTC (rev 629) @@ -67,8 +67,8 @@ self.assertEqual(img.fixed_input_array((1,2,3)), 6) self.assertRaises(ValueError, lambda : img.fixed_input_array([1,2,3,4])) self.assertRaises(ValueError, lambda : img.fixed_input_array([1,2])) - self.assertRaises(ValueError, lambda : img.fixed_input_array(1)) - self.assertRaises(ValueError, lambda : img.fixed_input_array(None)) + self.assertRaises(TypeError, lambda : img.fixed_input_array(1)) + self.assertRaises(TypeError, lambda : img.fixed_input_array(None)) # Check the fixed_output_array method self.assertEqual(img.fixed_output_array(), [1,2,3]) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |