Вопрос

I am looking at the source code for Python-2.7.6 to figure this out.

In Objects/fileobject.c, line 1052, there is a function called file_read, which I am guessing corresponds to the function read on the file object in Python.

On line 1067 of the same file, I see the following line. Note that bytesrequested is used further down in the function to decide the size of the buffer to allocate for storing the file.

 if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested))

It looks like the number of bytes requested is somehow encoded inside args, which has type PyObject*. Thus, the natural place to continue the search is to find out where file_read is called.

However, I cannot find any place in the entire Python source tree (recursive grep) where file_read is actually called, so I cannot continue the trace to find out how bytesrequested is actually computed. Is file_read somehow called under a different name in a different part of the source tree?

Это было полезно?

Решение

Look at line 2114

file_read is python's file.read method. The args in the C code are the arguments that you pass to file.read. bytesrequested is whatever you pass as the argument to file.read

with open('file') as fin:
    fin.read(5)  # bytesrequested will be 5.
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top