Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-106232: Make timeit doc command lines compatible with Windows. #106296

Merged
merged 2 commits into from
Jul 1, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions Doc/library/timeit.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ can be used to compare three different expressions:

.. code-block:: shell-session

$ python -m timeit '"-".join(str(n) for n in range(100))'
$ python -m timeit "'-'.join(str(n) for n in range(100))"
10000 loops, best of 5: 30.2 usec per loop
$ python -m timeit '"-".join([str(n) for n in range(100)])'
$ python -m timeit "'-'.join([str(n) for n in range(100)])"
10000 loops, best of 5: 27.5 usec per loop
$ python -m timeit '"-".join(map(str, range(100)))'
$ python -m timeit "'-'.join(map(str, range(100)))"
10000 loops, best of 5: 23.2 usec per loop

This can be achieved from the :ref:`python-interface` with::
Expand Down Expand Up @@ -277,9 +277,9 @@ It is possible to provide a setup statement that is executed only once at the be

.. code-block:: shell-session

$ python -m timeit -s 'text = "sample string"; char = "g"' 'char in text'
$ python -m timeit -s "text = 'sample string'; char = 'g'" "char in text"
5000000 loops, best of 5: 0.0877 usec per loop
$ python -m timeit -s 'text = "sample string"; char = "g"' 'text.find(char)'
$ python -m timeit -s "text = 'sample string'; char = 'g'" "text.find(char)"
1000000 loops, best of 5: 0.342 usec per loop

In the output, there are three fields. The loop count, which tells you how many
Expand Down Expand Up @@ -313,14 +313,14 @@ to test for missing and present object attributes:

.. code-block:: shell-session

$ python -m timeit 'try:' ' str.__bool__' 'except AttributeError:' ' pass'
$ python -m timeit "try:" " str.__bool__" "except AttributeError:" " pass"
20000 loops, best of 5: 15.7 usec per loop
$ python -m timeit 'if hasattr(str, "__bool__"): pass'
$ python -m timeit "if hasattr(str, '__bool__'): pass"
50000 loops, best of 5: 4.26 usec per loop

$ python -m timeit 'try:' ' int.__bool__' 'except AttributeError:' ' pass'
$ python -m timeit "try:" " int.__bool__" "except AttributeError:" " pass"
200000 loops, best of 5: 1.43 usec per loop
$ python -m timeit 'if hasattr(int, "__bool__"): pass'
$ python -m timeit "if hasattr(int, '__bool__'): pass"
100000 loops, best of 5: 2.23 usec per loop

::
Expand Down
2 changes: 1 addition & 1 deletion Doc/using/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
first element will be set to ``"-m"``). As with the :option:`-c` option,
the current directory will be added to the start of :data:`sys.path`.

:option:`-I` option can be used to run the script in isolated mode where

Check warning on line 104 in Doc/using/cmdline.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

'envvar' reference target not found: PYTHON*
:data:`sys.path` contains neither the current directory nor the user's
site-packages directory. All :envvar:`PYTHON*` environment variables are
ignored, too.
Expand All @@ -109,7 +109,7 @@
Many standard library modules contain code that is invoked on their execution
as a script. An example is the :mod:`timeit` module::

python -m timeit -s 'setup here' 'benchmarked code here'
python -m timeit -s "setup here" "benchmarked code here"
python -m timeit -h # for details

.. audit-event:: cpython.run_module module-name cmdoption-m
Expand Down Expand Up @@ -159,7 +159,7 @@
added to the start of :data:`sys.path` and the ``__main__.py`` file in
that location is executed as the :mod:`__main__` module.

:option:`-I` option can be used to run the script in isolated mode where

Check warning on line 162 in Doc/using/cmdline.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

'envvar' reference target not found: PYTHON*
:data:`sys.path` contains neither the script's directory nor the user's
site-packages directory. All :envvar:`PYTHON*` environment variables are
ignored, too.
Expand Down Expand Up @@ -280,7 +280,7 @@

.. cmdoption:: -E

Ignore all :envvar:`PYTHON*` environment variables, e.g.

Check warning on line 283 in Doc/using/cmdline.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

'envvar' reference target not found: PYTHON*
:envvar:`PYTHONPATH` and :envvar:`PYTHONHOME`, that might be set.

See also the :option:`-P` and :option:`-I` (isolated) options.
Expand All @@ -302,7 +302,7 @@
Run Python in isolated mode. This also implies :option:`-E`, :option:`-P`
and :option:`-s` options.

In isolated mode :data:`sys.path` contains neither the script's directory nor

Check warning on line 305 in Doc/using/cmdline.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

'envvar' reference target not found: PYTHON*
the user's site-packages directory. All :envvar:`PYTHON*` environment
variables are ignored, too. Further restrictions may be imposed to prevent
the user from injecting malicious code.
Expand Down Expand Up @@ -361,7 +361,7 @@
:envvar:`PYTHONHASHSEED` environment variable is set to ``0``, since hash
randomization is enabled by default.

On previous versions of Python, this option turns on hash randomization,

Check warning on line 364 in Doc/using/cmdline.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:meth reference target not found: __hash__
so that the :meth:`__hash__` values of str and bytes objects
are "salted" with an unpredictable random value. Although they remain
constant within an individual Python process, they are not predictable
Expand Down Expand Up @@ -629,7 +629,7 @@

.. envvar:: PYTHONPATH

Augment the default search path for module files. The format is the same as

Check warning on line 632 in Doc/using/cmdline.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

'envvar' reference target not found: PATH
the shell's :envvar:`PATH`: one or more directory pathnames separated by
:data:`os.pathsep` (e.g. colons on Unix or semicolons on Windows).
Non-existent directories are silently ignored.
Expand Down Expand Up @@ -849,7 +849,7 @@

.. envvar:: PYTHONFAULTHANDLER

If this environment variable is set to a non-empty string,

Check warning on line 852 in Doc/using/cmdline.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:const reference target not found: SIGSEGV

Check warning on line 852 in Doc/using/cmdline.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:const reference target not found: SIGFPE

Check warning on line 852 in Doc/using/cmdline.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:const reference target not found: SIGABRT

Check warning on line 852 in Doc/using/cmdline.rst

View workflow job for this annotation

GitHub Actions / Docs / Docs

py:const reference target not found: SIGBUS
:func:`faulthandler.enable` is called at startup: install a handler for
:const:`SIGSEGV`, :const:`SIGFPE`, :const:`SIGABRT`, :const:`SIGBUS` and
:const:`SIGILL` signals to dump the Python traceback. This is equivalent to
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Make timeit doc command lines compatible with Windows by using double quotes
for arguments. This works on linux and macOS also.