Re: [capi-sig] PyRun_InteractiveOne from string?

Thanks so much.. that looks like exactly what I want to do. Looks like you've done a fantastic job on the integration there - makes my weedy little console look very plain :)
Just need to work out why boost::python won't exec code.InteractiveConsole() now :)
Thanks again. Dave.
On Thu, Jun 10, 2010 at 9:29 PM, Campbell Barton <ideasman42@gmail.com>wrote:
In blender 2.5 we have an interactive console with multi-line support and auto complete. Demo of using the console
http://sites.google.com/site/satishgoda/blender/learningblender25/introducti...
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__} code.InteractiveConsole(namespace) 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@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@python.org http://mail.python.org/mailman/listinfo/capi-sig
--
- Campbell
capi-sig mailing list capi-sig@python.org http://mail.python.org/mailman/listinfo/capi-sig

I should have mentioned the console I linked to is not thread safe, I asked in #python about this and apparently there are 2 ways to work around this.
- use a context manager to work out which thread is taking to the overridden stdout.
- override print() not to use sys.stdout, since the consoles "write" function can already redirect exception output.
Id like to make this thread safe but for now its not a priority for me.
From the script I linked this looks like its done entirely in python, this is because we have a py/c api so python has access to the command line and can add output to the display, rather then having C call python directly but both ways can work.
On Fri, Jun 11, 2010 at 10:16 PM, Dave Brotherstone <davegb@pobox.com> wrote:
Thanks so much.. that looks like exactly what I want to do. Looks like you've done a fantastic job on the integration there - makes my weedy little console look very plain :)
Just need to work out why boost::python won't exec code.InteractiveConsole() now :)
Thanks again. Dave.
On Thu, Jun 10, 2010 at 9:29 PM, Campbell Barton <ideasman42@gmail.com>wrote:
In blender 2.5 we have an interactive console with multi-line support and auto complete. Demo of using the console
http://sites.google.com/site/satishgoda/blender/learningblender25/introducti...
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__} code.InteractiveConsole(namespace) 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@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@python.org http://mail.python.org/mailman/listinfo/capi-sig
--
- Campbell
capi-sig mailing list capi-sig@python.org http://mail.python.org/mailman/listinfo/capi-sig
capi-sig mailing list capi-sig@python.org http://mail.python.org/mailman/listinfo/capi-sig
--
- Campbell

Thanks for this. It's working perfectly now. Funnily enough threading was the next thing on the list, so the (non-console) scripts run in a separate thread, allowing them to be stopped by the UI. You don't happen to know if PyErr_SetString is thread safe, to "break" the currently running script?
I'll have to think about whether taking care of separate threads redirecting sys.stdout is important - I'm not sure that it is in our environment.
Cheers, Dave.
On Tue, Jun 15, 2010 at 11:53 AM, Campbell Barton <ideasman42@gmail.com>wrote:
I should have mentioned the console I linked to is not thread safe, I asked in #python about this and apparently there are 2 ways to work around this.
- use a context manager to work out which thread is taking to the overridden stdout.
- override print() not to use sys.stdout, since the consoles "write" function can already redirect exception output.
Id like to make this thread safe but for now its not a priority for me.
From the script I linked this looks like its done entirely in python, this is because we have a py/c api so python has access to the command line and can add output to the display, rather then having C call python directly but both ways can work.
Thanks so much.. that looks like exactly what I want to do. Looks like you've done a fantastic job on the integration there - makes my weedy
On Fri, Jun 11, 2010 at 10:16 PM, Dave Brotherstone <davegb@pobox.com> wrote: little
console look very plain :)
Just need to work out why boost::python won't exec code.InteractiveConsole() now :)
Thanks again. Dave.
On Thu, Jun 10, 2010 at 9:29 PM, Campbell Barton <ideasman42@gmail.com wrote:
In blender 2.5 we have an interactive console with multi-line support and auto complete. Demo of using the console
http://sites.google.com/site/satishgoda/blender/learningblender25/introducti...
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__} code.InteractiveConsole(namespace) 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@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@python.org http://mail.python.org/mailman/listinfo/capi-sig
--
- Campbell
capi-sig mailing list capi-sig@python.org http://mail.python.org/mailman/listinfo/capi-sig
capi-sig mailing list capi-sig@python.org http://mail.python.org/mailman/listinfo/capi-sig
--
- Campbell
participants (2)
-
Campbell Barton
-
Dave Brotherstone