1
import os
s = os.sys.stdin.buffer.read(1024*32)

失败了

D:\Projects\pytools>python t1.py
Traceback (most recent call last):
  File "t1.py", line 2, in <module>
    s = os.sys.stdin.buffer.read(1024*32)
OSError: [Errno 12] Not enough space

buf 如果给定 buflen = 1024*32-1 则正确

import os
s = os.sys.stdin.buffer.read(1024*32-1)

如果您运行 python t1.py,那么进程会阻塞并按预期等待输入。为什么 python3.3 有 1024*32-1 的缓冲区长度限制?是系统不同,还是所有系统都一样?我们怎样才能消除这个限制?

顺便说一句:我使用 Windows 7 python 32 位版本 3.3

4

1 回答 1

0

我们首先在这里查看os模块的源代码,其中第 26 行显示 这告诉我们这只是对标准模块的引用。 然后我们转到模块的源代码,在第 1593 行我们找到以下注释(谢天谢地有人把它放在那里......): 然后 我们转到文件,我们在第 1086 行遇到以下代码:在第 1091 行: 然后我们在第 910 行查找函数的定义。我们在第 999 行查找该函数的返回值,如下所示: 现在我们必须找出它是什么。这是函数的返回值
import sys, errno
os.syssys
sys
/* stdin/stdout/stderr are now set by pythonrun.c */
pythonrun.c
std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);

PySys_SetObject("stdin", std);
create_stdio()
return stream;
stream_PyObject_CallMethodId()在第 984 行调用。

我希望你能看到流程——试着从这里开始。

于 2013-04-24T21:07:03.887 回答