Skip to content

Commit e921463

Browse files
Boxiang Sunnavytux
Boxiang Sun
authored andcommitted
gpython: Support unbuffered mode option -u
-------- kirr: Redo the test via checking buffering behaviour without relying on timings. Original Boxiang's test can be seen here: https://lab.nexedi.com/Daetalus/pygolang/-/commit/307b9ce2 /reviewed-by @kirr /reviewed-on https://lab.nexedi.com/nexedi/pygolang/-/merge_requests/33
1 parent 6aec478 commit e921463

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

gpython/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from __future__ import print_function, absolute_import
4242

4343

44-
_pyopt = "c:Eim:OvVW:X:"
44+
_pyopt = "c:Eim:OuvVW:X:"
4545
_pyopt_long = ('version',)
4646

4747
# pymain mimics `python ...`
@@ -117,6 +117,7 @@ def pymain(argv, init=None):
117117
'-O', # optimize
118118
'-v', # trace import statements
119119
'-X', # set implementation-specific option
120+
'-u', # unbuffered mode
120121
):
121122

122123
# but keep `-X gpython.*` in user part of argv in case of reexec

gpython/gpython_test.py

+19
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,13 @@ def test_pymain_X():
353353
check_gpy_vs_py(['-X', 'faulthandler', 'testprog/print_faulthandler.py'], cwd=here)
354354

355355

356+
# pymain -u
357+
@gpython_only
358+
def test_pymain_u():
359+
_check_gpy_vs_py([ 'testprog/print_stdio_bufmode.py'], cwd=here)
360+
_check_gpy_vs_py(['-u', 'testprog/print_stdio_bufmode.py'], cwd=here)
361+
362+
356363
# pymain -v
357364
@gpython_only
358365
def test_pymain_v():
@@ -478,3 +485,15 @@ def check_gpy_vs_py(argv, postprocessf=None, **kw):
478485
postprocessf(gpyoutv, stdpyoutv)
479486

480487
assert gpyoutv == stdpyoutv
488+
489+
# _check_gpy_vs_py verifies that gpython stdout/stderr match underlying python stdout/stderr.
490+
def _check_gpy_vs_py(argv, **kw):
491+
kw = kw.copy()
492+
kw['stdout'] = PIPE
493+
kw['stderr'] = PIPE
494+
gpyret, gpyout, gpyerr = _pyrun(argv, **kw)
495+
stdret, stdout, stderr = _pyrun(argv, pyexe=sys._gpy_underlying_executable, **kw)
496+
497+
assert gpyout == stdout
498+
assert gpyerr == stderr
499+
assert (gpyret, stdret) == (0, 0)
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright (C) 2025 Nexedi SA and Contributors.
3+
# Kirill Smelkov <[email protected]>
4+
#
5+
# This program is free software: you can Use, Study, Modify and Redistribute
6+
# it under the terms of the GNU General Public License version 3, or (at your
7+
# option) any later version, as published by the Free Software Foundation.
8+
#
9+
# You can also Link and Combine this program with other software covered by
10+
# the terms of any of the Free Software licenses or any of the Open Source
11+
# Initiative approved licenses and Convey the resulting work. Corresponding
12+
# source of such a combination shall include the source code for all other
13+
# software used.
14+
#
15+
# This program is distributed WITHOUT ANY WARRANTY; without even the implied
16+
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17+
#
18+
# See COPYING file for full licensing terms.
19+
# See https://www.nexedi.com/licensing for rationale and options.
20+
"""Program print_stdio_bufmode prints information about stdout/stderr buffering mode."""
21+
22+
from __future__ import print_function, absolute_import
23+
24+
import sys, os
25+
26+
27+
def main():
28+
null = os.open(os.devnull, os.O_WRONLY)
29+
30+
def check(subj, ioobj):
31+
ioobj.write('%s: unbuffered if you see the next line; buffered otherwise\n' % subj)
32+
ioobj.flush()
33+
ioobj.write('%s: unbuffered' % subj) # NOTE: no \n to avoid flush even on line-bufferring
34+
os.close(ioobj.fileno())
35+
os.dup2(null, ioobj.fileno()) # not to hit an error when ioobj is closed at the end
36+
37+
check('stdout', sys.stdout)
38+
check('stderr', sys.stderr)
39+
40+
41+
if __name__ == '__main__':
42+
main()

0 commit comments

Comments
 (0)