ionelmc / python-tblib

Serialization library for Exceptions and Tracebacks.
BSD 2-Clause "Simplified" License
165 stars 33 forks source link

Use a dict to de-duplicate filenames, and thus reduce the size of the to_dict() #38

Open ShaheedHaque opened 6 years ago

ShaheedHaque commented 6 years ago

Here is the patch I had in mind for #37. I've left the doctest's failing like this so you can see what the functional change is (and also because I'm not yet familiar enough with doctest :-)):

367     >>> import json
368     >>> from pprint import pprint
369     >>> try:
Differences (unified diff with -expected +actual):
    @@ -1,17 +1,19 @@
    -{'tb_frame': {'f_code': {'co_filename': '<doctest README.rst[...]>',
    -                         'co_name': '<module>'},
    +{'tb_frame': {'f_code': {'co_filename': 3, 'co_name': '<module>'},
                   'f_globals': {'__name__': '__main__'}},
      'tb_lineno': 2,
    - 'tb_next': {'tb_frame': {'f_code': {'co_filename': ...
    -                                     'co_name': 'inner_2'},
    + 'tb_next': {'tb_frame': {'f_code': {'co_filename': 2, 'co_name': 'inner_2'},
                               'f_globals': {'__name__': '__main__'}},
                  'tb_lineno': 2,
    -             'tb_next': {'tb_frame': {'f_code': {'co_filename': ...
    +             'tb_next': {'tb_frame': {'f_code': {'co_filename': 1,
                                                      'co_name': 'inner_1'},
                                           'f_globals': {'__name__': '__main__'}},
                              'tb_lineno': 2,
    -                         'tb_next': {'tb_frame': {'f_code': {'co_filename': ...
    +                         'tb_next': {'tb_frame': {'f_code': {'co_filename': 0,
                                                                  'co_name': 'inner_0'},
                                                       'f_globals': {'__name__': '__main__'}},
                                          'tb_lineno': 2,
    -                                     'tb_next': None}}}}
    +                                     'tb_next': None}}},
    + 'zip': {'<doctest README.rst[3]>': 0,
    +         '<doctest README.rst[40]>': 3,
    +         '<doctest README.rst[4]>': 1,
    +         '<doctest README.rst[5]>': 2}}

The basic change is that the co_filename, which often contains non-trivial filenames and duplicates too, stores an integer instead, and an extra "zip" dict at the top level provides the de-dupe service.

One other thing: for reasons I don't quite understand, on my dev system, though not in these tests, the f_globals dict also has a __file__ which I also took care of via "zip"...I don't understand why that field is not present here. It also stores the filename, and is a major cause of duplicates for my setup. (Yes, I'm baffled by this).

luckydonald commented 6 months ago

I feel like the zip dict should be reversed to make lookup easier. {3: "<doctest README.rst[40]>"}.

Also the zip thing isn't very expected.

Maybe we can borrow syntax from other such config systems, to stay familiar? I'm thinking about the object refs in openapi, or the yaml references.