Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 53b7d4e

Browse files
authored
bpo-34170: Add _PyCoreConfig.bytes_warning (GH-8447)
Add more fields to _PyCoreConfig: * _check_hash_pycs_mode * bytes_warning * debug * inspect * interactive * legacy_windows_fs_encoding * legacy_windows_stdio * optimization_level * quiet * unbuffered_stdio * user_site_directory * verbose * write_bytecode Changes: * Remove pymain_get_global_config() and pymain_set_global_config() which became useless. These functions have been replaced by _PyCoreConfig_GetGlobalConfig() and _PyCoreConfig_SetGlobalConfig(). * sys.flags.dont_write_bytecode value is now restricted to 1 even if -B option is specified multiple times on the command line. * PyThreadState_Clear() now uses the config from the current interpreter rather than using global Py_VerboseFlag
1 parent 95d34c2 commit 53b7d4e

7 files changed

Lines changed: 294 additions & 138 deletions

File tree

Include/pystate.h

Lines changed: 142 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,147 @@ typedef struct {
8787
these manipulations if site is explicitly imported later (call
8888
site.main() if you want them to be triggered).
8989
90-
Set to 0 by the -S command line option. If set to -1 (default), set to
91-
the negative value of Py_NoSiteFlag. */
90+
Set to 0 by the -S command line option. If set to -1 (default), it is
91+
set to !Py_NoSiteFlag. */
9292
int site_import;
9393

94+
/* Bytes warnings:
95+
96+
* If equal to 1, issue a warning when comparing bytes or bytearray with
97+
str or bytes with int.
98+
* If equal or greater to 2, issue an error.
99+
100+
Incremented by the -b command line option. If set to -1 (default), inherit
101+
Py_BytesWarningFlag value. */
102+
int bytes_warning;
103+
104+
/* If greater than 0, enable inspect: when a script is passed as first
105+
argument or the -c option is used, enter interactive mode after
106+
executing the script or the command, even when sys.stdin does not appear
107+
to be a terminal.
108+
109+
Incremented by the -i command line option. Set to 1 if the PYTHONINSPECT
110+
environment variable is non-empty. If set to -1 (default), inherit
111+
Py_InspectFlag value. */
112+
int inspect;
113+
114+
/* If greater than 0: enable the interactive mode (REPL).
115+
116+
Incremented by the -i command line option. If set to -1 (default),
117+
inherit Py_InteractiveFlag value. */
118+
int interactive;
119+
120+
/* Optimization level.
121+
122+
Incremented by the -O command line option. Set by the PYTHONOPTIMIZE
123+
environment variable. If set to -1 (default), inherit Py_OptimizeFlag
124+
value. */
125+
int optimization_level;
126+
127+
/* If greater than 0, enable the debug mode: turn on parser debugging
128+
output (for expert only, depending on compilation options).
129+
130+
Incremented by the -d command line option. Set by the PYTHONDEBUG
131+
environment variable. If set to -1 (default), inherit Py_DebugFlag
132+
value. */
133+
int debug;
134+
135+
/* If equal to 0, Python won't try to write ``.pyc`` files on the
136+
import of source modules.
137+
138+
Set to 0 by the -B command line option and the PYTHONDONTWRITEBYTECODE
139+
environment variable. If set to -1 (default), it is set to
140+
!Py_DontWriteBytecodeFlag. */
141+
int write_bytecode;
142+
143+
/* If greater than 0, enable the verbose mode: print a message each time a
144+
module is initialized, showing the place (filename or built-in module)
145+
from which it is loaded.
146+
147+
If greater or equal to 2, print a message for each file that is checked
148+
for when searching for a module. Also provides information on module
149+
cleanup at exit.
150+
151+
Incremented by the -v option. Set by the PYTHONVERBOSE environment
152+
variable. If set to -1 (default), inherit Py_VerboseFlag value. */
153+
int verbose;
154+
155+
/* If greater than 0, enable the quiet mode: Don't display the copyright
156+
and version messages even in interactive mode.
157+
158+
Incremented by the -q option. If set to -1 (default), inherit
159+
Py_QuietFlag value. */
160+
int quiet;
161+
162+
/* If greater than 0, don't add the user site-packages directory to
163+
sys.path.
164+
165+
Set to 0 by the -s and -I command line options , and the PYTHONNOUSERSITE
166+
environment variable. If set to -1 (default), it is set to
167+
!Py_NoUserSiteDirectory. */
168+
int user_site_directory;
169+
170+
/* If greater than 0, enable unbuffered mode: force the stdout and stderr
171+
streams to be unbuffered.
172+
173+
Set to 1 by the -u option. Set by the PYTHONUNBUFFERED environment
174+
variable. If set to -1 (default), inherit Py_UnbufferedStdioFlag
175+
value. */
176+
int unbuffered_stdio;
177+
178+
#ifdef MS_WINDOWS
179+
/* If greater than 1, use the "mbcs" encoding instead of the UTF-8
180+
encoding for the filesystem encoding.
181+
182+
Set to 1 if the PYTHONLEGACYWINDOWSFSENCODING environment variable is
183+
set to a non-empty string. If set to -1 (default), inherit
184+
Py_LegacyWindowsFSEncodingFlag value.
185+
186+
See PEP 529 for more details. */
187+
int legacy_windows_fs_encoding;
188+
189+
/* If greater than zero, use io.FileIO instead of WindowsConsoleIO for sys
190+
standard streams.
191+
192+
Set to 1 if the PYTHONLEGACYWINDOWSSTDIO environment variable is set to
193+
a non-empty string. If set to -1 (default), inherit
194+
Py_LegacyWindowsStdioFlag value.
195+
196+
See PEP 528 for more details. */
197+
int legacy_windows_stdio;
198+
#endif
199+
94200
/* --- Private fields -------- */
95201

96202
/* Install importlib? If set to 0, importlib is not initialized at all.
97203
Needed by freeze_importlib: see install_importlib argument of
98204
_Py_InitializeEx_Private(). */
99205
int _install_importlib;
206+
207+
/* Value of the --check-hash-based-pycs configure option. Valid values:
208+
209+
- "default" means the 'check_source' flag in hash-based pycs
210+
determines invalidation
211+
- "always" causes the interpreter to hash the source file for
212+
invalidation regardless of value of 'check_source' bit
213+
- "never" causes the interpreter to always assume hash-based pycs are
214+
valid
215+
216+
Set by the --check-hash-based-pycs command line option.
217+
If set to NULL (default), inherit _Py_CheckHashBasedPycsMode value.
218+
219+
See PEP 552 "Deterministic pycs" for more details. */
220+
const char *_check_hash_pycs_mode;
100221
} _PyCoreConfig;
101222

223+
#ifdef MS_WINDOWS
224+
# define _PyCoreConfig_WINDOWS_INIT \
225+
.legacy_windows_fs_encoding = -1, \
226+
.legacy_windows_stdio = -1,
227+
#else
228+
# define _PyCoreConfig_WINDOWS_INIT
229+
#endif
230+
102231
#define _PyCoreConfig_INIT \
103232
(_PyCoreConfig){ \
104233
.install_signal_handlers = -1, \
@@ -110,6 +239,17 @@ typedef struct {
110239
.nmodule_search_path = -1, \
111240
.isolated = -1, \
112241
.site_import = -1, \
242+
.bytes_warning = -1, \
243+
.inspect = -1, \
244+
.interactive = -1, \
245+
.optimization_level = -1, \
246+
.debug= -1, \
247+
.write_bytecode = -1, \
248+
.verbose = -1, \
249+
.quiet = -1, \
250+
.user_site_directory = -1, \
251+
.unbuffered_stdio = -1, \
252+
_PyCoreConfig_WINDOWS_INIT \
113253
._install_importlib = 1}
114254
/* Note: _PyCoreConfig_INIT sets other fields to 0/NULL */
115255

Lib/test/test_cmd_line.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,13 +507,15 @@ def test_sys_flags_set(self):
507507
PYTHONDONTWRITEBYTECODE=value,
508508
PYTHONVERBOSE=value,
509509
)
510+
dont_write_bytecode = int(bool(value))
510511
code = (
511512
"import sys; "
512513
"sys.stderr.write(str(sys.flags)); "
513514
f"""sys.exit(not (
514515
sys.flags.debug == sys.flags.optimize ==
515-
sys.flags.dont_write_bytecode == sys.flags.verbose ==
516+
sys.flags.verbose ==
516517
{expected}
518+
and sys.flags.dont_write_bytecode == {dont_write_bytecode}
517519
))"""
518520
)
519521
with self.subTest(envar_value=value):

0 commit comments

Comments
 (0)