Title: ElementTree.Element.__deepcopy__() raises a SystemError in case of a bad memo
Type: behavior Stage: resolved
Components: XML Versions: Python 3.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Oren Milman, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2017-09-12 12:04 by Oren Milman, last changed 2017-09-12 14:40 by serhiy.storchaka. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 3512 merged Oren Milman, 2017-09-12 12:25
Messages (3)
msg301953 - (view) Author: Oren Milman (Oren Milman) * Date: 2017-09-12 12:04
The following code causes ElementTree.Element.__deepcopy__() to raise a

class BadMemo:
    def get(*args):
        return None

import xml.etree.ElementTree

this is because _elementtree_Element___deepcopy__() (in Modules/_elementtree.c)
assumes that memo is a dictionary, and passes it to PyDict_SetItem(), which
raises the SystemError.
msg301955 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-12 12:24
I suppose the Python implementation supports arbitrary mapping, not just dict. We could use general mapping API in the C implementation, but I think it isn't worth. __deepcopy__ is used internally by copy.deepcopy(), which always passes a dict. I think it isn't worth to backport a fix, and it doesn't need tests. The behavior of calling __deepcopy__ with non-dict is implementation detail. But it shouldn't crash or raise SystemError.
msg301963 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-12 14:39
New changeset d056818ed2ade6d28190a375d7183f4aef9caa55 by Serhiy Storchaka (Oren Milman) in branch 'master':
bpo-31428: Prevent raising a SystemError in case the memo arg of ElementTree.Element.__deepcopy__() isn't a dictionary. (#3512)
Date User Action Args
2017-09-12 14:40:01serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-09-12 14:39:19serhiy.storchakasetmessages: + msg301963
2017-09-12 12:25:35Oren Milmansetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request3507
2017-09-12 12:24:00serhiy.storchakasetmessages: + msg301955
versions: - Python 2.7, Python 3.6
2017-09-12 12:15:10serhiy.storchakasetnosy: + serhiy.storchaka
stage: needs patch

versions: + Python 2.7, Python 3.6
2017-09-12 12:04:08Oren Milmancreate