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

argparse: Check if stderr is defined before writing to it #101640

Closed
simaoafonso-pwt opened this issue Feb 7, 2023 · 8 comments
Closed

argparse: Check if stderr is defined before writing to it #101640

simaoafonso-pwt opened this issue Feb 7, 2023 · 8 comments
Assignees
Labels
OS-windows pending The issue will be closed if no feedback is provided stdlib Python modules in the Lib dir topic-IO type-bug An unexpected behavior, bug, or error

Comments

@simaoafonso-pwt
Copy link

simaoafonso-pwt commented Feb 7, 2023

Bug report

argparse tries to write to stderr inconditionally, even when it's None. This is only a problem on Windows. Here:

https://github.com/python/cpython/blob/3.11/Lib/argparse.py#L2596-L2600

I detected this when using PyInstaller to create a binary on Windows using --noconsole, which triggers using pythonw.exe. In this case, sys.stderr is None.

This is similar to #89057

Your environment

  • Any version of Python on Windows, up to 3.11

Linked PRs

@simaoafonso-pwt simaoafonso-pwt added the type-bug An unexpected behavior, bug, or error label Feb 7, 2023
@arhadthedev arhadthedev added OS-windows stdlib Python modules in the Lib dir topic-IO labels Feb 7, 2023
@terryjreedy terryjreedy changed the title argparse help: Check if stderr is defined before writing to it argparse: Check if stderr is defined before writing to it May 6, 2023
terryjreedy added a commit that referenced this issue May 6, 2023
* In particular, don't exit when trying to print to stderr = None.
* Add tests

Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 6, 2023
…ythonGH-101802)

* In particular, don't exit when trying to print to stderr = None.
* Add tests

(cherry picked from commit 42f54d1)

Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
@terryjreedy terryjreedy self-assigned this May 6, 2023
@terryjreedy terryjreedy added the pending The issue will be closed if no feedback is provided label May 6, 2023
@terryjreedy
Copy link
Member

terryjreedy commented May 6, 2023

@simaoafonso-pwt The title of the 3.11 backport says specifically what was done. Without a stacktrace, we cannot be sure that this was the site of the error. And even if it was, whether this is sufficient to fix this issue for you. If you can, please make a new binary (after the 3.11 merge) and see if it now works. And report here so we can either close or look further (given a traceback and message).

terryjreedy added a commit that referenced this issue May 6, 2023
…H-101802) (#104250)

gh-101640: Make argparse _print_message catch any write error (GH-101802)

* In particular, don't exit when trying to print to stderr = None.
* Add tests

(cherry picked from commit 42f54d1)

Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
@simaoafonso-pwt
Copy link
Author

Sorry, is there some kind of nightly build I could use to test this?
From what I can tell, none of the alpha versions include the changes, either 3.11 or the main branch.

Compiling Python on Windows is not something I'm very comfortable with.

jbower-fb pushed a commit to jbower-fb/cpython-jbowerfb that referenced this issue May 8, 2023
…ython#101802)

* In particular, don't exit when trying to print to stderr = None.
* Add tests

Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
@arhadthedev
Copy link
Member

arhadthedev commented May 9, 2023

Sorry, is there some kind of nightly build I could use to test this?

You can follow https://devguide.python.org/getting-started/ to get the latest sources and build them.

The prebuilt executable will be availible as CPython 3.12b1 on May 22.

@simaoafonso-pwt
Copy link
Author

simaoafonso-pwt commented May 30, 2023

The prebuilt executable will be availible as CPython 3.12b1 on May 22.

I tried to check this, but it seems PyInstaller is not yet 3.12-ready, it might take a longer time: pyinstaller/pyinstaller#7601

@terryjreedy
Copy link
Member

I am closing for now. This can be reopened or a new issue opened if later found necessary.

@terryjreedy
Copy link
Member

Compiling on Windows is pretty easy now if one has git installed. Clone python/cpython. Install VS 2022 with python extension exactly as detailed in devguide. Give command line, I believe PCbuild\build.bat -e.

@simaoafonso-pwt
Copy link
Author

This is already closed, but only now I got time to check this, now that PyInstaller published wheels for 3.12, and can confirm this is fixed in 3.12b1. Thanks.

@simaoafonso-pwt
Copy link
Author

simaoafonso-pwt commented Sep 28, 2023

FYI, in case you need a workaround for this issue on lower Python versions, you can use this:

# Workaround for https://github.com/python/cpython/issues/101640
if sys.version_info >= (3, 12):
    ArgumentParser = argparse.ArgumentParser
else:
    class ArgumentParser(argparse.ArgumentParser):
        def _print_message(self, message, file=None):
            if file is None:
                pass  # Don't print if the file is unavailable
            else:
                super()._print_message(message, file=file)

Then use ArgumentParser instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OS-windows pending The issue will be closed if no feedback is provided stdlib Python modules in the Lib dir topic-IO type-bug An unexpected behavior, bug, or error
Projects
Status: Doc issues
Development

No branches or pull requests

3 participants