def outputTypeChecking(self, methodClass, args, file, nesting): """ Output an assert statement to check the type of each arg in this method This can be turned off with a command line parameter in generatePythonCode It is valid to pass in None for methodClass if you are not in any methodClass """ if FFIConstants.wantTypeChecking: for i in range(len(args)): methodArgSpec = args[i] typeDesc = methodArgSpec.typeDescriptor.recursiveTypeDescriptor() typeName = FFIOverload.getTypeName(methodClass, typeDesc) # We only do type checking on class types. C++ can do # type checking on the primitive types, and will do a # better job anyway. if typeDesc.__class__ == FFITypes.ClassTypeDescriptor: # Get the real return type (not derived) if ( (not typeDesc.isNested) and # Do not put our own module in the import list (methodClass != typeDesc) ): indent(file, nesting, "import " + typeDesc.foreignTypeName + "\n") indent(file, nesting, "if not isinstance(" + methodArgSpec.name + ", " + typeName + "):\n") indent( file, nesting + 1, 'raise TypeError, "Invalid argument %s, expected <%s>"\n' % (i, typeDesc.foreignTypeName), )
def generateReturnValueWrapper(self, classTypeDesc, file, userManagesMemory, needsDowncast, nesting): """ Generate code that creates a shadow object of this type then sets the this pointer and returns the object. We call the class destructor with None as the only parameter to get an empty shadow object. """ if classTypeDesc != self: indent(file, nesting, 'import ' + self.foreignTypeName + '\n') indent(file, nesting, 'returnObject = ') # Do not put Class.Class if this file is the file that defines Class # Also check for nested classes. They do not need the module name either typeName = FFIOverload.getTypeName(classTypeDesc, self) file.write(typeName) file.write('(None)\n') indent(file, nesting, 'returnObject.this = returnValue\n') # Zero this pointers get returned as the Python None object indent(file, nesting, 'if (returnObject.this == 0): return None\n') if userManagesMemory: indent(file, nesting, 'returnObject.userManagesMemory = 1\n') if needsDowncast: if (FFIOverload.inheritsFrom(self, TypedObjectDescriptor) or self == TypedObjectDescriptor): indent(file, nesting, 'return returnObject.setPointer()\n') else: indent(file, nesting, 'return returnObject\n') else: indent(file, nesting, 'return returnObject\n')
def outputTypeChecking(self, methodClass, args, file, nesting): """ Output an assert statement to check the type of each arg in this method This can be turned off with a command line parameter in generatePythonCode It is valid to pass in None for methodClass if you are not in any methodClass """ if FFIConstants.wantTypeChecking: for i in range(len(args)): methodArgSpec = args[i] typeDesc = methodArgSpec.typeDescriptor.recursiveTypeDescriptor( ) typeName = FFIOverload.getTypeName(methodClass, typeDesc) # We only do type checking on class types. C++ can do # type checking on the primitive types, and will do a # better job anyway. if typeDesc.__class__ == FFITypes.ClassTypeDescriptor: # Get the real return type (not derived) if ((not typeDesc.isNested) and # Do not put our own module in the import list (methodClass != typeDesc)): indent(file, nesting, 'import ' + typeDesc.foreignTypeName + '\n') indent( file, nesting, 'if not isinstance(' + methodArgSpec.name + ', ' + typeName + '):\n') indent( file, nesting + 1, 'raise TypeError, "Invalid argument %s, expected <%s>"\n' % (i, typeDesc.foreignTypeName))