torch/csrc/serialization.cpp
// Call Python fildes.read(nbytes) and copy it to buf.
Call Python fildes.read(nbytes) and copy it to buf.
Call Python fildes . read ( nbytes ) and copy it to buf .
static inline ssize_t doPythonReadBuffered(PyObject* fildes, void* buf, size_t nbytes) {
  const size_t buffer_size = 262144;  
  size_t read_bytes = 0;
  while (read_bytes < nbytes) {
    auto remaining = nbytes - read_bytes;
    auto to_read = remaining > buffer_size ? buffer_size : remaining;
    THPObjectPtr r(PyObject_CallMethod(fildes, "read", "i", to_read));
    if (!r) throw python_error();
#if PY_MAJOR_VERSION >= 3
    auto size = PyBytes_GET_SIZE(r.get());
    const void* bytes = PyBytes_AsString(r.get());
#else
    auto size = PyString_GET_SIZE(r.get());
    const void* bytes = PyString_AsString(r.get());
#endif
    if (size == 0) {
      return read_bytes;
    }
    memcpy(reinterpret_cast<char*>(buf) + read_bytes, bytes, size);
    read_bytes += size;
  } 
  return read_bytes;
}
static inline ssize_t doPythonReadBuffered ( PyObject * fildes , void * buf , size_t nbytes ) { const size_t buffer_size = 262144 ; size_t read_bytes = 0 ; while ( read_bytes < nbytes ) { auto remaining = nbytes - read_bytes ; auto to_read = remaining > buffer_size ? buffer_size : remaining ; THPObjectPtr r ( PyObject_CallMethod ( fildes , " " , " " , to_read ) ) ; if ( ! r ) throw python_error ( ) ; #if PY_MAJOR_VERSION >= 3 auto size = PyBytes_GET_SIZE ( r . get ( ) ) ; const void * bytes = PyBytes_AsString ( r . get ( ) ) ; #else auto size = PyString_GET_SIZE ( r . get ( ) ) ; const void * bytes = PyString_AsString ( r . get ( ) ) ; #endif if ( size == 0 ) { return read_bytes ; } memcpy ( reinterpret_cast < char * > ( buf ) + read_bytes , bytes , size ) ; read_bytes += size ; } return read_bytes ; }
Call Python fildes.read(nbytes) and copy it to buf.
Call Python fildes . read ( nbytes ) and copy it to buf .
// 2^18 // read output is String (Python 2) / Bytes (Python 3) // we read EOF // Reading loop