Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
bpo-32101: Add sys.flags.dev_mode flag
Rename also the "Developer mode" to the "Development mode".
  • Loading branch information
vstinner committed Nov 29, 2017
commit 4c3e538f19ab9678c3a3163d86cff13ade54f0d9
4 changes: 4 additions & 0 deletions Doc/library/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ always available.
:const:`bytes_warning` :option:`-b`
:const:`quiet` :option:`-q`
:const:`hash_randomization` :option:`-R`
:const:`dev_mode` :option:`-X` ``dev``
============================= =============================

.. versionchanged:: 3.2
Expand All @@ -345,6 +346,9 @@ always available.
.. versionchanged:: 3.3
Removed obsolete ``division_warning`` attribute.

.. versionchanged:: 3.7
Added ``dev_mode`` attribute for the new :option:`-X` ``dev`` flag.


.. data:: float_info

Expand Down
2 changes: 1 addition & 1 deletion Doc/using/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ Miscellaneous options
nested imports). Note that its output may be broken in multi-threaded
application. Typical usage is ``python3 -X importtime -c 'import
asyncio'``. See also :envvar:`PYTHONPROFILEIMPORTTIME`.
* ``-X dev``: enable CPython's "developer mode", introducing additional
* ``-X dev``: enable CPython's "development mode", introducing additional
runtime checks which are too expensive to be enabled by default. It should
not be more verbose than the default if the code is correct: new warnings
are only emitted when an issue is detected. Effect of the developer mode:
Expand Down
11 changes: 8 additions & 3 deletions Doc/whatsnew/3.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,10 @@ resolution on Linux and Windows.
PEP written and implemented by Victor Stinner


New Developer Mode: -X dev
--------------------------
New Development Mode: -X dev
----------------------------

Add a new "developer mode": ``-X dev`` command line option to enable debug
Add a new "development mode": ``-X dev`` command line option to enable debug
checks at runtime.

In short, ``python3 -X dev ...`` behaves as ``PYTHONMALLOC=debug python3 -W
Expand Down Expand Up @@ -371,6 +371,11 @@ string
expression pattern for braced placeholders and non-braced placeholders
separately. (Contributed by Barry Warsaw in :issue:`1198569`.)

sys
---

Added :attr:`sys.flags.dev_mode` flag for the new development mode.

time
----

Expand Down
2 changes: 1 addition & 1 deletion Lib/asyncio/coroutines.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def _is_debug_mode():
# when _DEBUG is true.
debug = (not sys.flags.ignore_environment and
bool(os.environ.get('PYTHONASYNCIODEBUG')))
if hasattr(sys, '_xoptions') and 'dev' in sys._xoptions:
if sys.flags.dev_mode:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now the second check is shorter, this could be simplified to:

debug = sys.flags.dev_mode or (not sys.flags.ignore_environment and
                               bool(os.environ.get('PYTHONASYNCIODEBUG'))
return debug

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

debug = True
return debug

Expand Down
18 changes: 16 additions & 2 deletions Lib/test/test_cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,14 +508,18 @@ def test_sys_flags_set(self):
with self.subTest(envar_value=value):
assert_python_ok('-c', code, **env_vars)

def run_xdev(self, *args, check_exitcode=True):
def run_xdev(self, *args, check_exitcode=True, xdev=True):
env = dict(os.environ)
env.pop('PYTHONWARNINGS', None)
env.pop('PYTHONDEVMODE', None)
# Force malloc() to disable the debug hooks which are enabled
# by default for Python compiled in debug mode
env['PYTHONMALLOC'] = 'malloc'

args = (sys.executable, '-X', 'dev', *args)
if xdev:
args = (sys.executable, '-X', 'dev', *args)
else:
args = (sys.executable, *args)
proc = subprocess.run(args,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
Expand All @@ -526,6 +530,14 @@ def run_xdev(self, *args, check_exitcode=True):
return proc.stdout.rstrip()

def test_xdev(self):
# sys.flags.dev_mode
code = "import sys; print(sys.flags.dev_mode)"
out = self.run_xdev("-c", code, xdev=False)
self.assertEqual(out, "False")
out = self.run_xdev("-c", code)
self.assertEqual(out, "True")

# Warnings
code = ("import sys, warnings; "
"print(' '.join('%s::%s' % (f[0], f[2].__name__) "
"for f in warnings.filters))")
Expand Down Expand Up @@ -555,6 +567,7 @@ def test_xdev(self):
"default::ResourceWarning "
"default::Warning")

# Memory allocator debug hooks
try:
import _testcapi
except ImportError:
Expand All @@ -569,6 +582,7 @@ def test_xdev(self):
alloc_name = "malloc_debug"
self.assertEqual(out, alloc_name)

# Faulthandler
try:
import faulthandler
except ImportError:
Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,10 +526,12 @@ def test_sys_flags(self):
attrs = ("debug",
"inspect", "interactive", "optimize", "dont_write_bytecode",
"no_user_site", "no_site", "ignore_environment", "verbose",
"bytes_warning", "quiet", "hash_randomization", "isolated")
"bytes_warning", "quiet", "hash_randomization", "isolated",
"dev_mode")
for attr in attrs:
self.assertTrue(hasattr(sys.flags, attr), attr)
self.assertEqual(type(getattr(sys.flags, attr)), int, attr)
attr_type = bool if attr == "dev_mode" else int
self.assertEqual(type(getattr(sys.flags, attr)), attr_type, attr)
self.assertTrue(repr(sys.flags))
self.assertEqual(len(sys.flags), len(attrs))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add :attr:`sys.flags.dev_mode` flag
5 changes: 4 additions & 1 deletion Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1814,21 +1814,23 @@ static PyStructSequence_Field flags_fields[] = {
{"quiet", "-q"},
{"hash_randomization", "-R"},
{"isolated", "-I"},
{"dev_mode", "-X dev"},
{0}
};

static PyStructSequence_Desc flags_desc = {
"sys.flags", /* name */
flags__doc__, /* doc */
flags_fields, /* fields */
13
14
};

static PyObject*
make_flags(void)
{
int pos = 0;
PyObject *seq;
_PyCoreConfig *core_config = &_PyGILState_GetInterpreterStateUnsafe()->core_config;

seq = PyStructSequence_New(&FlagsType);
if (seq == NULL)
Expand All @@ -1853,6 +1855,7 @@ make_flags(void)
SetFlag(Py_HashRandomizationFlag);
SetFlag(Py_IsolatedFlag);
#undef SetFlag
PyStructSequence_SET_ITEM(seq, pos++, PyBool_FromLong(core_config->dev_mode));

if (PyErr_Occurred()) {
Py_DECREF(seq);
Expand Down