← 输入框|github.com|刷新
Skip to content

gh-89290: Add notes to the exception for an unmarshallable object - #160939

Open
vasiliyk wants to merge 1 commit into
python:mainfrom
vasiliyk:gh-89290-marshal-notes
Open

vasiliyk wants to merge 1 commit into
python:mainfrom
vasiliyk:gh-89290-marshal-notes

Conversation

@vasiliyk

@vasiliyk vasiliyk commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Add notes to the ValueError raised by marshal.dumps() for an unsupported object.

The notes follow pickle's convention: one note per enclosing level, innermost first, with the same wording for container items, dict values and set elements.

3 differences with pickle:

  • a failing dict key gets a note, pickle leaves it unannotated;
  • a failing slice field is named directly (start, stop, step), pickle reports a tuple index inside the reconstructor arguments;
  • code object fields are identified by name, qualified name, file and line, pickle rejects code objects.

current:

>>> import marshal
>>> class MyStr(str): pass
... marshal.dumps(compile("def f(): pass", MyStr("foo.py"), "exec"))
... 
Traceback (most recent call last):
  File "<python-input-1>", line 2, in <module>
    marshal.dumps(compile("def f(): pass", MyStr("foo.py"), "exec"))
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: cannot marshal MyStr objects

with notes:

>>> import marshal
>>> class MyStr(str): pass
... 
>>> marshal.dumps(compile("def f(): pass", MyStr("foo.py"), "exec"))
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    marshal.dumps(compile("def f(): pass", MyStr("foo.py"), "exec"))
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: cannot marshal MyStr objects
when marshalling co_filename of code object f (foo.py, line 1)
when marshalling tuple item 0
when marshalling co_consts of code object <module> (foo.py, line 1)

New tests cover every container type, slice field and code object field.

Each enclosing container item and code object field on the path to the
unsupported object gets a note, innermost first, as in pickle and json.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant