Fix lambda stdout/stderr mocking (#851)

Originally, the code was setting sys.stdout and sys.stderr back to the
original, official forms, but this breaks idioms like mocking stdout to
capture printing output for tests. So instead, we will reset sys.stdout
and sys.stderr to what they were before running the lambda function, so
that in case someone is mocking stdout or stderr, their tests won't
break.
This commit is contained in:
Andrew Garrett 2017-03-04 20:01:50 -08:00 committed by Steve Pulec
parent f6465df630
commit e7ea6b350c

View File

@ -135,6 +135,8 @@ class LambdaFunction(object):
print("Exception %s", ex)
try:
original_stdout = sys.stdout
original_stderr = sys.stderr
codeOut = StringIO()
codeErr = StringIO()
sys.stdout = codeOut
@ -150,8 +152,8 @@ class LambdaFunction(object):
finally:
codeErr.close()
codeOut.close()
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
sys.stdout = original_stdout
sys.stderr = original_stderr
return self.convert(result)
def invoke(self, request, headers):