Python Forum
Change linenumber and filename printed in exceptions like #line in C
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Change linenumber and filename printed in exceptions like #line in C
#1
Hi,
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=1000
But 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.
Reply


Messages In This Thread
Change linenumber and filename printed in exceptions like #line in C - by kryptomatrix - Jul-11-2019, 01:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  coma separator is printed on a new line for some reason tester_V 4 419 Feb-02-2024, 06:06 PM
Last Post: tester_V
  PiCamera - print exceptions? korenron 2 792 Dec-15-2022, 10:48 PM
Last Post: Larz60+
  How can histogram bins be separated and reduce number of labels printed on x-axis? cadena 1 853 Sep-07-2022, 09:47 AM
Last Post: Larz60+
  Class exceptions DPaul 1 1,259 Mar-11-2022, 09:01 AM
Last Post: Gribouillis
  is this a good way to catch exceptions? korenron 14 4,593 Jul-05-2021, 06:20 PM
Last Post: hussaind
  Python, exceptions KingKhan248 6 2,947 Nov-15-2020, 06:54 AM
Last Post: buran
  Split string between two different delimiters, with exceptions DreamingInsanity 2 1,982 Aug-24-2020, 08:23 AM
Last Post: DreamingInsanity
  handling 2 exceptions at once Skaperen 2 2,263 Jun-27-2020, 08:55 AM
Last Post: Yoriz
  remove spaces with exceptions catosp 4 2,366 May-29-2020, 09:32 AM
Last Post: catosp
  Reverse printed words hindubiceps 7 2,917 Apr-21-2020, 05:19 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020