subprocess: Fix patched functions with optional arguments
Since [1] we monkey patch subprocess module and it uncovered one issue -
the previous patched_function implementation would produce functions
with all optional arguments lacking their default values which made them
required. This is an issue on Python 3 where there's at least one
optional explicit argument in at least one of the functions
(check_call), on Python 2 the function signature has *args and **kwargs
so it wasn't an issue.
This patch is contributed by Smarkets Limited.
[1] 614a20462a
This commit is contained in:
@@ -119,7 +119,11 @@ class Popen(subprocess_orig.Popen):
|
|||||||
# Borrow subprocess.call() and check_call(), but patch them so they reference
|
# Borrow subprocess.call() and check_call(), but patch them so they reference
|
||||||
# OUR Popen class rather than subprocess.Popen.
|
# OUR Popen class rather than subprocess.Popen.
|
||||||
def patched_function(function):
|
def patched_function(function):
|
||||||
return FunctionType(six.get_function_code(function), globals())
|
new_function = FunctionType(six.get_function_code(function), globals())
|
||||||
|
if six.PY3:
|
||||||
|
new_function.__kwdefaults__ = function.__kwdefaults__
|
||||||
|
new_function.__defaults__ = function.__defaults__
|
||||||
|
return new_function
|
||||||
|
|
||||||
|
|
||||||
call = patched_function(subprocess_orig.call)
|
call = patched_function(subprocess_orig.call)
|
||||||
|
|||||||
@@ -83,3 +83,13 @@ def test_patched_communicate_290():
|
|||||||
# with AttributeError module `select` has no `poll` on Linux
|
# with AttributeError module `select` has no `poll` on Linux
|
||||||
# unpatched methods are removed for safety reasons in commit f63165c0e3
|
# unpatched methods are removed for safety reasons in commit f63165c0e3
|
||||||
tests.run_isolated('subprocess_patched_communicate.py')
|
tests.run_isolated('subprocess_patched_communicate.py')
|
||||||
|
|
||||||
|
|
||||||
|
def test_check_call_without_timeout_works():
|
||||||
|
# There was a regression that'd result in the following exception:
|
||||||
|
# TypeError: check_call() missing 1 required keyword-only argument: 'timeout'
|
||||||
|
subprocess.check_call(
|
||||||
|
['ls'],
|
||||||
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.PIPE,
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user