[New-bugs-announce] [issue39750] UnicodeError becomes unpicklable if data is appended to args

João Eiras report at bugs.python.org
Tue Feb 25 11:15:36 EST 2020


New submission from João Eiras <joao.eiras at gmail.com>:

Given some exception `ex`, you can append data like
  ex.args += (value1, value2, ...)
and then re-raise.

This is something I do in my projects to sometime propagate context when errors are raised, e.g., stacktraces across process boundaries or blobs of text with pickling or unicode errors.

When this is done with UnicodeError, the exception becomes non-unpicklable:

  TypeError: function takes exactly 5 arguments (6 given)

Example:
    import pickle

    def test_unicode_error_unpickle():
        ex0 = UnicodeEncodeError('ascii','message', 1, 2, 'e')
        ex0.args += ("extra context",)
        ex1 = pickle.loads(pickle.dumps(ex0))
        assert type(ex0).args == type(ex1).args
        assert ex0.args == ex1.args

The issue seems to be UnicodeEncodeError_init() at https://github.com/python/cpython/blob/v3.8.1/Objects/exceptions.c#L1895 and also UnicodeDecodeError_init().

The BaseException is initialized, but then Unicode*Error_init() tries to reparse the arguments and does not tolerate extra values.

This because BaseException.__reduce__ return a tuple (class,args).

----------
components: Interpreter Core
files: test_unicode_error_unpickle.py
messages: 362648
nosy: João Eiras
priority: normal
severity: normal
status: open
title: UnicodeError becomes unpicklable if data is appended to args
type: behavior
versions: Python 3.8
Added file: https://bugs.python.org/file48914/test_unicode_error_unpickle.py

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue39750>
_______________________________________


More information about the New-bugs-announce mailing list