Why doesn't PyRun_String evaluate bool literals?
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Quirky Dreamscape Looping
--
Chapters
00:00 Why Doesn'T Pyrun_string Evaluate Bool Literals?
00:30 Accepted Answer Score 5
01:29 Thank you
--
Full question
https://stackoverflow.com/questions/5120...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #boolean #cpython
#avk47
    Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Quirky Dreamscape Looping
--
Chapters
00:00 Why Doesn'T Pyrun_string Evaluate Bool Literals?
00:30 Accepted Answer Score 5
01:29 Thank you
--
Full question
https://stackoverflow.com/questions/5120...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #boolean #cpython
#avk47
ACCEPTED ANSWER
Score 5
PyObject* PyRun_String(const char *str, int start, PyObject *globals, PyObject *locals);
If you want True and False they will have to be in the *globals dict passed to the interpreter. You might be able to fix that by calling PyEval_GetBuiltins.
From the Python 2.6 source code:
if (PyDict_GetItemString(globals, "__builtins__") == NULL) {
    if (PyDict_SetItemString(globals, "__builtins__",
                 PyEval_GetBuiltins()) != 0)
        return NULL;
}
If that doesn't work, you could try to PyRun_String("import __builtin__ as __builtins__", globals, locals) before calling PyRun_String("True", ...).
You might notice the Python interactive interpreter always runs code in the __main__ module which we haven't bothered to create here. I don't know whether you need to have a __main__ module, except that there are a lot of scripts that contain if __name__ == "__main__".