Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

python - How to retrieve filename and lineno attribute of SyntaxError

I have embedded python interpreter in my application. I use it to run python scripts using PyRun_String() API. I can fetch error information using PyErr_Fetch() if an error/exception encounters. This gives me exception type, exception value and exception traceback. I then find line number of the originated error from traceback. However, i dont get any kind of traceback in case of Syntax Error. Do anybody know how to retrieve line number without traceback..? Is there any way to retreive it using exception type or exception value..?

Python documentation stated that:

Instances of this class have attributes filename, lineno, offset and text for easier access to the details. str() of the exception instance returns only the message.

How do I retrieve filename and lineno attribute of SyntaxError in embedded python..?

Any help will be appreciated. Thanks in advance.


Thanks for your suggestion Brett. But I already tried it by using PyObject_GetAttr().

Please see below sample code that I used for testing purpose.

int main(int argc, char** argv)
{
    Py_Initialize();

    // Get a reference to the main module.
    PyObject* main_module =
        PyImport_AddModule("__main__");

    // Get the main module's dictionary
    // and make a copy of it.
    PyObject* main_dict =
        PyModule_GetDict(main_module);

    const char *script_source = "def main():
print('Hello'

main()";

    PyObject *res = PyRun_String(script_source,Py_file_input,main_dict,main_dict);
    if(res == NULL)
    {
        PyObject *ptype = NULL, *pvalue = NULL, *ptraceback = NULL;
        PyErr_Fetch(&ptype,&pvalue,&ptraceback);

        PyObject* py_filename = PyUnicode_FromString("filename");
        PyObject* file_name = PyObject_GetAttr(ptype,py_filename);

        PyObject* py_lineno = PyUnicode_FromString("lineno");
        PyObject* line_no = PyObject_GetAttr(ptype,py_lineno);
    }
}

I have intentionally inserted a syntax error in script_source.

I am unable to retrieve actual file name and line number from file_name and line_no objects.

I tried to debug file_name and line_no objects and the content's looks like below. "member filename of Syntax Error Objects" and "member lineno of Syntax Error Objects"

These objects are not callable so I can't use PyObject_Call() variant's over these.

Is there any way so that I can get actual file name and line number from these object's..?

In this file name will be as I am using PyRun_String().

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I resolved it in following way. I used PyErr_NormalizeException() after PyErr_Fetch() and code is working fine now.

int main(int argc, char** argv)
{
    Py_Initialize();

    // Get a reference to the main module.
    PyObject* main_module =
        PyImport_AddModule("__main__");

    // Get the main module's dictionary
    // and make a copy of it.
    PyObject* main_dict =
        PyModule_GetDict(main_module);

    const char *script_source = "def main():
print('Hello'

main()";

    PyObject *res = PyRun_String(script_source,Py_file_input,main_dict,main_dict);
    if(res == NULL)
    {
        PyObject *ptype = NULL, *pvalue = NULL, *ptraceback = NULL;
        PyErr_Fetch(&ptype,&pvalue,&ptraceback);
        PyErr_NormalizeException(&ptype,&pvalue,&ptraceback);

        char *msg;
        char *file;
        int line;
        int offset;
        char *text;

        int res = PyArg_ParseTuple(pvalue,"s(siis)",&msg,&file,&line,&offset,&text);

        PyObject* file_name = PyObject_GetAttrString(pvalue,"filename");
        PyObject* file_name_str = PyObject_Str(file_name);
        PyObject* file_name_unicode = PyUnicode_AsEncodedString(file_name_str,"utf-8", "Error");
        char *actual_file_name = PyBytes_AsString(file_name_unicode);

        PyObject* line_no = PyObject_GetAttrString(pvalue,"lineno");
        PyObject* line_no_str = PyObject_Str(line_no);
        PyObject* line_no_unicode = PyUnicode_AsEncodedString(line_no_str,"utf-8", "Error");
        char *actual_line_no = PyBytes_AsString(line_no_unicode);

        printf("done");
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...