Jul-11-2019, 01:10 PM
Hi,
As you might know, there is the #line command in the C-preprocessor:
I tried:
As you might know, there is the #line command in the C-preprocessor:
#include <stdio.h> #line 20 "abc" int main() { printf("%i %s\n", __LINE__, __FILE__); //outputs 22 abc }Is there something like this #line command in python? I want to change what is printed in exceptions.
I tried:
import inspect frame = inspect.currentframe() fileNo = frame.f_lineno frame.f_lineno=1000But this resulted in the output
Output:Traceback (most recent call last):
File "pythontest.py", line 4, in <module>
frame.f_lineno=1000
ValueError: f_lineno can only be set by a trace function
In case you were wondering why I would want to change the filename and filenumber: In my workflow, I have a files that contain python source code and other text. A script searches those python source code blocks, puts them into the right order, writes them into a file (called sum.py) and executes this file. This works fine, but obviously exceptions show the linenumber in sum.py and not the linenumber in the original files.