Skip to content

More stdlib updates #5737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions Lib/fileinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
sequence must be accessed in strictly sequential order; sequence
access and readline() cannot be mixed.

Optional in-place filtering: if the keyword argument inplace=1 is
Optional in-place filtering: if the keyword argument inplace=True is
passed to input() or to the FileInput constructor, the file is moved
to a backup file and standard output is directed to the input file.
This makes it possible to write a filter that rewrites its input file
Expand Down Expand Up @@ -399,7 +399,7 @@ def isstdin(self):


def hook_compressed(filename, mode, *, encoding=None, errors=None):
if encoding is None: # EncodingWarning is emitted in FileInput() already.
if encoding is None and "b" not in mode: # EncodingWarning is emitted in FileInput() already.
encoding = "locale"
ext = os.path.splitext(filename)[1]
if ext == '.gz':
Expand Down
15 changes: 11 additions & 4 deletions Lib/getpass.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import io
import os
import sys
import warnings

__all__ = ["getpass","getuser","GetPassWarning"]

Expand Down Expand Up @@ -118,6 +117,7 @@ def win_getpass(prompt='Password: ', stream=None):


def fallback_getpass(prompt='Password: ', stream=None):
import warnings
warnings.warn("Can not control echo on the terminal.", GetPassWarning,
stacklevel=2)
if not stream:
Expand Down Expand Up @@ -156,17 +156,24 @@ def getuser():

First try various environment variables, then the password
database. This works on Windows as long as USERNAME is set.
Any failure to find a username raises OSError.

.. versionchanged:: 3.13
Previously, various exceptions beyond just :exc:`OSError`
were raised.
"""

for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
user = os.environ.get(name)
if user:
return user

# If this fails, the exception will "explain" why
import pwd
return pwd.getpwuid(os.getuid())[0]
try:
import pwd
return pwd.getpwuid(os.getuid())[0]
except (ImportError, KeyError) as e:
raise OSError('No username set in the environment') from e


# Bind the name getpass to the appropriate function
try:
Expand Down
112 changes: 83 additions & 29 deletions Lib/reprlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,49 +29,100 @@ def wrapper(self):
wrapper.__name__ = getattr(user_function, '__name__')
wrapper.__qualname__ = getattr(user_function, '__qualname__')
wrapper.__annotations__ = getattr(user_function, '__annotations__', {})
wrapper.__type_params__ = getattr(user_function, '__type_params__', ())
wrapper.__wrapped__ = user_function
return wrapper

return decorating_function

class Repr:

def __init__(self):
self.maxlevel = 6
self.maxtuple = 6
self.maxlist = 6
self.maxarray = 5
self.maxdict = 4
self.maxset = 6
self.maxfrozenset = 6
self.maxdeque = 6
self.maxstring = 30
self.maxlong = 40
self.maxother = 30
_lookup = {
'tuple': 'builtins',
'list': 'builtins',
'array': 'array',
'set': 'builtins',
'frozenset': 'builtins',
'deque': 'collections',
'dict': 'builtins',
'str': 'builtins',
'int': 'builtins'
}

def __init__(
self, *, maxlevel=6, maxtuple=6, maxlist=6, maxarray=5, maxdict=4,
maxset=6, maxfrozenset=6, maxdeque=6, maxstring=30, maxlong=40,
maxother=30, fillvalue='...', indent=None,
):
self.maxlevel = maxlevel
self.maxtuple = maxtuple
self.maxlist = maxlist
self.maxarray = maxarray
self.maxdict = maxdict
self.maxset = maxset
self.maxfrozenset = maxfrozenset
self.maxdeque = maxdeque
self.maxstring = maxstring
self.maxlong = maxlong
self.maxother = maxother
self.fillvalue = fillvalue
self.indent = indent

def repr(self, x):
return self.repr1(x, self.maxlevel)

def repr1(self, x, level):
typename = type(x).__name__
cls = type(x)
typename = cls.__name__

if ' ' in typename:
parts = typename.split()
typename = '_'.join(parts)
if hasattr(self, 'repr_' + typename):
return getattr(self, 'repr_' + typename)(x, level)
else:
return self.repr_instance(x, level)

method = getattr(self, 'repr_' + typename, None)
if method:
# not defined in this class
if typename not in self._lookup:
return method(x, level)
module = getattr(cls, '__module__', None)
# defined in this class and is the module intended
if module == self._lookup[typename]:
return method(x, level)

return self.repr_instance(x, level)

def _join(self, pieces, level):
if self.indent is None:
return ', '.join(pieces)
if not pieces:
return ''
indent = self.indent
if isinstance(indent, int):
if indent < 0:
raise ValueError(
f'Repr.indent cannot be negative int (was {indent!r})'
)
indent *= ' '
try:
sep = ',\n' + (self.maxlevel - level + 1) * indent
except TypeError as error:
raise TypeError(
f'Repr.indent must be a str, int or None, not {type(indent)}'
) from error
return sep.join(('', *pieces, ''))[1:-len(indent) or None]

def _repr_iterable(self, x, level, left, right, maxiter, trail=''):
n = len(x)
if level <= 0 and n:
s = '...'
s = self.fillvalue
else:
newlevel = level - 1
repr1 = self.repr1
pieces = [repr1(elem, newlevel) for elem in islice(x, maxiter)]
if n > maxiter: pieces.append('...')
s = ', '.join(pieces)
if n == 1 and trail: right = trail + right
if n > maxiter:
pieces.append(self.fillvalue)
s = self._join(pieces, level)
if n == 1 and trail and self.indent is None:
right = trail + right
return '%s%s%s' % (left, s, right)

def repr_tuple(self, x, level):
Expand Down Expand Up @@ -104,17 +155,20 @@ def repr_deque(self, x, level):

def repr_dict(self, x, level):
n = len(x)
if n == 0: return '{}'
if level <= 0: return '{...}'
if n == 0:
return '{}'
if level <= 0:
return '{' + self.fillvalue + '}'
newlevel = level - 1
repr1 = self.repr1
pieces = []
for key in islice(_possibly_sorted(x), self.maxdict):
keyrepr = repr1(key, newlevel)
valrepr = repr1(x[key], newlevel)
pieces.append('%s: %s' % (keyrepr, valrepr))
if n > self.maxdict: pieces.append('...')
s = ', '.join(pieces)
if n > self.maxdict:
pieces.append(self.fillvalue)
s = self._join(pieces, level)
return '{%s}' % (s,)

def repr_str(self, x, level):
Expand All @@ -123,15 +177,15 @@ def repr_str(self, x, level):
i = max(0, (self.maxstring-3)//2)
j = max(0, self.maxstring-3-i)
s = builtins.repr(x[:i] + x[len(x)-j:])
s = s[:i] + '...' + s[len(s)-j:]
s = s[:i] + self.fillvalue + s[len(s)-j:]
return s

def repr_int(self, x, level):
s = builtins.repr(x) # XXX Hope this isn't too slow...
if len(s) > self.maxlong:
i = max(0, (self.maxlong-3)//2)
j = max(0, self.maxlong-3-i)
s = s[:i] + '...' + s[len(s)-j:]
s = s[:i] + self.fillvalue + s[len(s)-j:]
return s

def repr_instance(self, x, level):
Expand All @@ -144,7 +198,7 @@ def repr_instance(self, x, level):
if len(s) > self.maxother:
i = max(0, (self.maxother-3)//2)
j = max(0, self.maxother-3-i)
s = s[:i] + '...' + s[len(s)-j:]
s = s[:i] + self.fillvalue + s[len(s)-j:]
return s


Expand Down
63 changes: 63 additions & 0 deletions Lib/test/support/i18n_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import re
import subprocess
import sys
import unittest
from pathlib import Path
from test.support import REPO_ROOT, TEST_HOME_DIR, requires_subprocess
from test.test_tools import skip_if_missing


pygettext = Path(REPO_ROOT) / 'Tools' / 'i18n' / 'pygettext.py'

msgid_pattern = re.compile(r'msgid(.*?)(?:msgid_plural|msgctxt|msgstr)',
re.DOTALL)
msgid_string_pattern = re.compile(r'"((?:\\"|[^"])*)"')


def _generate_po_file(path, *, stdout_only=True):
res = subprocess.run([sys.executable, pygettext,
'--no-location', '-o', '-', path],
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
text=True)
if stdout_only:
return res.stdout
return res


def _extract_msgids(po):
msgids = []
for msgid in msgid_pattern.findall(po):
msgid_string = ''.join(msgid_string_pattern.findall(msgid))
msgid_string = msgid_string.replace(r'\"', '"')
if msgid_string:
msgids.append(msgid_string)
return sorted(msgids)


def _get_snapshot_path(module_name):
return Path(TEST_HOME_DIR) / 'translationdata' / module_name / 'msgids.txt'


@requires_subprocess()
class TestTranslationsBase(unittest.TestCase):

def assertMsgidsEqual(self, module):
'''Assert that msgids extracted from a given module match a
snapshot.

'''
skip_if_missing('i18n')
res = _generate_po_file(module.__file__, stdout_only=False)
self.assertEqual(res.returncode, 0)
self.assertEqual(res.stderr, '')
msgids = _extract_msgids(res.stdout)
snapshot_path = _get_snapshot_path(module.__name__)
snapshot = snapshot_path.read_text().splitlines()
self.assertListEqual(msgids, snapshot)


def update_translation_snapshots(module):
contents = _generate_po_file(module.__file__)
msgids = _extract_msgids(contents)
snapshot_path = _get_snapshot_path(module.__name__)
snapshot_path.write_text('\n'.join(msgids))
Loading
Loading