def test_function_type(self): def stringObj(c_ptr): char_ptr = ctypes.c_char_p(c_ptr) python_string = char_ptr.value TestUtilPrototype.lib.free(c_ptr) return python_string Prototype.registerType("string_obj", stringObj) dateStamp = TestUtilPrototype("string_obj util_alloc_date_stamp()") date_stamp = dateStamp() self.assertIsInstance(date_stamp, str)
class CFILE(BaseCClass): """ Utility class to map a Python file handle <-> FILE* in C """ TYPE_NAME = "FILE" _as_file = Prototype(ctypes.pythonapi, "void* PyFile_AsFile(py_object)") def __init__(self, py_file): """ Takes a python file handle and looks up the underlying FILE * The purpose of the CFILE class is to be able to use python file handles when calling C functions which expect a FILE pointer. A CFILE instance should be created based on the Python file handle, and that should be passed to the function expecting a FILE pointer. The implementation is based on the ctypes object pythonapi which is ctypes wrapping of the CPython api. C-function: void fprintf_hello(FILE * stream , const char * msg); Python wrapper: lib = clib.load( "lib.so" ) fprintf_hello = Prototype(lib, "void fprintf_hello( FILE , char* )") Python use: py_fileH = open("file.txt" , "w") fprintf_hello( CFILE( py_fileH ) , "Message ...") py_fileH.close() If the supplied argument is not of type py_file the function will raise a TypeException. Examples: ert.ecl.ecl_kw.EclKW.fprintf_grdecl() """ c_ptr = self._as_file(py_file) try: super(CFILE, self).__init__(c_ptr) except ValueError as e: raise TypeError( "Sorry - the supplied argument is not a valid Python file handle!" ) self.py_file = py_file def __del__(self): pass
def test_already_registered(self): with self.assertRaises(PrototypeError): Prototype.registerType("test_stringlist", None)