[capi-sig] PyRun_InteractiveOne from string?

Campbell Barton ideasman42 at gmail.com
Thu Jun 10 21:29:52 CEST 2010

In blender 2.5 we have an interactive console with multi-line support
and auto complete.
Demo of using the console

Heres the script which interfaces python and blenders generic console
api, look at how it uses the 'code' module.

We do our own line editing functionality but basically you can use the
code module like this
  import code
  namespace = {'__builtins__': __builtins__}
  is_multiline = console.push(line_exec) # <--- this is the line you
need to run in a loop and give user input to.

even though you mention not having a console, for the record that can
be done like this...
PyRun_String("__import__('code').interact()", Py_eval_input,
namespace, namespace);

On Thu, Jun 10, 2010 at 8:59 PM, Dave Brotherstone <davegb at pobox.com> wrote:
> Hi,
> I hope I'm asking this in the right place...  I've embedded Python in an
> application, and I'm now trying to emulate a "console" for immediate
> commands.  I can see PyRun_InteractiveOneFlags and the variants are used to
> send single statements, but there doesn't seem to be any variants that take
> a string, only a FILE*.  I'm in a windows app, so there's no console.  (I
> actually have effectively two text boxes, one multi-line read only for
> stdout/stderr, and one input.), I know the prompt would be in the wrong
> place, but that's no biggie.
> PyRun_String does almost what I want, except that it fails on multiline
> statements (if ... : etc).
> I just wanted to check if there's an API I've missed, or, whether anyone can
> recommend an approach.
> I think I could either:
> a) Emulate the console through an FILE* - not sure quite how this works in
> Windows, but I'm sure it's possible
> b) Copy what PyRun_InteractiveOneFlags() does, just replacing
> the PyParser_ASTFromFile with PyParser_ASTFromString.  Only problem here is
> that I seem to need mod_ty, and not sure whether it's a good idea to use
> something that looks like an internal type.
> Many thanks for any comments.
> Dave.
> _______________________________________________
> capi-sig mailing list
> capi-sig at python.org
> http://mail.python.org/mailman/listinfo/capi-sig

- Campbell

More information about the capi-sig mailing list