Cython:打开 io.BytesIO 对象作为文件

问题描述 投票:0回答:0

我有一个 Cython 函数,它对我想通过 python 输入的字节数据执行一些操作

with open() as f:
块:

Cython 函数:

from libc.stdio import fdopen

def access(fileobj)
    infile = fdopen(fileobj.fileno(), b"rb")
    do_something(infile)

函数调用:

with open('file.bin', 'rb') as f:
    access(f)

这适用于磁盘上的文件,但不适用于内存中的字节数据,因为对其调用

fileno
方法将导致
UnsupportedOperation
异常:

import io
import os

byte_data = os.urandom(10)
with io.BytesIO(byte_data) as f:
    access(f)

不幸的是,

do_something
函数需要接受一个
FILE
对象并且无法更改。有什么方法可以修改
access
函数,以便它可以在给定由
FILE
创建的类文件对象时生成有效的
io.BytesIO
对象?

python io cython stdio bytesio
© www.soinside.com 2019 - 2024. All rights reserved.