简体   繁体   English

从Cython中暴露出类似文件的对象

[英]Exposing a file-like object from Cython

I need to expose a file-like object from a C library that i'm wrapping with a Cython module. 我需要从C库中公开一个类似文件的对象,我用Cython模块包装它。 I want to reuse python's generic io code for stuff like buffering, readline(), etc. 我想重用python的通用io代码,比如缓冲,readline()等。

The new IO module seems to be just what i need, but actually using it from Cython seems to be non-trivial, I've tried several aproaches: 新的IO模块似乎正是我需要的,但实际上从Cython使用它似乎是非平凡的,我已经尝试了几个aproaches:

  • My code in a cdef class that inherits from IO.RawIOBase - This fails because cdef classes can inherit only from other cython cdef classes, while IO is "raw" C. 我的代码在一个继承自IO.RawIOBase的cdef类中 - 这失败了,因为cdef类只能从其他cython cdef类继承,而IO是“raw”C。

  • My code in a cdef class, another (non-cdef) class that inherits both my cdef class and RawIOBase - Fails with "TypeError: multiple bases have instance lay-out conflict" 我的代码在cdef类中,另一个(非cdef)类继承了我的cdef类和RawIOBase - 失败了“TypeError:多个base有实例布局冲突”

  • My code in a (non-cdef) class that inherits from RawIOBase - This works, but i loose the ability to store my c-level (that i need to talk to the underlying library) stuff inside the class, so i need a make a cdef wrapper around it and store that as a member... this looks like a mess. 我的代码在一个(非cdef)类中继承自RawIOBase - 这是有效的,但我放弃了在类中存储我的c级(我需要与底层库交谈)的能力,所以我需要一个make围绕它的cdef包装并将其存储为成员...这看起来像一团糟。

  • My code in cdef class that doesn't inherit (Raw)IOBase rather reimplements it's functionality, Python code gets my object wrapped in BufferedReader/BufferedWriter - This one seems to work and less messy than the previous option. 我的cdef类中的代码不继承(Raw)IOBase而是重新实现它的功能,Python代码将我的对象包装在BufferedReader / BufferedWriter中 - 这个似乎比以前的选项更有效。

My questions(s): 我的问题:

1) Am I missing something and reinventing the wheel here? 1)我错过了什么并在这里重新发明轮子?

2) What is the exact stuff from IOBase that I need to implement to keep BufferedReader/Writer happy with my object in current and future versions of python? 2)我需要实现的IOBase的确切内容是什么,以保持BufferedReader / Writer在我目前和未来的python版本中对我的对象满意? Is this documented anywhere? 这记录在哪里?

3) How will that work in python 2.6 where IO is pure python? 3)在Python是纯Python的python 2.6中如何工作? I guess that performance will suffer but it will work, right? 我想性能会受到影响,但它会起作用,对吗?

Would it be too inefficient to call os.fdopen() on the file descriptor number returned by the underlying library, and then to dispatch normal Python method calls to the resulting file object in order to do your input and output? 在底层库返回的文件描述符号上调用os.fdopen() ,然后将正常的Python方法调用分派给生成的文件对象以进行输入和输出是否效率太低? With most I/O, I would be surprised if you could see a difference with whether you called a C routine directly or let the Python method dispatch logic call it for you — but, of course, you might be in an unusual situation and I could be wrong! 对于大多数I / O,如果您看到是否直接调用C例程或让Python方法调度逻辑为您调用它,我会感到惊讶 - 但是,当然,您可能处于异常情况而我可能是错的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM