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

gh-114272: fix Windows test_asyncio/test_subprocess when sys.executable contains unescaped spaces #128160

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

andyrosa2
Copy link

@andyrosa2 andyrosa2 commented Dec 21, 2024

gh-114272: Fix or skip tests that fail due to spaces in paths

Defines and uses the function get_quoted_sys_executable
if sys.executable is not quoted and it has unescaped spaces, it adds them. Win32 and Linux aware.

def get_quoted_sys_executable():
    if hasattr(get_quoted_sys_executable, '_cached_get_quoted_sys_executable'):
        return get_quoted_sys_executable._cached_get_quoted_sys_executable

    sysExecutable = sys.executable
    if not ((sysExecutable.startswith('"') and sysExecutable.endswith('"')) or 
        (sysExecutable.startswith("'") and sysExecutable.endswith("'"))):
        if ' ' in (sysExecutable if sys.platform == 'win32' else sysExecutable.replace('\\ ', '')):
            sysExecutable = f'"{sysExecutable}"'

    get_quoted_sys_executable._cached_get_quoted_sys_executable = sysExecutable
    return sysExecutable

Copy link

cpython-cla-bot bot commented Dec 21, 2024

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Dec 21, 2024

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app bedevere-app bot added tests Tests in the Lib/test dir awaiting review labels Dec 21, 2024
@picnixz picnixz changed the title Fixes test_subprocess.py Windows failures when there are unescaped spaces in sys.executable gh-114272: fix Windows test_subprocess when sys.executable contains unescapable spaces Dec 21, 2024
@picnixz picnixz changed the title gh-114272: fix Windows test_subprocess when sys.executable contains unescapable spaces gh-114272: fix Windows test_asyncio/test_subprocess when sys.executable contains unescapable spaces Dec 21, 2024
@picnixz picnixz changed the title gh-114272: fix Windows test_asyncio/test_subprocess when sys.executable contains unescapable spaces gh-114272: fix Windows test_asyncio/test_subprocess when sys.executable contains unescaped spaces Dec 21, 2024
Copy link
Contributor

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these the only tests that need modification or are there more that can be fixed?

@@ -25,12 +25,26 @@
if support.check_sanitizer(address=True):
raise unittest.SkipTest("Exposes ASAN flakiness in GitHub CI")


def get_quoted_sys_executable():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you refactor the function and use shorter names for the attributes? it's a test file so it doesn't matter what we use. Or use an LRU-cache instead. OTOH, you can setup the test cases so that they have a test attribute (namely cls.sysexec = ... in setUpClass and you'd use as self.sysexec instead of sys.executable in the tests)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi picnixz.
I think caching is overkill. I'd do it for my own code but not for opensource to keep it simple. I'll remove it.

Comment on lines +34 to +36
if not ((sysExecutable.startswith('"') and sysExecutable.endswith('"')) or
(sysExecutable.startswith("'") and sysExecutable.endswith("'"))):
if ' ' in (sysExecutable if sys.platform == 'win32' else sysExecutable.replace('\\ ', '')):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we use some smarter escape mechanism? for instance, something in os.path or shlex.quote perhaps?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.path does not add the quotes and shlex.quote quotes a quoted string ad-nauseam.
i think we should merge this have less failed tests, and somebody later (maybe me) should fix shlex.quote

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*fewer failed test
I do not know if other libraries should benefit from this . I just run python -m unittest for the first time, and the only failures are fixed by this. There are some skips, but that's also for another time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @zooba for how we should proceed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants