New submission from Stephen Kelly:

Consider the following three snippets:


1) 

const char* sourceCode = 
    "a = 9\n"
    "a";
// This is OK! Python runs both lines.
// BUT: The value of 'a' is not printed
PyRun_StringFlags(sourceCode, Py_file_input, localDictionary, localDictionary, 
0);


2)

// This is OK! We run one statement at a time:
PyRun_StringFlags("a = 9", Py_single_input, localDictionary, localDictionary, 
0);
// Python prints the value of 'a' because we use Py_single_input!
PyRun_StringFlags("a", Py_single_input, localDictionary, localDictionary, 0);


3)

const char* sourceCode = 
    "a = 9\n"
    "a";
// This is NOT OK! Python throws a SyntaxError because we used Py_single_input.
PyRun_StringFlags(sourceCode, Py_single_input, localDictionary, 
localDictionary, 0);




The intention is to be able to run script code in an interpreter built into an 
application, and to maintain two user features:

1) The behavior is the same as the standard python interpreter with regard to 
printing values automatically without requiring the print() statement.
2) It is allowed to copy/paste possibly multiple lines/statements and execute 
them

These two requirements are in conflict, because while Py_single_input enables 
the first, it forbids the second.

I have worked around this by using internal API in Python-ast.h and setting 
`mod->kind = Interactive_kind;` before calling `PyAST_CompileEx`, but that 
should not be needed.

----------
components: Interpreter Core
messages: 298162
nosy: steveire
priority: normal
severity: normal
status: open
title: Embedding should have public API for interactive mode
versions: Python 3.6

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue30905>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to