简体   繁体   中英

python os.sys.stdin.buffer.read failed if given buffer length

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

failed with

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 if given buflen = 1024*32-1 then it goes right

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

if you run python t1.py, then the process blocked and waiting for input as expect. Why python3.3 have 1024*32-1 buffer length limitation? Is it system different, or just a the same for all systems? How can we remove this limitation?

BTW: i using windows 7 python 32 bit version 3.3

We start by looking at the source of os module here , where line 26 reads
import sys, errno
This tells us that os.sys is just a reference to the standard sys module.
Then we head over to the source of the sys module, where in line 1593 we find the following comment (thankfully someone put it there...):
/* stdin/stdout/stderr are now set by pythonrun.c */
Then we go to the pythonrun.c file , where we meet the following code in line 1086:
std = create_stdio(iomod, fd, 0, "<stdin>", encoding, errors);
and this on line 1091:
PySys_SetObject("stdin", std);
Then we look for definition of create_stdio() function which we find in line 910. We look for the return value of this function which is on line 999 and looks like this:
return stream;
Now we have to find out what the stream is. It's the return value of function _PyObject_CallMethodId() called in line 984.

I hope you see the flow - try to follow from here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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