Skip to content

Commit 92fe28f

Browse files
Merge branch 'main' into docs-clarify-argparse-neg-num
2 parents 7d177f3 + 5ffefdb commit 92fe28f

30 files changed

Lines changed: 359 additions & 165 deletions

Doc/library/argparse.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,12 +1564,12 @@ it exits and prints the error along with a usage message::
15641564
>>> # invalid option
15651565
>>> parser.parse_args(['--bar'])
15661566
usage: PROG [-h] [--foo FOO] [bar]
1567-
PROG: error: no such option: --bar
1567+
PROG: error: unrecognized arguments: --bar
15681568

15691569
>>> # wrong number of arguments
15701570
>>> parser.parse_args(['spam', 'badger'])
15711571
usage: PROG [-h] [--foo FOO] [bar]
1572-
PROG: error: extra arguments found: badger
1572+
PROG: error: unrecognized arguments: badger
15731573

15741574

15751575
Arguments containing ``-``
@@ -1606,7 +1606,7 @@ there are no options in the parser that look like negative numbers::
16061606
>>> # negative number options present, so -2 is an option
16071607
>>> parser.parse_args(['-2'])
16081608
usage: PROG [-h] [-1 ONE] [foo]
1609-
PROG: error: no such option: -2
1609+
PROG: error: unrecognized arguments: -2
16101610

16111611
>>> # negative number options present, so both -1s are options
16121612
>>> parser.parse_args(['-1', '-1'])

Doc/library/inspect.rst

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -707,9 +707,10 @@ attributes (see :ref:`import-mod-attrs` for module attributes):
707707
Retrieving source code
708708
----------------------
709709

710-
.. function:: getdoc(object, *, inherit_class_doc=True, fallback_to_class_doc=True)
710+
.. function:: getdoc(object, *, inherit_class_doc=True, fallback_to_class_doc=True, dedent=True)
711711

712-
Get the documentation string for an object, cleaned up with :func:`cleandoc`.
712+
Get the documentation string for an object, cleaned up with :func:`cleandoc`
713+
(with the same meaning of *dedent*).
713714
If the documentation string for an object is not provided:
714715

715716
* if the object is a class and *inherit_class_doc* is true (by default),
@@ -730,6 +731,9 @@ Retrieving source code
730731
Documentation strings on :class:`~functools.cached_property`
731732
objects are now inherited if not overridden.
732733

734+
.. versionchanged:: next
735+
Added the *dedent* parameter.
736+
733737

734738
.. function:: getcomments(object)
735739

@@ -791,15 +795,23 @@ Retrieving source code
791795
former.
792796

793797

794-
.. function:: cleandoc(doc)
798+
.. function:: cleandoc(doc, *, dedent=True)
795799

796800
Clean up indentation from docstrings that are indented to line up with blocks
797801
of code.
798802

799803
All leading whitespace is removed from the first line. Any leading whitespace
800-
that can be uniformly removed from the second line onwards is removed. Empty
801-
lines at the beginning and end are subsequently removed. Also, all tabs are
802-
expanded to spaces.
804+
that can be uniformly removed from the second line onwards is removed, unless
805+
*dedent* is false. Empty lines at the beginning and end are subsequently
806+
removed. Also, all tabs are expanded to spaces.
807+
808+
Since Python 3.13 the compiler removes the indentation of docstrings, so
809+
*dedent* only affects documentation strings which are not written as
810+
docstrings in the source code, like those generated by Argument Clinic,
811+
where the indentation is meaningful.
812+
813+
.. versionchanged:: next
814+
Added the *dedent* parameter.
803815

804816

805817
.. _inspect-signature-object:

Doc/library/subprocess.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ underlying :class:`Popen` interface can be used directly.
236236

237237
.. attribute:: returncode
238238

239-
Exit status of the child process. If the process exited due to a
240-
signal, this will be the negative signal number.
239+
Exit status of the child process, an integer. If the process
240+
exited due to a signal, this will be the negative signal number.
241241

242242
.. attribute:: cmd
243243

Doc/library/test.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,32 @@ The :mod:`!test.support.os_helper` module provides support for os tests.
16561656
wrapped with a wait loop that checks for the existence of the file.
16571657

16581658

1659+
.. decorator:: with_source_date_epoch(*, epoch=123456789)
1660+
1661+
A decorator for running tests with the :envvar:`SOURCE_DATE_EPOCH`
1662+
environment variable set to *epoch*.
1663+
1664+
1665+
.. decorator:: without_source_date_epoch
1666+
1667+
A decorator for running tests with the :envvar:`SOURCE_DATE_EPOCH`
1668+
environment variable unset.
1669+
1670+
1671+
.. class:: SourceDateEpochTestMeta
1672+
1673+
Metaclass wrapping all test methods of the class with
1674+
:func:`with_source_date_epoch` if the *source_date_epoch* keyword class
1675+
argument is true, or with :func:`without_source_date_epoch` otherwise.
1676+
For example::
1677+
1678+
class TestsWithSourceEpoch(Tests,
1679+
metaclass=SourceDateEpochTestMeta,
1680+
source_date_epoch=True):
1681+
pass
1682+
1683+
1684+
16591685
:mod:`!test.support.import_helper` --- Utilities for import tests
16601686
=================================================================
16611687

Doc/library/zipfile.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ ZipFile objects
285285
Added support for specifying member name encoding for reading
286286
metadata in the zipfile's directory and file headers.
287287

288+
.. versionchanged:: next
289+
Deleting a writable, open :class:`zipfile.ZipFile` now emits a
290+
:exc:`ResourceWarning`. Use as a :term:`context manager` or call
291+
:meth:`~zipfile.ZipFile.close` explicitly.
288292

289293
.. method:: ZipFile.close()
290294

Lib/argparse.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2917,11 +2917,14 @@ def print_help(self, file=None):
29172917
self._print_message(help_text, file)
29182918

29192919
def _print_message(self, message, file=None):
2920-
if message:
2921-
file = file or _sys.stderr
2920+
if not message:
2921+
return
2922+
if file is None:
2923+
file = _sys.stderr
2924+
if file is not None:
29222925
try:
29232926
file.write(message)
2924-
except (AttributeError, OSError):
2927+
except OSError:
29252928
pass
29262929

29272930
def _get_theme(self, file=None):

Lib/inspect.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -793,12 +793,14 @@ def _getowndoc(obj):
793793
except AttributeError:
794794
return None
795795

796-
def getdoc(object, *, fallback_to_class_doc=True, inherit_class_doc=True):
796+
def getdoc(object, *, fallback_to_class_doc=True, inherit_class_doc=True,
797+
dedent=True):
797798
"""Get the documentation string for an object.
798799
799800
All tabs are expanded to spaces. To clean up docstrings that are
800801
indented to line up with blocks of code, any whitespace than can be
801-
uniformly removed from the second line onwards is removed."""
802+
uniformly removed from the second line onwards is removed, unless
803+
dedent is false."""
802804
if fallback_to_class_doc:
803805
try:
804806
doc = object.__doc__
@@ -813,22 +815,23 @@ def getdoc(object, *, fallback_to_class_doc=True, inherit_class_doc=True):
813815
return None
814816
if not isinstance(doc, str):
815817
return None
816-
return cleandoc(doc)
818+
return cleandoc(doc, dedent=dedent)
817819

818-
def cleandoc(doc):
820+
def cleandoc(doc, *, dedent=True):
819821
"""Clean up indentation from docstrings.
820822
821823
Any whitespace that can be uniformly removed from the second line
822-
onwards is removed."""
824+
onwards is removed, unless dedent is false."""
823825
lines = doc.expandtabs().split('\n')
824826

825827
# Find minimum indentation of any non-blank lines after first line.
826828
margin = sys.maxsize
827-
for line in lines[1:]:
828-
content = len(line.lstrip(' '))
829-
if content:
830-
indent = len(line) - content
831-
margin = min(margin, indent)
829+
if dedent:
830+
for line in lines[1:]:
831+
content = len(line.lstrip(' '))
832+
if content:
833+
indent = len(line) - content
834+
margin = min(margin, indent)
832835
# Remove indentation.
833836
if lines:
834837
lines[0] = lines[0].lstrip(' ')

Lib/pydoc.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,12 @@ def pathdirs():
130130
return dirs
131131

132132
def _getdoc(object):
133+
# Docstrings written in the source are dedented by the compiler; the
134+
# indentation of generated docstrings is meaningful.
133135
return inspect.getdoc(object,
134136
fallback_to_class_doc=False,
135-
inherit_class_doc=False)
137+
inherit_class_doc=False,
138+
dedent=False)
136139

137140
def getdoc(object):
138141
"""Get the doc string or comments for an object."""

Lib/subprocess.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,16 @@ def __init__(self, returncode, cmd, output=None, stderr=None):
143143
self.stderr = stderr
144144

145145
def __str__(self):
146-
if self.returncode and self.returncode < 0:
146+
if isinstance(self.returncode, int) and self.returncode < 0:
147147
try:
148148
return "Command %r died with %r." % (
149149
self.cmd, signal.Signals(-self.returncode))
150150
except ValueError:
151151
return "Command %r died with unknown signal %d." % (
152152
self.cmd, -self.returncode)
153153
else:
154-
return "Command %r returned non-zero exit status %d." % (
155-
self.cmd, self.returncode)
154+
return (f"Command {self.cmd!r} returned non-zero "
155+
f"exit status {self.returncode}.")
156156

157157
@property
158158
def stdout(self):

Lib/test/dtracedata/call_stack.stp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function basename:string(path:string)
1010
return last_token;
1111
}
1212

13-
probe process.mark("function__entry")
13+
probe @PYTHON_SYSTEMTAP_PROBE@("function__entry")
1414
{
1515
funcname = user_string($arg2);
1616

@@ -19,7 +19,8 @@ probe process.mark("function__entry")
1919
}
2020
}
2121

22-
probe process.mark("function__entry"), process.mark("function__return")
22+
probe @PYTHON_SYSTEMTAP_PROBE@("function__entry"),
23+
@PYTHON_SYSTEMTAP_PROBE@("function__return")
2324
{
2425
filename = user_string($arg1);
2526
funcname = user_string($arg2);
@@ -31,7 +32,7 @@ probe process.mark("function__entry"), process.mark("function__return")
3132
}
3233
}
3334

34-
probe process.mark("function__return")
35+
probe @PYTHON_SYSTEMTAP_PROBE@("function__return")
3536
{
3637
funcname = user_string($arg2);
3738

0 commit comments

Comments
 (0)