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 attribute error
Type: behavior Stage:
Components: Versions: Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, ghazel, pitrou, vstinner
Priority: normal Keywords: needs review, patch

Created on 2008-10-03 23:31 by ghazel, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
use_struct_member.diff benjamin.peterson, 2008-10-04 22:53
Messages (9)
msg74282 - (view) Author: Greg Hazel (ghazel) Date: 2008-10-03 23:31
Unrelated to this bug, I would like to have the ability to remove the 
reference to the frame from the traceback object. Specifically so that 
the traceback object could be stored for a while without keeping all 
the locals alive as well.

So, periodically I test to see if python allows that. Python 2.6 gave 
some strange results compared to 2.5.2:

Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit 
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> try:
...     x = dskjfds
... except:
...     import sys
...     t, v, tb = sys.exc_info()
...
>>> tb
<traceback object at 0x01396670>
>>> dir(tb)
['tb_frame', 'tb_lasti', 'tb_lineno', 'tb_next']
>>> tb.tb_frame
<frame object at 0x01371598>
>>> tb.tb_frame = None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'traceback' object has only read-only attributes (assign 
to .tb_frame)
>>>


Python 2.6 (r26:66721, Oct  2 2008, 11:35:03) [MSC v.1500 32 bit 
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> try:
...     x = lfdskf
... except:
...     import sys
...     t, v, tb = sys.exc_info()
...
>>> tb
<traceback object at 0x01581F80>
>>> dir(tb)
['tb_frame', 'tb_lasti', 'tb_lineno', 'tb_next']
>>> tb.tb_frame
<frame object at 0x013B9E10>
>>> tb.tb_frame = None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'traceback' object has no attribute 'tb_frame'
>>>
msg74326 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-10-04 22:53
Here's a patch. It looks like traceback just needs to use a struct
sequence instead of providing tp_getattr its self.
msg74330 - (view) Author: Greg Hazel (ghazel) Date: 2008-10-04 23:31
There seem to be some other exception type and string inconsistencies, 
but they are not new to Python 2.6

>>> tb.tb_frame = None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'traceback' object has only read-only attributes (assign 
to .tb_frame)

>>> tb.tb_frame.f_locals = None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: attribute 'f_locals' of 'frame' objects is not writable

>>> tb.tb_frame.f_globals = None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: readonly attribute

>>> dict.clear = "foo"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type 'dict'


Should it be an AttributeError or TypeError? Should it be "read-only", 
readonly", "not writable" or "can't set"?


Btw, here's the other ticket for the feature request: 
http://bugs.python.org/issue1565525
msg74405 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2008-10-06 22:52
Instead of converting tb_frame attribute to read only, I prefer to 
allow the user to clear the traceback to free some memory bytes. So I 
wrote a different patch.

marge$ ./python
Python 2.7a0 (trunk:66786M, Oct  7 2008, 00:48:32)
>>> try:
...  a
... except:
...  import sys
...  t, v, tb = sys.exc_info()
...
>>> tb
<traceback object at 0xb7c76edc>
>>> tb.tb_frame
<frame object at 0x81cdbec>
>>> tb.tb_frame=42
>>> tb.tb_frame
42
>>> tb.tb_frame=None
>>>
msg74783 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2008-10-14 23:33
My patch causes a crash with:

import sys
try:
    raise Exception("hm!")
except:
    t, v, tb = sys.exc_info()
    tb.tb_frame = {}
    raise t, v, tb

Change tb.tb_frame value is not a good idea. It's better to clear 
locals/globals: see msg74329.
msg78411 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2008-12-28 15:37
One possibility would be to only allow deleting the tb_frame attribute
(setting it to NULL), not setting it to an arbitrary object.
msg83719 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2009-03-17 23:52
Ping. benjamin.peterson's patch fixes the very strange issue, and I 
would like to see it upstream.

About clearing the frame/traceback, it's another issue (#1565525).
msg83776 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-03-18 20:52
Fixed in r70463.
msg83780 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2009-03-18 21:35
@benjamin.peterson: Cool! Thanks.
History
Date User Action Args
2022-04-11 14:56:40adminsetgithub: 48284
2009-03-18 21:35:15vstinnersetmessages: + msg83780
2009-03-18 20:52:52benjamin.petersonsetstatus: open -> closed
resolution: fixed
messages: + msg83776
2009-03-17 23:52:56vstinnersetmessages: + msg83719
2008-12-28 15:37:03pitrousetnosy: + pitrou
messages: + msg78411
2008-10-14 23:33:49vstinnersetfiles: - traceback_tb_frame_setter.patch
2008-10-14 23:33:41vstinnersetmessages: + msg74783
2008-10-06 22:52:24vstinnersetfiles: + traceback_tb_frame_setter.patch
nosy: + vstinner
messages: + msg74405
2008-10-04 23:31:42ghazelsetmessages: + msg74330
2008-10-04 22:53:12benjamin.petersonsetkeywords: + needs review, patch
files: + use_struct_member.diff
messages: + msg74326
nosy: + benjamin.peterson
2008-10-03 23:31:58ghazelcreate