Commit 895862aa authored by Victor Stinner's avatar Victor Stinner Committed by Łukasz Langa

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.
parent c5a20715
...@@ -369,7 +369,9 @@ class EnsurePipTest(BaseTest): ...@@ -369,7 +369,9 @@ class EnsurePipTest(BaseTest):
self.fail(msg.format(exc, details)) self.fail(msg.format(exc, details))
# Ensure pip is available in the virtual environment # Ensure pip is available in the virtual environment
envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe) envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe)
cmd = [envpy, '-Im', 'pip', '--version'] # Ignore DeprecationWarning since pip code is not part of Python
cmd = [envpy, '-W', 'ignore::DeprecationWarning', '-I',
'-m', 'pip', '--version']
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
out, err = p.communicate() out, err = p.communicate()
...@@ -386,7 +388,8 @@ class EnsurePipTest(BaseTest): ...@@ -386,7 +388,8 @@ class EnsurePipTest(BaseTest):
# http://bugs.python.org/issue19728 # http://bugs.python.org/issue19728
# Check the private uninstall command provided for the Windows # Check the private uninstall command provided for the Windows
# installers works (at least in a virtual environment) # installers works (at least in a virtual environment)
cmd = [envpy, '-Im', 'ensurepip._uninstall'] cmd = [envpy, '-W', 'ignore::DeprecationWarning', '-I',
'-m', 'ensurepip._uninstall']
with EnvironmentVarGuard() as envvars: with EnvironmentVarGuard() as envvars:
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE) stderr=subprocess.PIPE)
......
...@@ -508,10 +508,13 @@ except ImportError: ...@@ -508,10 +508,13 @@ except ImportError:
# Module initialization # Module initialization
_processoptions(sys.warnoptions) _processoptions(sys.warnoptions)
if not _warnings_defaults: if not _warnings_defaults:
silence = [ImportWarning, PendingDeprecationWarning] py_debug = hasattr(sys, 'gettotalrefcount')
silence.append(DeprecationWarning) if not py_debug:
for cls in silence: silence = [ImportWarning, PendingDeprecationWarning]
simplefilter("ignore", category=cls) silence.append(DeprecationWarning)
for cls in silence:
simplefilter("ignore", category=cls)
bytes_warning = sys.flags.bytes_warning bytes_warning = sys.flags.bytes_warning
if bytes_warning > 1: if bytes_warning > 1:
bytes_action = "error" bytes_action = "error"
...@@ -520,8 +523,9 @@ if not _warnings_defaults: ...@@ -520,8 +523,9 @@ if not _warnings_defaults:
else: else:
bytes_action = "ignore" bytes_action = "ignore"
simplefilter(bytes_action, category=BytesWarning, append=1) simplefilter(bytes_action, category=BytesWarning, append=1)
# resource usage warnings are enabled by default in pydebug mode # resource usage warnings are enabled by default in pydebug mode
if hasattr(sys, 'gettotalrefcount'): if py_debug:
resource_action = "always" resource_action = "always"
else: else:
resource_action = "ignore" resource_action = "ignore"
......
warnings: When Python is build is debug mode (``Py_DEBUG``),
:exc:`DeprecationWarning`, :exc:`PendingDeprecationWarning` and
:exc:`ImportWarning` warnings are now displayed by default.
...@@ -1196,7 +1196,11 @@ create_filter(PyObject *category, const char *action) ...@@ -1196,7 +1196,11 @@ create_filter(PyObject *category, const char *action)
static PyObject * static PyObject *
init_filters(void) init_filters(void)
{ {
#ifndef Py_DEBUG
PyObject *filters = PyList_New(5); PyObject *filters = PyList_New(5);
#else
PyObject *filters = PyList_New(2);
#endif
unsigned int pos = 0; /* Post-incremented in each use. */ unsigned int pos = 0; /* Post-incremented in each use. */
unsigned int x; unsigned int x;
const char *bytes_action, *resource_action; const char *bytes_action, *resource_action;
...@@ -1204,12 +1208,15 @@ init_filters(void) ...@@ -1204,12 +1208,15 @@ init_filters(void)
if (filters == NULL) if (filters == NULL)
return NULL; return NULL;
#ifndef Py_DEBUG
PyList_SET_ITEM(filters, pos++, PyList_SET_ITEM(filters, pos++,
create_filter(PyExc_DeprecationWarning, "ignore")); create_filter(PyExc_DeprecationWarning, "ignore"));
PyList_SET_ITEM(filters, pos++, PyList_SET_ITEM(filters, pos++,
create_filter(PyExc_PendingDeprecationWarning, "ignore")); create_filter(PyExc_PendingDeprecationWarning, "ignore"));
PyList_SET_ITEM(filters, pos++, PyList_SET_ITEM(filters, pos++,
create_filter(PyExc_ImportWarning, "ignore")); create_filter(PyExc_ImportWarning, "ignore"));
#endif
if (Py_BytesWarningFlag > 1) if (Py_BytesWarningFlag > 1)
bytes_action = "error"; bytes_action = "error";
else if (Py_BytesWarningFlag) else if (Py_BytesWarningFlag)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment