Skip to content

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

Description

@arye321

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

Activity

  1. tpackard1 commented on Jun 29, 2023

    @tpackard1
    Contributor

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

  2. arye321 commented on Jun 29, 2023

    @arye321
    Author

    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
    
  3. arye321 commented on Jun 29, 2023

    @arye321
    Author

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

  4. eryksun commented on Jun 29, 2023

    @eryksun
    Contributor

    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.

  5. terryjreedy commented on Jun 29, 2023

    @terryjreedy
    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.

  6. added 2 commits that reference this issue on Jun 30, 2023
  7. added a commit that references this issue on Jul 1, 2023
  8. added a commit that references this issue on Jul 1, 2023
  9. terryjreedy commented on Jul 1, 2023

    @terryjreedy
    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.

  10. added a commit that references this issue on Jul 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    OS-windowsdocsDocumentation in the Doc dir

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions