This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: traceback tb_lineno example needs correction for Python3
Type: Stage: resolved
Components: Documentation Versions: Python 3.1, Python 3.2, Python 2.7, Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ezio.melotti Nosy List: ezio.melotti, georg.brandl, mnewman
Priority: normal Keywords:

Created on 2010-02-24 14:12 by mnewman, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
print_traceback_and_exception_rev1.py mnewman, 2010-02-24 14:12 Corrected Example
Messages (4)
msg100033 - (view) Author: Michael Newman (mnewman) Date: 2010-02-24 14:12
In the second example given in "27.8.1. Traceback Examples" at:
http://docs.python.org/3.1/library/traceback.html
http://docs.python.org/py3k/library/traceback.html
which has the lumberjack:

The last line won't work in Python 3.1 and 3.2:
 print("*** tb_lineno:", traceback.tb_lineno(exceptionTraceback))
because "tb_lineno" is no longer an attribute in the "traceback" module.

# Python 2.6 works fine (because Python2 retains the "tb_lineno" attribute in the "traceback" module):
# Python 2.6.4 (r264:75706, Dec  7 2009, 18:45:15) [GCC 4.4.1] on linux2
*** print_tb:
  File "print_traceback_and_exception.py", line 18, in <module>
    lumberjack()
...
*** format_tb:
['  File "print_traceback_and_exception.py", line 18, in <module>\n    lumberjack()\n', '  File "print_traceback_and_exception.py", line 12, in lumberjack\n    bright_side_of_death()\n', '  File "print_traceback_and_exception.py", line 15, in bright_side_of_death\n    return tuple()[0]\n']
('*** tb_lineno:', 18)

# Broken example using Python 3.1:
# Python 3.1.1 (r311:74480, Feb  7 2010, 16:32:28) [GCC 4.4.1] on linux2
mike@ebx2009:/media/Cruzer/notes/Programming/python3/lib/traceback$ python3.1 print_traceback_and_exception.py
*** print_tb:
  File "print_traceback_and_exception.py", line 18, in <module>
    lumberjack()
...
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "print_traceback_and_exception.py", line 38, in <module>
    print("*** tb_lineno:", traceback.tb_lineno(exceptionTraceback))
AttributeError: 'module' object has no attribute 'tb_lineno'

# Broken example using Python 3.2:
# Python 3.2a0 (py3k:78294, Feb 21 2010, 16:37:29) [GCC 4.4.1] on linux2
mike@ebx2009:/media/Cruzer/notes/Programming/python3/lib/traceback$ python3.2 print_traceback_and_exception.py 
*** print_tb:
  File "print_traceback_and_exception.py", line 18, in <module>
    lumberjack()
...
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "print_traceback_and_exception.py", line 38, in <module>
    print("*** tb_lineno:", traceback.tb_lineno(exceptionTraceback))
AttributeError: 'module' object has no attribute 'tb_lineno'

This can be corrected by changing the last line of the example from:
print("*** tb_lineno:", traceback.tb_lineno(exceptionTraceback))
to:
print("*** tb_lineno:", exceptionTraceback.tb_lineno)

# Using my "rev1" example:

# Python 3.1 now sees the brighter side of life:
mike@ebx2009:/media/Cruzer/notes/Programming/python3/lib/traceback$ python3.1 print_traceback_and_exception_rev1.py 
*** print_tb:
  File "print_traceback_and_exception_rev1.py", line 19, in <module>
    lumberjack()
...
*** format_tb:
['  File "print_traceback_and_exception_rev1.py", line 19, in <module>\n    lumberjack()\n', '  File "print_traceback_and_exception_rev1.py", line 13, in lumberjack\n    bright_side_of_death()\n', '  File "print_traceback_and_exception_rev1.py", line 16, in bright_side_of_death\n    return tuple()[0]\n']
*** tb_lineno: 19

For reference:
"print_traceback_and_exception_rev1.py" = example corrected with Python3. The original line has been commented out in case you want to turn it back on and see the error yourself.
msg100079 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2010-02-25 00:08
I think this can be changed on 2.x as well, since the traceback.tb_lineno function "has no use in versions past 2.3" (so maybe it should be deprecated too, at least in the doc). Also the except should catch an IndexError.
msg100983 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2010-03-13 01:31
Fixed in r77895 (trunk), r77896 (py3k) and r77897 (release31-maint), thanks for the report!
msg101006 - (view) Author: Michael Newman (mnewman) Date: 2010-03-13 15:56
No problem. Please note its actually fixed in r78895 (trunk), r78896 (py3k) and r78897 (release31-maint). Your previous message had the svn revision numbers off by 1000.
History
Date User Action Args
2022-04-11 14:56:58adminsetgithub: 52259
2010-03-13 15:56:06mnewmansetmessages: + msg101006
2010-03-13 01:31:35ezio.melottisetstatus: open -> closed
resolution: fixed
messages: + msg100983

stage: needs patch -> resolved
2010-02-25 00:08:24ezio.melottisetpriority: normal

assignee: georg.brandl -> ezio.melotti
versions: + Python 2.6, Python 2.7
nosy: + ezio.melotti

messages: + msg100079
stage: needs patch
2010-02-24 14:12:37mnewmancreate