◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
13 changes: 11 additions & 2 deletions Doc/c-api/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ Operating System Utilities

.. versionadded:: 3.5


.. c:function:: char* Py_EncodeLocale(const wchar_t *text, size_t *error_pos)

Expand All @@ -138,19 +141,25 @@ Operating System Utilities
to free the memory. Return ``NULL`` on encoding error or memory allocation
error

If error_pos is not ``NULL``, ``*error_pos`` is set to the index of the
invalid character on encoding error, or set to ``(size_t)-1`` otherwise.

Use the :c:func:`Py_DecodeLocale` function to decode the bytes string back
to a wide character string.

.. seealso::

The :c:func:`PyUnicode_EncodeFSDefault` and
:c:func:`PyUnicode_EncodeLocale` functions.

.. versionadded:: 3.5


.. _systemfunctions:

Expand Down
13 changes: 12 additions & 1 deletion Doc/library/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ always available.
has caught :exc:`SystemExit` (such as an error flushing buffered data
in the standard streams), the exit status is changed to 120.


.. data:: flags

Expand All @@ -335,6 +338,7 @@ always available.
:const:`quiet` :option:`-q`
:const:`hash_randomization` :option:`-R`
:const:`dev_mode` :option:`-X` ``dev``
============================= =============================

.. versionchanged:: 3.2
Expand All @@ -347,7 +351,8 @@ always available.
Removed obsolete ``division_warning`` attribute.

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


.. data:: float_info
Expand Up @@ -492,6 +497,8 @@ always available.
:func:`os.fsencode` and :func:`os.fsdecode` should be used to ensure that
the correct encoding and errors mode are used.

* On Mac OS X, the encoding is ``'utf-8'``.

* On Unix, the encoding is the locale encoding.
Expand All @@ -506,6 +513,10 @@ always available.
Windows is no longer guaranteed to return ``'mbcs'``. See :pep:`529`
and :func:`_enablelegacywindowsfsencoding` for more information.

.. function:: getfilesystemencodeerrors()

Return the name of the error mode used to convert between Unicode filenames
Expand Down
13 changes: 12 additions & 1 deletion Doc/using/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ Miscellaneous options
* Set the :attr:`~sys.flags.dev_mode` attribute of :attr:`sys.flags` to
``True``

It also allows passing arbitrary values and retrieving them through the
:data:`sys._xoptions` dictionary.

Expand All @@ -455,7 +458,7 @@ Miscellaneous options
The ``-X showalloccount`` option.

.. versionadded:: 3.7
The ``-X importtime`` and ``-X dev`` options.


Options you shouldn't use
@@ -816,6 +819,14 @@ conflict.

.. versionadded:: 3.7

Debug-mode variables
~~~~~~~~~~~~~~~~~~~~

21 changes: 21 additions & 0 deletions Doc/whatsnew/3.7.rst
Original file line number Diff line number Diff line change
@@ -185,6 +185,23 @@ resolution on Linux and Windows.
PEP written and implemented by Victor Stinner


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

Expand Down Expand Up @@ -353,6 +370,10 @@ Added another argument *monetary* in :meth:`format_string` of :mod:`locale`.
If *monetary* is true, the conversion uses monetary thousands separator and
grouping strings. (Contributed by Garvit in :issue:`10379`.)

math
----

Expand Down
4 changes: 4 additions & 0 deletions Include/fileobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ PyAPI_DATA(const char *) Py_FileSystemDefaultEncodeErrors;
#endif
PyAPI_DATA(int) Py_HasFileSystemDefaultEncoding;

/* Internal API

The std printer acts as a preliminary sys.stderr until the new io
Expand Down
1 change: 1 addition & 0 deletions Include/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ typedef struct {
int show_alloc_count; /* -X showalloccount */
int dump_refs; /* PYTHONDUMPREFS */
int malloc_stats; /* PYTHONMALLOCSTATS */
} _PyCoreConfig;

#define _PyCoreConfig_INIT (_PyCoreConfig){.use_hash_seed = -1}
Expand Down
6 changes: 6 additions & 0 deletions Lib/_bootlocale.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

if sys.platform.startswith("win"):
def getpreferredencoding(do_setlocale=True):
return _locale._getdefaultlocale()[1]
else:
try:
Expand All @@ -21,13 +23,17 @@ def getpreferredencoding(do_setlocale=True):
return 'UTF-8'
else:
def getpreferredencoding(do_setlocale=True):
# This path for legacy systems needs the more complex
# getdefaultlocale() function, import the full locale module.
import locale
return locale.getpreferredencoding(do_setlocale)
else:
def getpreferredencoding(do_setlocale=True):
assert not do_setlocale
result = _locale.nl_langinfo(_locale.CODESET)
if not result and sys.platform == 'darwin':
# nl_langinfo can return an empty string
Expand Down
5 changes: 3 additions & 2 deletions Lib/encodings/__init__.py
Original file line number Diff line number Diff line change
@@ -158,8 +158,9 @@ def search_function(encoding):
if sys.platform == 'win32':
def _alias_mbcs(encoding):
try:
import _bootlocale
if encoding == _bootlocale.getpreferredencoding(False):
import encodings.mbcs
return encodings.mbcs.getregentry()
except ImportError:
Expand Down
6 changes: 6 additions & 0 deletions Lib/locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,8 @@ def resetlocale(category=LC_ALL):
# On Win32, this will return the ANSI code page
def getpreferredencoding(do_setlocale = True):
"""Return the charset that the user is likely using."""
import _bootlocale
return _bootlocale.getpreferredencoding(False)
else:
Expand All @@ -634,6 +636,8 @@ def getpreferredencoding(do_setlocale = True):
def getpreferredencoding(do_setlocale = True):
"""Return the charset that the user is likely using,
by looking at environment variables."""
res = getdefaultlocale()[1]
if res is None:
# LANG not set, default conservatively to ASCII
@@ -643,6 +647,8 @@ def getpreferredencoding(do_setlocale = True):
def getpreferredencoding(do_setlocale = True):
"""Return the charset that the user is likely using,
according to the system configuration."""
import _bootlocale
if do_setlocale:
oldloc = setlocale(LC_CTYPE)
Expand Down
2 changes: 1 addition & 1 deletion Lib/subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def _args_from_interpreter_flags():
if dev_mode:
args.extend(('-X', 'dev'))
for opt in ('faulthandler', 'tracemalloc', 'importtime',
'showalloccount', 'showrefcount'):
if opt in xoptions:
value = xoptions[opt]
if value is True:
2 changes: 1 addition & 1 deletion Lib/test/test_c_locale_coercion.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def get_child_details(cls, env_vars):
that.
"""
result, py_cmd = run_python_until_end(
"-c", cls.CHILD_PROCESS_SCRIPT,
__isolated=True,
**env_vars
)
10 changes: 2 additions & 8 deletions Lib/test/test_codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
import unittest
import encodings

from test import support

Expand Down Expand Up @@ -3180,16 +3181,9 @@ def test_incremental(self):
def test_mbcs_alias(self):
# Check that looking up our 'default' codepage will return
# mbcs when we don't have a more specific one available
import _bootlocale
def _get_fake_codepage(*a):
return 'cp123'
old_getpreferredencoding = _bootlocale.getpreferredencoding
_bootlocale.getpreferredencoding = _get_fake_codepage
try:
codec = codecs.lookup('cp123')
self.assertEqual(codec.name, 'mbcs')
finally:
_bootlocale.getpreferredencoding = old_getpreferredencoding


class ASCIITest(unittest.TestCase):
Expand Down
8 changes: 5 additions & 3 deletions Lib/test/test_sys.py
Original file line number Diff line number Diff line change
@@ -527,14 +527,16 @@ def test_sys_flags(self):
"inspect", "interactive", "optimize", "dont_write_bytecode",
"no_user_site", "no_site", "ignore_environment", "verbose",
"bytes_warning", "quiet", "hash_randomization", "isolated",
"dev_mode")
for attr in attrs:
self.assertTrue(hasattr(sys.flags, attr), 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))

def assert_raise_on_new_sys_type(self, sys_attr):
# Users are intentionally prevented from creating new instances of
# sys.flags, sys.version_info, and sys.getwindowsversion.
Expand Down Expand Up @@ -710,8 +712,8 @@ def test_c_locale_surrogateescape(self):
# have no any effect
out = self.c_locale_get_error_handler(encoding=':')
self.assertEqual(out,
'stdin: surrogateescape\n'
'stdout: surrogateescape\n'
'stderr: backslashreplace\n')
out = self.c_locale_get_error_handler(encoding='')
self.assertEqual(out,
Expand Down
Loading
Toggle all file notes Toggle all file annotations