]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Fix flush for sys.stderr
authorTom Tromey <tromey@adacore.com>
Mon, 15 Aug 2022 18:45:43 +0000 (12:45 -0600)
committerTom Tromey <tromey@adacore.com>
Tue, 30 Aug 2022 17:49:48 +0000 (11:49 -0600)
GDB overwrites Python's sys.stdout and sys.stderr, but does not
properly implement the 'flush' method -- it only ever will flush
stdout.  This patch fixes the bug.  I couldn't find a straightforward
way to write a test for this.

gdb/python/lib/gdb/__init__.py
gdb/testsuite/gdb.python/python.exp

index 5b10e3e23813ee335c43299f1ac1f3a7940f884c..191915e4f3bae65e90d4e65042a29b264e6d52b7 100644 (file)
@@ -39,6 +39,9 @@ class _GdbFile(object):
     encoding = "UTF-8"
     errors = "strict"
 
+    def __init__(self, stream):
+        self.stream = stream
+
     def close(self):
         # Do nothing.
         return None
@@ -51,23 +54,15 @@ class _GdbFile(object):
             self.write(line)
 
     def flush(self):
-        flush()
-
+        flush(stream=self.stream)
 
-class _GdbOutputFile(_GdbFile):
     def write(self, s):
-        write(s, stream=STDOUT)
-
+        write(s, stream=self.stream)
 
-sys.stdout = _GdbOutputFile()
-
-
-class _GdbOutputErrorFile(_GdbFile):
-    def write(self, s):
-        write(s, stream=STDERR)
 
+sys.stdout = _GdbFile(STDOUT)
 
-sys.stderr = _GdbOutputErrorFile()
+sys.stderr = _GdbFile(STDERR)
 
 # Default prompt hook does nothing.
 prompt_hook = None
index 8c0da6daa26949d9ff8dd91cb4c386ba47ea764b..48ff07e91e552dd6ff75c36e7af78636dcba57b4 100644 (file)
@@ -297,8 +297,8 @@ with_test_prefix "test decode_line" {
 }
 
 # gdb.write
-gdb_test "python print (sys.stderr)" ".*gdb._GdbOutputErrorFile (instance|object) at.*" "test stderr location"
-gdb_test "python print (sys.stdout)" ".*gdb._GdbOutputFile (instance|object) at.*" "test stdout location"
+gdb_test "python print (sys.stderr)" ".*gdb._GdbFile (instance|object) at.*" "test stderr location"
+gdb_test "python print (sys.stdout)" ".*gdb._GdbFile (instance|object) at.*" "test stdout location"
 gdb_test "python gdb.write(\"Foo\\n\")" "Foo" "test default write"
 gdb_test "python gdb.write(\"Error stream\\n\", stream=gdb.STDERR)" "Error stream" "test stderr write"
 gdb_test "python gdb.write(\"Normal stream\\n\", stream=gdb.STDOUT)" "Normal stream" "test stdout write"