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

Skip to content

Commit 895862a

Browse files
vstinnerambv
authored andcommitted
bpo-32088: Display Deprecation in debug mode (#4474)
When Python is build is debug mode (Py_DEBUG), DeprecationWarning, PendingDeprecationWarning and ImportWarning warnings are now displayed by default. test_venv: run "-m pip" and "-m ensurepip._uninstall" with -W ignore::DeprecationWarning since pip code is not part of Python.
1 parent c5a2071 commit 895862a

4 files changed

Lines changed: 24 additions & 7 deletions

File tree

Lib/test/test_venv.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,9 @@ def do_test_with_pip(self, system_site_packages):
369369
self.fail(msg.format(exc, details))
370370
# Ensure pip is available in the virtual environment
371371
envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe)
372-
cmd = [envpy, '-Im', 'pip', '--version']
372+
# Ignore DeprecationWarning since pip code is not part of Python
373+
cmd = [envpy, '-W', 'ignore::DeprecationWarning', '-I',
374+
'-m', 'pip', '--version']
373375
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
374376
stderr=subprocess.PIPE)
375377
out, err = p.communicate()
@@ -386,7 +388,8 @@ def do_test_with_pip(self, system_site_packages):
386388
# http://bugs.python.org/issue19728
387389
# Check the private uninstall command provided for the Windows
388390
# installers works (at least in a virtual environment)
389-
cmd = [envpy, '-Im', 'ensurepip._uninstall']
391+
cmd = [envpy, '-W', 'ignore::DeprecationWarning', '-I',
392+
'-m', 'ensurepip._uninstall']
390393
with EnvironmentVarGuard() as envvars:
391394
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
392395
stderr=subprocess.PIPE)

Lib/warnings.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -508,10 +508,13 @@ def _filters_mutated():
508508
# Module initialization
509509
_processoptions(sys.warnoptions)
510510
if not _warnings_defaults:
511-
silence = [ImportWarning, PendingDeprecationWarning]
512-
silence.append(DeprecationWarning)
513-
for cls in silence:
514-
simplefilter("ignore", category=cls)
511+
py_debug = hasattr(sys, 'gettotalrefcount')
512+
if not py_debug:
513+
silence = [ImportWarning, PendingDeprecationWarning]
514+
silence.append(DeprecationWarning)
515+
for cls in silence:
516+
simplefilter("ignore", category=cls)
517+
515518
bytes_warning = sys.flags.bytes_warning
516519
if bytes_warning > 1:
517520
bytes_action = "error"
@@ -520,8 +523,9 @@ def _filters_mutated():
520523
else:
521524
bytes_action = "ignore"
522525
simplefilter(bytes_action, category=BytesWarning, append=1)
526+
523527
# resource usage warnings are enabled by default in pydebug mode
524-
if hasattr(sys, 'gettotalrefcount'):
528+
if py_debug:
525529
resource_action = "always"
526530
else:
527531
resource_action = "ignore"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
warnings: When Python is build is debug mode (``Py_DEBUG``),
2+
:exc:`DeprecationWarning`, :exc:`PendingDeprecationWarning` and
3+
:exc:`ImportWarning` warnings are now displayed by default.

Python/_warnings.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,20 +1196,27 @@ create_filter(PyObject *category, const char *action)
11961196
static PyObject *
11971197
init_filters(void)
11981198
{
1199+
#ifndef Py_DEBUG
11991200
PyObject *filters = PyList_New(5);
1201+
#else
1202+
PyObject *filters = PyList_New(2);
1203+
#endif
12001204
unsigned int pos = 0; /* Post-incremented in each use. */
12011205
unsigned int x;
12021206
const char *bytes_action, *resource_action;
12031207

12041208
if (filters == NULL)
12051209
return NULL;
12061210

1211+
#ifndef Py_DEBUG
12071212
PyList_SET_ITEM(filters, pos++,
12081213
create_filter(PyExc_DeprecationWarning, "ignore"));
12091214
PyList_SET_ITEM(filters, pos++,
12101215
create_filter(PyExc_PendingDeprecationWarning, "ignore"));
12111216
PyList_SET_ITEM(filters, pos++,
12121217
create_filter(PyExc_ImportWarning, "ignore"));
1218+
#endif
1219+
12131220
if (Py_BytesWarningFlag > 1)
12141221
bytes_action = "error";
12151222
else if (Py_BytesWarningFlag)

0 commit comments

Comments
 (0)