Skip to content

Commit d2d58ca

Browse files
committed
gh-133905: Fix parsing problems in example code
1 parent a05433f commit d2d58ca

28 files changed

+66
-24
lines changed

Doc/faq/programming.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ Why are default values shared between objects?
340340
This type of bug commonly bites neophyte programmers. Consider this function::
341341

342342
def foo(mydict={}): # Danger: shared reference to one dict for all calls
343-
... compute something ...
343+
# ... compute something ...
344344
mydict[key] = value
345345
return mydict
346346

@@ -382,7 +382,7 @@ requested again. This is called "memoizing", and can be implemented like this::
382382
return _cache[(arg1, arg2)]
383383

384384
# Calculate the value
385-
result = ... expensive computation ...
385+
result = ... # ... expensive computation ...
386386
_cache[(arg1, arg2)] = result # Store result in the cache
387387
return result
388388

@@ -1555,7 +1555,8 @@ that does something::
15551555
... # code to search a mailbox
15561556
elif isinstance(obj, Document):
15571557
... # code to search a document
1558-
elif ...
1558+
elif ...:
1559+
...
15591560

15601561
A better approach is to define a ``search()`` method on all the classes and just
15611562
call it::

Doc/howto/ipaddress.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ membership test syntax like this::
255255

256256
if address in network:
257257
# do something
258+
...
258259

259260
Containment testing is done efficiently based on the network prefix::
260261

Doc/howto/logging.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ following example::
190190
numeric_level = getattr(logging, loglevel.upper(), None)
191191
if not isinstance(numeric_level, int):
192192
raise ValueError('Invalid log level: %s' % loglevel)
193-
logging.basicConfig(level=numeric_level, ...)
193+
logging.basicConfig(..., level=numeric_level)
194194

195195
The call to :func:`basicConfig` should come *before* any calls to a logger's
196196
methods such as :meth:`~Logger.debug`, :meth:`~Logger.info`, etc. Otherwise,

Doc/howto/urllib2.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ Number 1
360360
print('Reason: ', e.reason)
361361
else:
362362
# everything is fine
363+
...
363364

364365

365366
.. note::
@@ -386,6 +387,7 @@ Number 2
386387
print('Error code: ', e.code)
387388
else:
388389
# everything is fine
390+
...
389391

390392

391393
info and geturl

Doc/library/argparse.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ upper-cased name. For example::
698698

699699
>>> parser = argparse.ArgumentParser(prog='PROG')
700700
>>> parser.add_argument('--foo-bar')
701-
>>> parser.parse_args(['--foo-bar', 'FOO-BAR']
701+
>>> parser.parse_args(['--foo-bar', 'FOO-BAR'])
702702
Namespace(foo_bar='FOO-BAR')
703703
>>> parser.print_help()
704704
usage: [-h] [--foo-bar FOO-BAR]

Doc/library/ast.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
.. testsetup::
1111

12-
import ast
12+
import ast
1313

1414
**Source code:** :source:`Lib/ast.py`
1515

Doc/library/asyncio-eventloop.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,7 @@ Do not instantiate the :class:`Server` class directly.
16561656

16571657
async with srv:
16581658
# some code
1659+
...
16591660

16601661
# At this point, srv is closed and no longer accepts new connections.
16611662

Doc/library/asyncio-sync.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Lock
5252
# ... later
5353
async with lock:
5454
# access shared state
55+
...
5556

5657
which is equivalent to::
5758

@@ -61,6 +62,7 @@ Lock
6162
await lock.acquire()
6263
try:
6364
# access shared state
65+
...
6466
finally:
6567
lock.release()
6668

@@ -300,6 +302,7 @@ Semaphore
300302
# ... later
301303
async with sem:
302304
# work with shared resource
305+
...
303306

304307
which is equivalent to::
305308

@@ -309,6 +312,7 @@ Semaphore
309312
await sem.acquire()
310313
try:
311314
# work with shared resource
315+
...
312316
finally:
313317
sem.release()
314318

Doc/library/contextlib.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Functions and classes provided:
6969
The function can then be used like this::
7070

7171
>>> with managed_resource(timeout=3600) as resource:
72+
... ...
7273
... # Resource is released at the end of this block,
7374
... # even if code in the block raises an exception
7475

@@ -145,6 +146,7 @@ Functions and classes provided:
145146
@timeit()
146147
async def main():
147148
# ... async code ...
149+
...
148150

149151
When used as a decorator, a new generator instance is implicitly created on
150152
each function call. This allows the otherwise "one-shot" context managers
@@ -241,6 +243,7 @@ Functions and classes provided:
241243
cm = contextlib.nullcontext()
242244
with cm:
243245
# Do something
246+
...
244247

245248
An example using *enter_result*::
246249

@@ -254,6 +257,7 @@ Functions and classes provided:
254257

255258
with cm as file:
256259
# Perform processing on the file
260+
...
257261

258262
It can also be used as a stand-in for
259263
:ref:`asynchronous context managers <async-context-managers>`::
@@ -268,6 +272,7 @@ Functions and classes provided:
268272

269273
async with cm as session:
270274
# Send http requests with session
275+
...
271276

272277
.. versionadded:: 3.7
273278

@@ -438,12 +443,14 @@ Functions and classes provided:
438443
def f():
439444
with cm():
440445
# Do stuff
446+
...
441447

442448
``ContextDecorator`` lets you instead write::
443449

444450
@cm()
445451
def f():
446452
# Do stuff
453+
...
447454

448455
It makes it clear that the ``cm`` applies to the whole function, rather than
449456
just a piece of it (and saving an indentation level is nice, too).
@@ -706,9 +713,11 @@ protocol can be separated slightly in order to allow this::
706713
x = stack.enter_context(cm)
707714
except Exception:
708715
# handle __enter__ exception
716+
...
709717
else:
710718
with stack:
711719
# Handle normal case
720+
...
712721

713722
Actually needing to do this is likely to indicate that the underlying API
714723
should be providing a direct resource management interface for use with

Doc/library/dataclasses.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ Module contents
226226
:meth:`~object.__init__` method, which will be defined as::
227227

228228
def __init__(self, a: int, b: int = 0):
229+
...
229230

230231
:exc:`TypeError` will be raised if a field without a default value
231232
follows a field with a default value. This is true whether this
@@ -670,6 +671,7 @@ type of :attr:`!x` is :class:`int`, as specified in class :class:`!C`.
670671
The generated :meth:`~object.__init__` method for :class:`!C` will look like::
671672

672673
def __init__(self, x: int = 15, y: int = 0, z: int = 10):
674+
...
673675

674676
Re-ordering of keyword-only parameters in :meth:`!__init__`
675677
-----------------------------------------------------------
@@ -698,6 +700,7 @@ fields, and :attr:`!Base.x` and :attr:`!D.z` are regular fields::
698700
The generated :meth:`!__init__` method for :class:`!D` will look like::
699701

700702
def __init__(self, x: Any = 15.0, z: int = 10, *, y: int = 0, w: int = 1, t: int = 0):
703+
...
701704

702705
Note that the parameters have been re-ordered from how they appear in
703706
the list of fields: parameters derived from regular fields are

0 commit comments

Comments
 (0)