放弃了 😞 ,最后补充一些东西
极简 demo
python -u -c "print('a' * 2 * 10000)"
发现输出的 a 永远是 10000 个,原因?不知道 😑
标准输出是真的有截断
print 的源码没找着,不过倒是发现 Python-3.7.4/Python/sysmodule.c
的 2705 行 (或者看 PySys_WriteStdout ) 有这样的说明
/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
Adapted from code submitted by Just van Rossum.
PySys_WriteStdout(format, ...)
PySys_WriteStderr(format, ...)
The first function writes to sys.stdout; the second to sys.stderr. When
there is a problem, they write to the real (C level) stdout or stderr;
no exceptions are raised.
PyErr_CheckSignals() is not called to avoid the execution of the Python
signal handlers: they may raise a new exception whereas sys_write()
ignores all exceptions.
Both take a printf-style format string as their first argument followed
by a variable length argument list determined by the format string.
*** WARNING ***
The format should limit the total size of the formatted output string to
1000 bytes. In particular, this means that no unrestricted "%s" formats
should occur; these should be limited using "%.<N>s where <N> is a
decimal number calculated so that <N> plus the maximum size of other
formatted text does not exceed 1000 bytes. Also watch out for "%f",
which can print hundreds of digits for very large numbers.
*/
static void
sys_write(_Py_Identifier *key, FILE *fp, const char *format, va_list va)
{
PyObject *file;
PyObject *error_type, *error_value, *error_traceback;
char buffer[1001];
int written;
PyErr_Fetch(&error_type, &error_value, &error_traceback);
file = _PySys_GetObjectId(key);
written = PyOS_vsnprintf(buffer, sizeof(buffer), format, va);
if (sys_pyfile_write(buffer, file) != 0) {
PyErr_Clear();
fputs(buffer, fp);
}
if (written < 0 || (size_t)written >= sizeof(buffer)) {
const char *truncated = "... truncated";
if (sys_pyfile_write(truncated, file) != 0)
fputs(truncated, fp);
}
PyErr_Restore(error_type, error_value, error_traceback);
}
void
PySys_WriteStdout(const char *format, ...)
{
va_list va;
va_start(va, format);
sys_write(&PyId_stdout, stdout, format, va);
va_end(va);
}
有提到 1000 bytes 的限制,多的就 truncate 掉,(虽然据我测试,没发现怎样的情况下会被 truncate
总之我是找不下去了,有兴趣的老哥可以深入再看看