2

I am wondering if it is possible to edit/customize the behavior and printout of built-in errors in Python. For example, if I type:

>>> a = 1
>>> print A
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'A' is not defined

I want the output to instead be:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'A' is not defined. Check capitalization.

Moreover, I want this to occur at a global level, for ALL FUTURE CODE, without having to explicitly include an exception in my code. If such a change is possible, I would assume this needs to be done at the very source or library-file level of Python. However, I am not sure where exactly to look to know if this is even possible.

I am using Python 2.7 on both Ubuntu and OSX, so help on either system would be appreciated.

(My apologies in advance if this is covered elsewhere, but searching for threads on "changing Python error messages" generally gave me topics on Exceptions, which is not necessarily my interest here. If anyone can point me to a page on this though, I'd greatly appreciate it.)

Dennis
  • 111
  • 6
  • @shash678 The topic that you brought does not respond to the current question – KromviellBlack Dec 05 '16 at 03:00
  • You'll likely have to play around with the source code, and I believe they are likely in C, so you would have to rebuild Python. Messing with the Python source is a *bad idea* unless you *really know what you are doing*. – juanpa.arrivillaga Dec 05 '16 at 03:04
  • You will have to add more logic to the source code. For eg. even if you do print A when both a and A are not defined it will give you same error. The very fact that you don't know where to edit tells that you need to first study the CPython compiler and how exceptions works before you can start. – Mohammad Yusuf Dec 05 '16 at 03:17
  • Thank you for the responses. I understand that it is not advisable to edit and rebuild the python source code if I don't know what I am doing, but that is the reason I wanted to ask it here...for some advice on where to start. From my experience learning how python works so far, it's been generally most educational when I can work on a problem and study the relevant documents at the same time, rather than trying to find a starting point in the documents on my own. – Dennis Dec 05 '16 at 04:12

1 Answers1

1

YES! There is a way to exactly what you want! traceback.py is the program that detects errors in your code. It then gives you an explanation of what happened (creates the error message that you see.) You can find this file in your library folder for python. When in that file you can change the messages that it outputs when you come across an error! Please tell me if this helped you!

Qwerty
  • 1,252
  • 1
  • 11
  • 23
  • Wonderful! This most certainly did help me, thank you very much! Oddly enough, however, the only error type that doesn't seem to be included explicitly in the file is the NameError, but others like SyntaxError and ValueError are. Regardless, this is exactly the help I was looking for, since it definitely gives me a place to start. I will update this thread if I figure out where the NameError can be edited. – Dennis Dec 05 '16 at 18:51
  • I'm glad I helped! – Qwerty Dec 05 '16 at 22:11