KLZY
KLZY

Reputation: 83

disabling python script tracing, equivalent of turning off -x in bash

I have python scripts which I'd like to disable tracing on. By tracing, I mean the ability to run:

python -m trace --tracing thescript.py

In bash, if you want to see the inner workings of a script, you can just run the following:

sh -x thescript.sh

or

bash -x thescript.sh

However, if thescript.sh contains a set +x; that will stop the external sh -x or bash -x from showing any further inner workings of the script past the line containing the set +x.

I want the same for python. I'm only aware of python -m trace --tracing as the only way to see the inner workings of a python script. I'm sure there are many different ways to do this. What I'm looking for here is a solid method to use to stop any type of tracing that could be done to a python script.

I understand if a user has the proper permissions, they can edit the script and comment out whatever is put in to disable tracing. I'm aware of that. But i would still like to know.

Upvotes: 0

Views: 218

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295472

Taking even a cursory look at trace.py from the standard library makes the interfaces it uses, and thus the mechanism to disable them, clear:

#!/usr/bin/env python
import sys, threading
sys.settrace(None)
threading.settrace(None)
print("This is not traced")

Of course, this won't (and can't) work against anyone who wants to modify the live interpreter to NOP out those calls; the usual caveats about anyone who owns and controls the hardware being able to own and control the software apply.

Upvotes: 2

Related Questions