Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #42276 > unrolled thread

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

Started byNick Gnedin <ngnedin@gmail.com>
First post2013-03-29 16:04 -0500
Last post2013-03-29 22:52 -0700
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  dir() vs print(dir()) in the embedded mode Nick Gnedin <ngnedin@gmail.com> - 2013-03-29 16:04 -0500
    Re: dir() vs print(dir()) in the embedded mode Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-29 22:16 +0000
    Re: dir() vs print(dir()) in the embedded mode rusi <rustompmody@gmail.com> - 2013-03-29 22:52 -0700

#42276 — dir() vs print(dir()) in the embedded mode

FromNick Gnedin <ngnedin@gmail.com>
Date2013-03-29 16:04 -0500
Subjectdir() vs print(dir()) in the embedded mode
Message-ID<mailman.3964.1364591074.2939.python-list@python.org>
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;
}

[toc] | [next] | [standalone]


#42287

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-03-29 22:16 +0000
Message-ID<515612b4$0$29974$c3e8da3$5496439d@news.astraweb.com>
In reply to#42276
On Fri, 29 Mar 2013 16:04:24 -0500, Nick Gnedin wrote:

> 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.



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#42324

Fromrusi <rustompmody@gmail.com>
Date2013-03-29 22:52 -0700
Message-ID<8479da74-58aa-4521-b096-ee748dbb21d6@5g2000pbs.googlegroups.com>
In reply to#42276
On Mar 30, 2:04 am, Nick Gnedin <ngne...@gmail.com> wrote:
> 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.

>
> 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;
>
>
>
>
>
>
>
> }

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web