Skip to content

SyntaxError.text could be None #3118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Hanaasagi opened this issue Jul 14, 2019 · 1 comment · Fixed by #3119
Closed

SyntaxError.text could be None #3118

Hanaasagi opened this issue Jul 14, 2019 · 1 comment · Fixed by #3119
Labels
stubs: false positive Type checkers report false errors

Comments

@Hanaasagi
Copy link
Contributor

We defined str in typeshed.

class SyntaxError(_StandardError):
msg: str
lineno: int
offset: Optional[int]
text: str
filename: str

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;
    }

https://github.com/python/cpython/blob/c6b31061997526b31961ec34328408ca421f51fc/Python/errors.c#L1203-L1215

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
@srittau
Copy link
Collaborator

srittau commented Jul 14, 2019

Thank you for the analysis, patches welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stubs: false positive Type checkers report false errors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants