Change in osmo-gsm-tester[master]: do not redirect_stdout in test scripts

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

neels gerrit-no-reply at lists.osmocom.org
Thu Dec 3 23:56:23 UTC 2020


neels has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-gsm-tester/+/21513 )


Change subject: do not redirect_stdout in test scripts
......................................................................

do not redirect_stdout in test scripts

In a test, I called print() on a multi-line string and saw the log
showing each line 0.2 seconds apart. redirect.stdout seems to be pretty
inefficient.

Instead, put a print() function into the testenv, to directly call log()
on the strings passed to print().

The initial idea for redirect_stdout was that we could print() in any
deeper functions called from a test script. But we have no such nested
print() anywhere, only in test scripts themselves.

Change-Id: I0972c66b9165bd7f2b0b387e0335172849199193
---
M src/osmo_gsm_tester/core/log.py
M src/osmo_gsm_tester/core/test.py
M src/osmo_gsm_tester/testenv.py
3 files changed, 8 insertions(+), 38 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-gsm-tester refs/changes/13/21513/1

diff --git a/src/osmo_gsm_tester/core/log.py b/src/osmo_gsm_tester/core/log.py
index b8182f5..81b430c 100644
--- a/src/osmo_gsm_tester/core/log.py
+++ b/src/osmo_gsm_tester/core/log.py
@@ -460,9 +460,6 @@
     def _set_log_category(self, category):
         self._log_category = category
 
-    def redirect_stdout(self):
-        return contextlib.redirect_stdout(SafeRedirectStdout(self))
-
     def ancestry(self):
         origins = []
         n = 10
@@ -495,37 +492,6 @@
         '''same as log.err() but passes this object to skip looking up an origin'''
         err(*messages, _origin=self, _src=_src, **named_items)
 
-class SafeRedirectStdout:
-    '''
-    To be able to use 'print' in test scripts, this is used to redirect stdout
-    to a test class' log() function. However, it turns out doing that breaks
-    python debugger sessions -- it uses extended features of stdout, and will
-    fail dismally if it finds this wrapper in sys.stdout. Luckily, overriding
-    __getattr__() to return the original sys.__stdout__ attributes for anything
-    else than write() makes the debugger session work nicely again!
-    '''
-    _log_line_buf = None
-
-    def __init__(self, origin):
-        self.origin = origin
-
-    def write(self, message):
-        lines = message.splitlines()
-        if not lines:
-            return
-        if self._log_line_buf:
-            lines[0] = self._log_line_buf + lines[0]
-            self._log_line_buf = None
-        if not message.endswith('\n'):
-            self._log_line_buf = lines[-1]
-            lines = lines[:-1]
-        for line in lines:
-            _log(messages=(line,),
-                 origin=self.origin, level=L_LOG, src=2)
-
-    def __getattr__(self, name):
-        return sys.__stdout__.__getattribute__(name)
-
 def trace(exc_info=None, origin=None):
     if exc_info is None:
         exc_info = sys.exc_info()
diff --git a/src/osmo_gsm_tester/core/test.py b/src/osmo_gsm_tester/core/test.py
index fd3ac04..9b899e2 100644
--- a/src/osmo_gsm_tester/core/test.py
+++ b/src/osmo_gsm_tester/core/test.py
@@ -71,9 +71,8 @@
             self.status = Test.UNKNOWN
             self.start_timestamp = time.time()
             testenv_obj = testenv.setup(self.suite_run, self)
-            with self.redirect_stdout():
-                util.run_python_file('%s.%s' % (self.suite_run.definition.name(), self.basename),
-                                     self.path)
+            util.run_python_file('%s.%s' % (self.suite_run.definition.name(), self.basename),
+                                 self.path)
             if self.status == Test.UNKNOWN:
                  self.set_pass()
         except Exception as e:
diff --git a/src/osmo_gsm_tester/testenv.py b/src/osmo_gsm_tester/testenv.py
index a74891d..651183c 100644
--- a/src/osmo_gsm_tester/testenv.py
+++ b/src/osmo_gsm_tester/testenv.py
@@ -43,6 +43,7 @@
 Sms = None
 process = None
 tenv = None
+print = None
 
 class Timeout(Exception):
     pass
@@ -349,12 +350,15 @@
         self.log('using BVCI', bvci)
         return bvci
 
+    def print(self, *messages, **named_items):
+        self.log(*messages, **named_items)
+
 
 def setup(suite_run, _test):
     from .core.event_loop import MainLoop
     from .obj.sms import Sms as Sms_class
 
-    global test, log, dbg, err, wait, wait_no_raise, sleep, poll, prompt, Sms, process, tenv
+    global test, log, dbg, err, wait, wait_no_raise, sleep, poll, prompt, Sms, process, tenv, print
 
     test = _test
     log = test.log
@@ -368,6 +372,7 @@
     Sms = Sms_class
     process = process_module
     prompt = tenv.prompt
+    print = tenv.print
     return tenv
 
 # vim: expandtab tabstop=4 shiftwidth=4

-- 
To view, visit https://gerrit.osmocom.org/c/osmo-gsm-tester/+/21513
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-gsm-tester
Gerrit-Branch: master
Gerrit-Change-Id: I0972c66b9165bd7f2b0b387e0335172849199193
Gerrit-Change-Number: 21513
Gerrit-PatchSet: 1
Gerrit-Owner: neels <nhofmeyr at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20201203/fd02f15a/attachment.htm>


More information about the gerrit-log mailing list