[capi-sig] PyRun_InteractiveOne from string?

Campbell Barton ideasman42 at gmail.com
Tue Jun 15 11:53:16 CEST 2010


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 at 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 at 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/introduction-to-blender-python-api
>>
>> Heres the script which interfaces python and blenders generic console
>> api, look at how it uses the 'code' module.
>>
>> http://www.google.com/codesearch/p?hl=en#J_CGftyjihw/release/scripts/op/console_python.py&q=console_python&sa=N&cd=1&ct=rc
>>
>> 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 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
>> _______________________________________________
>> capi-sig mailing list
>> capi-sig at python.org
>> http://mail.python.org/mailman/listinfo/capi-sig
>>
> _______________________________________________
> 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