Skip to content

Pickling is losing some fields on exceptions #103333

Description

@csm10495

Bug report

Say we have an AttributeError with some fields on it (like name). When we pickle and unpickle it, we can see that .name (at least) is lost (and set to None). Optimally those fields should still be in-tact on the unpickled instance.

C:\Users\csm10495\Desktop>python
Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle
>>> a = AttributeError("test text", name="test name", obj="test obj")
>>> a.name
'test name'
>>> pickle.loads(pickle.dumps(a)).name
>>> pickle.loads(pickle.dumps(a))
AttributeError('test text')

Your environment

C:\Users\csm10495\Desktop>python --version
Python 3.11.0

C:\Users\csm10495\Desktop>ver

Microsoft Windows [Version 10.0.19045.2364]

C:\Users\csm10495\Desktop>

Same thing seems to happen on 3.10.7 and 3.11.3 as well.

Linked PRs

Activity

  1. csm10495 commented on Apr 7, 2023

    @csm10495
    ContributorAuthor

    Why don't values like name/obj appear in __dict__ for exceptions? I see that if that were the case, BaseException's default reduce should have worked out here.

  2. sobolevn commented on Apr 7, 2023

    @sobolevn
    Member

    It looks very related to #73652
    If you agree, let's move there :)

  3. csm10495 commented on Apr 7, 2023

    @csm10495
    ContributorAuthor

    I think its the same overall problem, but i'm not really fit to come up with the be-all-end-all solution to the problem for all exceptions and their fields. (Which is where that issues seems to point).

    For now I'd like to just try to fix this for AttributeError (if that's ok)... i've raised #103352 if you could check it.

  4. added
    3.11only security fixes
    3.12only security fixes
    stdlibStandard Library Python modules in the Lib/ directory
    on Apr 21, 2023
  5. added a commit that references this issue on May 12, 2023
  6. added
    type-featureA feature request or enhancement
    and removed
    3.11only security fixes
    on May 12, 2023
  7. gpshead commented on May 12, 2023

    @gpshead
    Member

    Thanks for the contribution, merged! I'm considering this a feature and doing it for 3.12 only as it can be seen as an API enhancement otherwise so doing this mid-cycle in 3.11 unless it were a regression from past releases doesn't feel right.

    @sobolevn is right that there is a larger all Exceptions issue that could be dealt with, but this is still a meaningful improvement in the interim.

  8. added a commit that references this issue on May 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errortype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions