Skip to content

Commit 8738c5b

Browse files
[3.12] gh-106232: Make timeit doc command lines compatible with Windows. (GH-106296) (#106298)
gh-106232: Make timeit doc command lines compatible with Windows. (GH-106296) Command Prompt (CMD Shell) and older versions of PowerShell require double quotes and single quotes inside the string. This form also works on linux and macOS. (cherry picked from commit 04dfc6f) Co-authored-by: Terry Jan Reedy <[email protected]>
1 parent c4298d5 commit 8738c5b

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

Doc/library/timeit.rst

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ can be used to compare three different expressions:
2727

2828
.. code-block:: shell-session
2929
30-
$ python -m timeit '"-".join(str(n) for n in range(100))'
30+
$ python -m timeit "'-'.join(str(n) for n in range(100))"
3131
10000 loops, best of 5: 30.2 usec per loop
32-
$ python -m timeit '"-".join([str(n) for n in range(100)])'
32+
$ python -m timeit "'-'.join([str(n) for n in range(100)])"
3333
10000 loops, best of 5: 27.5 usec per loop
34-
$ python -m timeit '"-".join(map(str, range(100)))'
34+
$ python -m timeit "'-'.join(map(str, range(100)))"
3535
10000 loops, best of 5: 23.2 usec per loop
3636
3737
This can be achieved from the :ref:`python-interface` with::
@@ -277,9 +277,9 @@ It is possible to provide a setup statement that is executed only once at the be
277277

278278
.. code-block:: shell-session
279279
280-
$ python -m timeit -s 'text = "sample string"; char = "g"' 'char in text'
280+
$ python -m timeit -s "text = 'sample string'; char = 'g'" "char in text"
281281
5000000 loops, best of 5: 0.0877 usec per loop
282-
$ python -m timeit -s 'text = "sample string"; char = "g"' 'text.find(char)'
282+
$ python -m timeit -s "text = 'sample string'; char = 'g'" "text.find(char)"
283283
1000000 loops, best of 5: 0.342 usec per loop
284284
285285
In the output, there are three fields. The loop count, which tells you how many
@@ -313,14 +313,14 @@ to test for missing and present object attributes:
313313

314314
.. code-block:: shell-session
315315
316-
$ python -m timeit 'try:' ' str.__bool__' 'except AttributeError:' ' pass'
316+
$ python -m timeit "try:" " str.__bool__" "except AttributeError:" " pass"
317317
20000 loops, best of 5: 15.7 usec per loop
318-
$ python -m timeit 'if hasattr(str, "__bool__"): pass'
318+
$ python -m timeit "if hasattr(str, '__bool__'): pass"
319319
50000 loops, best of 5: 4.26 usec per loop
320320
321-
$ python -m timeit 'try:' ' int.__bool__' 'except AttributeError:' ' pass'
321+
$ python -m timeit "try:" " int.__bool__" "except AttributeError:" " pass"
322322
200000 loops, best of 5: 1.43 usec per loop
323-
$ python -m timeit 'if hasattr(int, "__bool__"): pass'
323+
$ python -m timeit "if hasattr(int, '__bool__'): pass"
324324
100000 loops, best of 5: 2.23 usec per loop
325325
326326
::

Doc/using/cmdline.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ source.
109109
Many standard library modules contain code that is invoked on their execution
110110
as a script. An example is the :mod:`timeit` module::
111111

112-
python -m timeit -s 'setup here' 'benchmarked code here'
112+
python -m timeit -s "setup here" "benchmarked code here"
113113
python -m timeit -h # for details
114114

115115
.. audit-event:: cpython.run_module module-name cmdoption-m
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Make timeit doc command lines compatible with Windows by using double quotes
2+
for arguments. This works on linux and macOS also.

0 commit comments

Comments
 (0)