Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3481322
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:26:09+00:00 2026-05-18T10:26:09+00:00

In my python code I have this line: try: result = eval(command, self.globals, self.locals)

  • 0

In my python code I have this line:

try:
    result = eval(command, self.globals, self.locals)
except SyntaxError:
    exec(command, self.globals, self.locals)

The command variable can be any string. Hence the python debugger pdb may be started in eval/exec and still be active when eval/exec is returning. What I want to do is make sure normal program execution is resumed when returning from eval/exec. To just give you an idea, this is approximately the behavior that I want:

try:
    result = eval(command, self.globals, self.locals)
    try: self.globals['pdb'].run('continue')
    except: pass
except SyntaxError:
    exec(command, self.globals, self.locals)
    try: self.globals['pdb'].run('continue')
    except: pass

However the try line is shown in the debugger before it is executed, but I dont want the debugger to show my code at all. Also it doesn’t really work… The reason i repeat code is to minimize debugging in my code, else I could just do it after the except block.

So how can I do this?


As a sidenote:

If you try to enter the following lines into the IPython or bpython interpreters you’ll see that they have the same problem and you are able to step into their code.

import pdb
pdb.set_trace()
next

However if you do this in the standard cpython interpreter you are returned to the python prompt. The reason for this is obviously because the two former are implemented in python and the last one is not. But my wish is to get the same behavior even when all code is python.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:26:10+00:00Added an answer on May 18, 2026 at 10:26 am

    While I’m somewhat concerned that you are eval/exec’ing a string that you don’t control, I’ll assume you’ve thought that bit through.

    I think the simplest thing would be to persuade pdb to check the stack frame on each step and resume automatically when you return to the desired level. You can do that with a simple bit of hotfixing. In the code below I’ve simplified it down to a simple eval since all you are really asking is to have pdb resume automatically on return to a specific function. Call Pdb().resume_here() in the function that you don’t want traced. N.B. the resumption is global and there’s only one resumption point but I’m sure you can modify that if you wanted.

    If you run the code then you’ll enter the debugger in function foo() and you can then single step but as soon as you return to bar() the code continues automatically.

    e.g.

    import sys
    from pdb import Pdb
    
    def trace_dispatch(self, frame, event, arg):
        if frame is self._resume_frame:
            self.set_continue()
            return
        return self._original_trace_dispatch(frame, event, arg)
    
    def resume_here(self):
        Pdb._resume_frame = sys._getframe().f_back
    
    # hotfix Pdb
    Pdb._original_trace_dispatch = Pdb.trace_dispatch
    Pdb.trace_dispatch = trace_dispatch
    Pdb.resume_here = resume_here
    Pdb._resume_frame = None
    
    def foo():
        import pdb
        pdb.set_trace()
        print("tracing...")
        for i in range(3):
            print(i)
    
    def bar():
        Pdb().resume_here()
        exec("foo();print('done')")
        print("returning")
    
    bar()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.