2013-04-23 86 views
1
import os 
s = os.sys.stdin.buffer.read(1024*32) 

失败,蟒蛇os.sys.stdin.buffer.read失败如果给缓冲区长度

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的缓冲区长度限制?它是系统不同的,还是所有系统都一样?我们如何消除这个限制?

BTW:我使用Windows 7蟒蛇32位版本3.3

+0

直到我将它增加到1024 * 3 * 6时,才在Linux 64bit上看到任何错误,然后出现'MemoryError'。 – 2013-04-24 16:56:19

+0

我的Win7 + Python3.3.0-32bit也出现同样的问题。但是如果我在Cygwin Python 2.7.3中运行脚本,它运行得很完美。升级到3.3.1没有帮助。 – Alexey 2013-04-24 17:01:19

+0

我试过32位Python 3.3.0,3.3.1,2.7.4和64位3.31,但都有同样的问题。这似乎是Windows端口特定的问题。你可以尝试在Python邮件列表上提出这个问题。 – Alexey 2013-04-24 17:21:32

回答

0

我们首先看os模块here,其中26行读取
import sys, errno
这告诉我们的来源,os.sys仅仅是一个参考标准sys模块。
然后我们头向sys模块,其中线1593,我们发现下面评论的source(谢天谢地有人把它放在那里...):
/* stdin/stdout/stderr are now set by pythonrun.c */
然后我们去pythonrun.cfile,我们见面在1086线以下代码:
std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
,这对行1091:
PySys_SetObject("stdin", std);
然后我们找create_stdio()功能的定义,我们在线路910我们期待这个函数的返回值发现这是对999行,看起来像这样:
return stream;
现在我们必须找出stream是什么。它是函数_PyObject_CallMethodId()在984行中调用的返回值。

我希望你看到流 - 试着从这里开始。