how to debug exec('code_string')?

I need to refactor a legacy application (Python3.7).
It contains a dynamic code import mechanism which gets strings to be executed.

Is there a way to debug into such a string? i.e. can I set a breakpoint?

Upload id: 2022_06_13_oUBNUgw7sTQT2QZWKrrfun (file: string_execute_example.py)

# get a string containing python code and execute it:

def other_function_to_call(name):
print(f'other_function_to_call: {name}') # Press Strg+F8 to toggle the breakpoint.
def more_to_execute():
print(f'more_to_execute() ..') # Press Strg+F8 to toggle the breakpoint.

# can I set a breakpoint in this code?
code_string = '''
other_function_to_call('hello from local string')
pass
more_to_execute()
pass
'''

if __name__ == '__main__':
print(f'text to execute:\n>>>{code_string}<<<')
exec (code_string)

2 comments
Comment actions Permalink

u1550 to set a breakpoint you simply click the mouse to the left of the line of code in the column and a red dot should appear. Here is a video of what I 'm talking about, Jetbrains has a ton of these lately they have been pumping out more content. https://blog.jetbrains.com/idea/2020/05/debugger-basics-in-intellij-idea/

0
Comment actions Permalink

andrew ,

you may have missed that my code is not in a file, but in a string.

See example above: variable 'code_string' contains the code I want to debug. 

 

0

Please sign in to leave a comment.