[issue23427] Python should expose command when invoked with -c

STINNER Victor report at bugs.python.org
Mon Jun 8 13:25:18 EDT 2020


STINNER Victor <vstinner at python.org> added the comment:

Example of sys.orig_argv usage to re-execute the Python process with different options:
---
import sys
import os
if not sys.flags.utf8_mode:
    # Force UTF-8 mode
    argv = sys.orig_argv.copy()
    argv[1:1] = ["-X", "utf8"]
    print(f"Re-execute to force UTF-8 mode! argv={argv}")
    os.execv(argv[0], argv)

print(f"Everybody loves UTF-8! utf8_mode={sys.flags.utf8_mode}")
---

Example coming from discussions on the PEP 597 :-)

Output:
---
$ ./python force_utf8_mode.py 
Re-execute to force UTF-8 mode! argv=['./python', '-X', 'utf8', 'force_utf8_mode.py']
Everybody loves UTF-8! utf8_mode=1
---

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue23427>
_______________________________________


More information about the Python-bugs-list mailing list