Skip to content

Commit

Permalink
Attempt to fix InvocationError for command run_tests.py exited
Browse files Browse the repository at this point in the history
with code 120
  • Loading branch information
evandrocoan committed Jan 25, 2019
1 parent 3c1e226 commit 3d008a9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
2 changes: 0 additions & 2 deletions tests/run_tests.py
Expand Up @@ -61,5 +61,3 @@
print( "results: %s" % results )
print( "results.wasSuccessful: %s" % results.wasSuccessful() )
sys.exit( not results.wasSuccessful() )


52 changes: 43 additions & 9 deletions tests/testing/std_capture.py
Expand Up @@ -49,6 +49,7 @@ class TeeNoFile(object):
How do I duplicate sys.stdout to a log file in python?
https://stackoverflow.com/questions/616645/how-do-i-duplicate-sys-stdout-to-a-log-file-in-python
"""
__closed = False

def __init__(self, stdout=False):
self._contents = []
Expand All @@ -65,19 +66,47 @@ def __init__(self, stdout=False):
# sys.stdout.write( "inspect.getmro(sys.stderr): %s\n" % str( inspect.getmro( type( sys.stderr ) ) ) )
# sys.stdout.write( "inspect.getmro(self.stderr): %s\n" % str( inspect.getmro( type( self._std_original ) ) ) )

@property
def closed(self):
"""
@return `True` if the file has been closed.
"""
return self.__closed

def __del__(self):
"""
The try/except block is in case this is called at program exit time, when it's possible
that globals have already been deleted, and then the close() call might fail. Since
there's nothing we can do about such failures and they annoy the end users, we suppress
the traceback.
https://github.com/python/cpython/blob/1fd06f1eca80dcbf3a916133919482a8327f3da4/Lib/_pyio.py#L380
python Exception AttributeError: “'NoneType' object has no attribute 'var'”
https://stackoverflow.com/questions/9750308/python-exception-attributeerror-nonetype-object-has-no-attribute-var
"""
self.close()

try:
self.close()

except:
pass

def clear(self, log):
log.clear()
del self._contents[:]

def flush(self):
self._std_original.flush()

try:
self._std_original.flush()

except AttributeError:

if self.closed:
pass

else:
raise

def write(self, *args, **kwargs):
# self._std_original.write( " 111111 %s" % self._std_original.write + str( args ), **kwargs )
Expand Down Expand Up @@ -112,13 +141,18 @@ def _process_contents(self, date_regex, output):
def close(self):

# On shutdown `__del__`, the sys module can be already set to None.
if sys and self._std_original:
if sys and self._std_original and not self.__closed:

if self.stdout_type:
sys.stdout = self._std_original
self._std_original = None
try:
self.flush()

else:
sys.stderr = self._std_original
self._std_original = None
finally:
self.__closed = True

if self.stdout_type:
sys.stdout = self._std_original

else:
sys.stderr = self._std_original

self._std_original = None

0 comments on commit 3d008a9

Please sign in to comment.