How to detect that Python code is being executed through the debugger?

后端 未结 8 1727
清酒与你
清酒与你 2020-12-24 12:20

Is there a simple way to detect, within Python code, that this code is being executed through the Python debugger?

I have a small Python application that uses Java c

相关标签:
8条回答
  • You can try to peek into your stacktrace.

    https://docs.python.org/library/inspect.html#the-interpreter-stack

    when you try this in a debugger session:

    import inspect
    inspect.getouterframes(inspect.currentframe()
    

    you will get a list of framerecords and can peek for any frames that refer to the pdb file.

    0 讨论(0)
  • 2020-12-24 13:19

    Python debuggers (as well as profilers and coverage tools) use the sys.settrace function (in the sys module) to register a callback that gets called when interesting events happen.

    If you're using Python 2.6, you can call sys.gettrace() to get the current trace callback function. If it's not None then you can assume you should be passing debug parameters to the JVM.

    It's not clear how you could do this pre 2.6.

    0 讨论(0)
提交回复
热议问题