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