I am trying to write a program that finds exceptions in a users program
I have a file called test.py which is :
try:
# some code to get the name of the calling program
#Then the code is like "import filename.py" , for example the next line
import sample.py # this is what should happen when the file name is got
execfile("test.py") # to run this file
catch:
# catching some exceptions here
now if I assume the user has a file say sample.py which looks like
import test.py
def __main__():
4/0 # sample exception
I am new to python programming , but I know that python treats all programs as modules , so essentially I envision the program flow to be
1) when the user runs sample.py , the control flow goes to test.py which then gets the name of the calling program and imports it in the try block.
2) the flow then goes back to sample.py and ideally I would like the exception to be encountered and caught
3) but when the control comes back to sample.py it gies back to test.py causing an recursive loop
my question is two fold
1) Is there a way to get the name of a program that has imported your module. also is there a way to get the name of a program that is in the same workspace ( same folder)
2) The reason I have this control flow is I would like the user not to open my program (test.py) , but no matter how I see it , if my module is just imported into his program it will run like a recursive loop , Any suggestions to modify my design to make it work , maybe a third script to get it together?
I am using python 2.7
The strange design pattern is because I do not want the user to write any code to run my program , the max he should be writing is just importing my program
Okay, so two routes:
First of all,
__name__contains the name of the module that you have been imported from.Secondly, my personal suggestion is distributing a test.pyc or something and asking the person to run that .pyc so that they do not know the contents of test.py