我们正在将基于 C++ openssl 的项目转换为带有 M2Crypto 的 python,并且我们遇到了一个与 M2Crypto 的 BIO 例程有些不寻常的问题。具体来说,对 BIO.readlines() 的任何调用都会永远挂在文件对象上。
这是我们尝试的快速示例:
f = open('test.txt','w')
f.write('hello world\n')
f.close()
import M2Crypto.BIO
bio = M2Crypto.BIO.openfile('test.txt','r')
lines = bio.readlines()
# the above call hangs forever
为了确保我们的 OpenSSL 安装没有严重错误,我们创建了一个小型测试程序来读取我们刚刚创建的 test.txt 文件
#include <openssl/bio.h>
#include <openssl/err.h>
int main() {
const int maxrd = 4096;
char line[maxrd];
int rd;
BIO* bio = BIO_new_file("test.txt","r");
while((rd = BIO_gets(bio, line, maxrd)) > 0) {
printf("%s",line);
}
if (rd == -1) {
printf("BIO error %ld\n", ERR_get_error());
}
}
没问题。
我们一直在研究 M2Crypto-0.21.1/SWIG/_bio.i 包装文件,并认为我们可能对问题的根源有所了解。第 109 行测试 BIO_gets() 的返回值
if (r < 0) {
// return Py_None
}
但是, BIO_gets() 的手册页表明它可以返回 0 或 -1 以指示流结束。
我相信应该是
if (r < 1) {
// return Py_None
}
但是想看看其他人是否遇到过——或者我们对 BIO_gets() 系统的理解是否有误。
--- 详细信息 --- Pythong 2.7 M2Crypto 0.21.1 OpenSSL 0.9.8q-fips 2010 年 12 月 2 日 FreeBSD 8.2-RELEASE-p4