lezebulon
lezebulon

Reputation: 7994

Simulate shell behavior (force eval of last command to be displayed)

In a python shell, if I type a = 2 nothing is printed. If I type a 2 gets printed automatically. Whereas, this doesn't happen if I run a script from idle.

I'd like to emulate this shell-like behavior using the python C api, how is it done?

For instance, executing this code PyRun_String("a=2 \na", Py_file_input, dic, dic); from C, will not print anything as the output. I'd like to simulate a shell-like behavior so that when I execute the previous command, the value "2" is stored in a string. Is it possible to do this easily, either via python commands or from the C api? Basically, how does the python shell do it?

Upvotes: 1

Views: 97

Answers (1)

user2357112
user2357112

Reputation: 281604

To compile your code so expression statements invoke sys.displayhook, you need to pass Py_single_input as the start parameter, and you need to provide one statement at a time.

Upvotes: 1

Related Questions