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

Use Python's standard filesystem encoding for command-line arguments #4507

Merged
merged 4 commits into from
Oct 3, 2022

Conversation

sampsyo
Copy link
Member

@sampsyo sampsyo commented Oct 1, 2022

This is very much an experiment, but it's an attempt to address persistent issues with invoking command-line tools with non-ASCII arguments. For example, the replaygain plugin wants to invoke ffmpeg on filenames that might contain special characters. Doing this in a cross-platform way has always been extremely fraught—it's hard to get it to work right simultaneously on Unix (where filenames are plain bytes that need not obey any specific encoding) and on Windows (where filenames are weird, but they are closer to being Unicode strings).

The experiment here is to centralize on the Python standard library's implicit recommendations for this: fsencode and fsdecode. These use surrogate escaping (at least in most configurations??), so they probably shouldn't crash. The subprocess module also apparently assumes this is the right encoding for CLI argument stuff, at least on Windows:
https://github.com/python/cpython/blob/54bbb5e3363ef3634f1fd7521ba3f3c42c81a331/Lib/subprocess.py#L561

…so hopefully using this encoding everywhere should make things better on Windows and no worse on Unix.

The hope is to address some CI failures that come from the replaygain invoking ffmpeg on Windows.

The idea in this PR is to converge on Python's `fsdecode` and `fsencode`
for argument manipulation. This seems to match up with the Python
standard library's assumptions: namely, on Windows, we use `fsdecode` to
get back to Unicode strings:
https://github.com/python/cpython/blob/54bbb5e3363ef3634f1fd7521ba3f3c42c81a331/Lib/subprocess.py#L561

So let's start by dropping this utility and going straight for
`fsdecode` here to match.
This can apparently never be `None`, as of Python 3.2.
On Windows, converting command-line arguments (hopefully!!!) only needs
to deal with valid strings from the OS. So it is not really relevant to
test with non-UTF-8, non-surrogate bytes.
@sampsyo sampsyo marked this pull request as ready for review October 2, 2022 00:31
@sampsyo
Copy link
Member Author

sampsyo commented Oct 2, 2022

Incredibly, this actually seems to have addressed the ffmpeg-related errors on Windows in CI! I'm going to give this a bit of a "cooling-off" period and then merge it so we can get master back to green.

@sampsyo
Copy link
Member Author

sampsyo commented Oct 3, 2022

OK, this does seem to work—at least for our tests—and I haven't thought of a good reason it should be wrong over the last couple of days. So I'm going to merge this one, with considerable trepidation, and we'll see if anything breaks in the wild!

@sampsyo sampsyo merged commit 4a9e7c1 into master Oct 3, 2022
@sampsyo sampsyo deleted the arg-encoding-experiment branch October 3, 2022 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant