From 8925599dc1a7e2f572e1bf7478f0b99753bbc207 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Thu, 4 Oct 2018 18:40:28 +0900 Subject: [PATCH 1/3] bpo-34871: inspect: Don't pollute sys.modules --- Lib/inspect.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 5b7f526939b60ff..3c9d354d9f4f3e6 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1988,7 +1988,6 @@ def _signature_fromstr(cls, obj, s, skip_bound_arg=True): module = sys.modules.get(module_name, None) if module: module_dict = module.__dict__ - sys_module_dict = sys.modules def parse_name(node): assert isinstance(node, ast.arg) @@ -2001,7 +2000,7 @@ def wrap_value(s): value = eval(s, module_dict) except NameError: try: - value = eval(s, sys_module_dict) + value = eval(s, sys.modules.copy()) except NameError: raise RuntimeError() From 8a5c0ded5b5b7fad0799c174db1df15d663080f7 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Thu, 4 Oct 2018 18:46:58 +0900 Subject: [PATCH 2/3] Add NEWS entry --- .../next/Library/2018-10-04-18-46-54.bpo-34871.t3X-dB.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2018-10-04-18-46-54.bpo-34871.t3X-dB.rst diff --git a/Misc/NEWS.d/next/Library/2018-10-04-18-46-54.bpo-34871.t3X-dB.rst b/Misc/NEWS.d/next/Library/2018-10-04-18-46-54.bpo-34871.t3X-dB.rst new file mode 100644 index 000000000000000..8cff15671ce5366 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-10-04-18-46-54.bpo-34871.t3X-dB.rst @@ -0,0 +1,2 @@ +Fix inspect module polluted ``sys.modules`` when parsing +``__text_signature__`` of callable. From 67b5e0ba866fc70537b66a1c102e54881b19c6b7 Mon Sep 17 00:00:00 2001 From: Inada Naoki Date: Fri, 5 Oct 2018 01:29:56 +0900 Subject: [PATCH 3/3] Copy once --- Lib/inspect.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/inspect.py b/Lib/inspect.py index 3c9d354d9f4f3e6..857892bc8144c56 100644 --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -1988,6 +1988,7 @@ def _signature_fromstr(cls, obj, s, skip_bound_arg=True): module = sys.modules.get(module_name, None) if module: module_dict = module.__dict__ + sys_module_dict = sys.modules.copy() def parse_name(node): assert isinstance(node, ast.arg) @@ -2000,7 +2001,7 @@ def wrap_value(s): value = eval(s, module_dict) except NameError: try: - value = eval(s, sys.modules.copy()) + value = eval(s, sys_module_dict) except NameError: raise RuntimeError()