Embedding bpython from the trepan debuggers

28 views
Skip to first unread message

Rocky Bernstein

unread,
May 28, 2015, 4:24:25 AM5/28/15
to bpy...@googlegroups.com
I have recently been beefing up support of the python trepan debugger to go into both a python and ipython shell. In doing that I came across bpython. 

How does one embed a bpython shell from inside another Python program such as a debugger? 

Here are some issues that come up:

  1. Setting evaluation (locals, globals) to be in the context of the debugged program
  2. Saving/restoring command history
  3. Saving/restoring command completion functions

Bob Farrell

unread,
May 28, 2015, 4:30:22 AM5/28/15
to bpy...@googlegroups.com, rocky.b...@gmail.com
Hi Rocky (awesome name, by the way),

Take a look at the bpdb/ directory in the root of the project directory: https://github.com/bpython/bpython/tree/master/bpdb

If I've understood you correctly this should be a working example of what you're looking for. It looks like you're more than familiar with pdb so hopefully this should be a breeze for you.

Let us now how you get on !

Cheers,

Rocky Bernstein

unread,
May 28, 2015, 8:51:37 AM5/28/15
to bpy...@googlegroups.com, rocky.b...@gmail.com

On Thursday, May 28, 2015 at 4:30:22 AM UTC-4, Bob Farrell wrote:
Hi Rocky (awesome name, by the way),

Take a look at the bpdb/ directory in the root of the project directory: https://github.com/bpython/bpython/tree/master/bpdb

If I've understood you correctly this should be a working example of what you're looking for. It looks like you're more than familiar with pdb so hopefully this should be a breeze for you.

Let us now how you get on !

Ok. I took a look at https://github.com/bpython/bpython/tree/master/bpdb. Although I didn't run that code, something that makes no sense to me is how  instance variables (self) in the BPdb class get picked up by bpython.

Specifically, self.prompt, and self.intro. That definitely didn't work for me and so I wound up setting sys.ps1 for the prompt and passing the "banner" parameter to bpython.embed(). And even if self.intro works, (does it?) passing it as a parameter to embed() seems a lot more straightforward and less magical. 

So that's the stuff I could figure out. Now onto the I can't.

The first problem I have is getting out of bpython. It seems to close sys.stdout. It does this even if inside bpython if I  run "raise SystemExit", "raise SystemError",  quit() or enter Ctrl-D (EOF). So how can I get out of bpython without it closing stdout? 

The second problem I encounter is in running callbacks into the debugger such as for listing files, changing frames for evaluation and so on. I get no output, unless I add a gratuitous print('') statement. And even then I am getting line feeds (\n) without a carriage return. What's going on there? 

The last problem is a more minor. embed() has a parameter to set locals, but none to set globals. I suppose I could fold or put all globals variables into the local scope. But aside from being ugly and potentially time consuming, it's semantically wrong. 

The specific code I have so far is https://github.com/rocky/python2-trepan/blob/master/trepan/processor/command/bpy.py but I'd appreciate it if you'd try it out . I think all of the things I've mentioned above will be clear. Instructions for installing from git are https://github.com/rocky/python2-trepan/wiki/How-to-Install . To run, assuming bpython is already installed, just type:

    $ trepan2 myscript.py
    (trepan2) bpython
    trepan2 python shell with locals
    Use dbgr(*string*) to issue debugger command: *string*
    trepan2 >>> 

    
As you say, since you are familiar with bpython hopefully this will be a breeze for you.

Cheers,
   Rocky

Bob Farrell

unread,
May 28, 2015, 9:45:02 AM5/28/15
to bpy...@googlegroups.com, rocky.b...@gmail.com
Hi Rocky,

Two things to point out: 1) I am the original author of bpython but I haven't actually worked on it in a number of years. 2) bpdb is a contrib, by someone who may or may not be still around.

So I'm afraid I'm not going to be much help - hopefully somebody else will pick this up. :)

--
You received this message because you are subscribed to the Google Groups "bpython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bpython+u...@googlegroups.com.
To post to this group, send email to bpy...@googlegroups.com.
Visit this group at http://groups.google.com/group/bpython.
For more options, visit https://groups.google.com/d/optout.

Thomas Ballinger

unread,
May 28, 2015, 11:50:06 AM5/28/15
to bpy...@googlegroups.com
Hi Rocky,

I looked into this a bit this morning and posted https://github.com/rocky/python2-trepan/pull/8. I've got a few questions, if you've got time today you might pop into #bpython and ping me (ballingt) and we can chat live. I does seem like we should fix embed to take locals and globals.

Tom

Reply all
Reply to author
Forward
0 new messages