cfile.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:cwrap 作者: Statoil 项目源码 文件源码
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: ecl.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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号