dir() vs print(dir()) in the embedded mode

N

Nick Gnedin

Folks,

I have a newbie question: I am trying to embed Python into my
application. While playing around, I noticed that the behavior of the
interpreter in the embedded mode differs from the standalone one.

Namely, in the standalone mode if I type dir(), I get a list of build-in
symbols. In the embedded mode only print(dir()) does that, while just
dir() returns silently.

Is there a way to intercept the output of dir() (and all other commands)
and display them to the user?

Here is an example code that illustrates the behavior (the first call to
PyRun_SimpleString() returns silently).

Many thanks for your future hints,

Nick


#include <Python.h>

int main()
{
Py_Initialize();
PyRun_SimpleString("dir()");
printf("-----\n");
PyRun_SimpleString("print(dir())");
Py_Finalize();

return 0;
}
 
S

Steven D'Aprano

Folks,

I have a newbie question: I am trying to embed Python into my
application. While playing around, I noticed that the behavior of the
interpreter in the embedded mode differs from the standalone one.

Namely, in the standalone mode if I type dir(), I get a list of build-in
symbols. In the embedded mode only print(dir()) does that, while just
dir() returns silently.

What do you mean by "standalone mode"? If you are talking about the
interactive interpreter, that is expected behaviour. The interactive
interpreter automatically prints the output of every line.

When running non-interactively as a script (whether embedded or not), if
you want to print the output of a line, you will have to call print on
that output.

Is there a way to intercept the output of dir() (and all other commands)
and display them to the user?

Automatically? I don't believe so. I think it is hard-coded behaviour of
the interactive interpreter, and cannot by enabled for scripts.
 
R

rusi

Folks,

I have a newbie question: I am trying to embed Python into my
application. While playing around, I noticed that the behavior of the
interpreter in the embedded mode differs from the standalone one.

Namely, in the standalone mode if I type dir(), I get a list of build-in
symbols. In the embedded mode only print(dir()) does that, while just
dir() returns silently.

This:
http://docs.python.org/2/library/custominterp.html
is (or can be conceptualized as providing) the interactive interpreter
behavior.
IOW the interactive interpreter (aka REPL) is a packaging of that.
Is there a way to intercept the output of dir() (and all other commands)
and display them to the user?

You are looking at it upside down (as MRAB hinted).
You dont want to intercept (aka monkeypatch or subtract) from a
functional ie value-returning dir etc
Instead you want to add (your own custom) REPL behavior.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,829
Latest member
PIXThurman

Latest Threads

Top