From a241970fb3f7cb86508f9e0a57356d0d058f1e76 Mon Sep 17 00:00:00 2001 From: Lucas Fernando Nunes Date: Mon, 11 Jan 2021 21:52:53 -0300 Subject: [PATCH] deepupdate change recursion to loop Solves RecursionErrors exceptions/crashes caused by bloated memory use or high recursion depth when calling deepupdate(). *The same issues can occur on copy.deepcopy() calls. --- src/apispec/utils.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/apispec/utils.py b/src/apispec/utils.py index a778e0c0..9581adeb 100644 --- a/src/apispec/utils.py +++ b/src/apispec/utils.py @@ -158,15 +158,19 @@ def dedent(content): return content.strip() -# http://stackoverflow.com/a/8310229 +# http://stackoverflow.com/a/8310229 #+ recursion -> loop def deepupdate(original, update): - """Recursively update a dict. + """Iteratively update a dict. Subdict's won't be overwritten but also updated. """ - for key, value in original.items(): - if key not in update: - update[key] = value - elif isinstance(value, dict): - deepupdate(value, update[key]) - return update + result = update + stack = [(original, update)] + while stack: + original, update = stack.pop() + for key, value in original.items(): + if key not in update: + update[key] = value + elif isinstance(value, dict): + stack.append((value, update[key])) + return result