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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

timeit basic examples are not compatible for Windows (CMD/ PowerShell) #106232

Closed
arye321 opened this issue Jun 29, 2023 · 6 comments
Closed

timeit basic examples are not compatible for Windows (CMD/ PowerShell) #106232

arye321 opened this issue Jun 29, 2023 · 6 comments
Labels
docs Documentation in the Doc dir OS-windows

Comments

@arye321
Copy link

arye321 commented Jun 29, 2023

Documentation

on this page
https://docs.python.org/3/library/timeit.html

the example

python3 -m timeit '"-".join(str(n) for n in range(100))'

should be

python3 -m timeit "'-'.join(str(n) for n in range(100))"

because CMD and PowerShell do not support the first format

Linked PRs

@arye321 arye321 added the docs Documentation in the Doc dir label Jun 29, 2023
@tpackard1
Copy link
Contributor

both the examples worked for me in PowerShell 7.3.4. What version of PowerShell did you try it with?

@arye321
Copy link
Author

arye321 commented Jun 29, 2023

PS Version 5.1.19041.3031
When running python3 -m timeit '"-".join(str(n) for n in range(100))'
I'm getting

option -. not recognized
use -h/--help for command line help

@arye321
Copy link
Author

arye321 commented Jun 29, 2023

Yes I just tested it on another machine, on Powershell v5.1 its not working but works on v7.3

@eryksun
Copy link
Contributor

eryksun commented Jun 29, 2023

Newer versions of PowerShell rewrite a command line that contains a single-quoted string to instead use double quotes, with literal double quote characters backslash escaped as needed. For example, '"-".join(str(n) for n in range(100))' -> "\"-\".join(str(n) for n in range(100))". This allows the C runtime in the Windows version of Python to parse the double-quoted string as a single command-line argument that contains literal double quote characters.

In contrast, the CMD shell has no special handling of single quote characters in this context. It passes the command line exactly as written to the spawned process. The C runtime in the spawned Python process has no special handling of single quote characters when parsing a command line into an argument array. For example, '"-".join(str(n) for n in range(100))' is parsed as an array of 5 arguments, ["'-.join(str(n)", 'for', 'n', 'in', "range(100))'"], which obviously won't work.

Also, most Python installations on Windows have no "python3" command. The exception, at least for official PSF releases, is the app distribution in the Microsoft Store, which enables a "python3.exe" appexec alias by default.

@terryjreedy
Copy link
Member

Examples should use the generic 'python' and work on Windows Command Prompt. People should substitute 'python' with 'python3', 'python3.y', 'py', 'py -3', 'py -3.y'. I presume " outside ' works on *nix. Working on PR.

terryjreedy added a commit to terryjreedy/cpython that referenced this issue Jun 30, 2023
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.
terryjreedy added a commit that referenced this issue Jul 1, 2023
…06296)

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.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jul 1, 2023
…s. (pythonGH-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 <tjreedy@udel.edu>
terryjreedy added a commit that referenced this issue Jul 1, 2023
…ws. (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 <tjreedy@udel.edu>
@terryjreedy
Copy link
Member

Main already has 'python', not 'python3'. However, 'python' was 'python3' in 3.11, changed in 3.12. Hence a backport would require redoing the patch.

manosriram pushed a commit to manosriram/cpython that referenced this issue Jul 1, 2023
…s. (python#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir OS-windows
Projects
None yet
Development

No branches or pull requests

4 participants