Closed
Description
We defined str
in typeshed.
typeshed/stdlib/2and3/builtins.pyi
Lines 1522 to 1527 in f76b7b2
How to reproduce it
➜ cat t.py
import ast
text = input()
try:
ast.parse(text)
except SyntaxError as e:
print(e.text)
➜ echo 'max(1 for i in range(10), key=lambda x: x+1)' | python t.py
None
Analysis
Check from Python 3.7 source code.
https://github.com/python/cpython/blob/c6b31061997526b31961ec34328408ca421f51fc/Python/ast.c#L681-L685
loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));
if (!loc) {
Py_INCREF(Py_None);
loc = Py_None;
}
PyObject *
PyErr_ProgramTextObject(PyObject *filename, int lineno)
{
FILE *fp;
if (filename == NULL || lineno <= 0)
return NULL;
fp = _Py_fopen_obj(filename, "r" PY_STDIOTEXTMODE);
if (fp == NULL) {
PyErr_Clear();
return NULL;
}
return err_programtext(fp, lineno);
}
let us make a special file to confirm
➜ echo 'for test' > \<unknown\>
➜ echo 'max(1 for i in range(10), key=lambda x: x+1)' | python t.py
for test