Skip to content

[u2c.py] Fix unicode files argument handling for Python 2.7 #388

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

Merged
merged 1 commit into from
Aug 1, 2025

Conversation

KevinXuxuxu
Copy link
Contributor

@KevinXuxuxu KevinXuxuxu commented Aug 1, 2025

As mentioned in https://stackoverflow.com/questions/22947181/dont-argparse-read-unicode-from-commandline it is a known issue with Python 2.7 and argparse because Python 2.7 treats sys.argv as a list of byte strings, not Unicode strings, while Python 3 does. So when passing in files which contains unicode (Chinese, Japanese...) results in the following error:

$ ~/u2c.py -a wark copyparty.domain.name/movies/ 飞黄腾达.mp4 
/share/homes/xu/u2c.py:1520: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  if "--version" in sys.argv:
/share/homes/xu/u2c.py:1524: UnicodeWarning: Unicode unequal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  sys.argv = [x for x in sys.argv if x != "--ws"]
usage: u2c.py [-h] [-v] [-a PASSWD] [-s] [-x REGEX] [--ok] [--touch] [--ow]
               [--owo] [--spd] [--version] [-u] [-ud] [-uf PATH] [--cls]
               [--rh TRIES] [--dl] [--dr] [--drd] [--wsalt S] [--chs] [--jw]
               [-j CONNS] [-J CORES] [--sz MiB] [--szm MiB] [-nh] [-ns]
               [--cxp SEC] [--cd SEC] [--safe] [-z] [-te PATH] [-td]
               url files [files ...]
u2c.py: error: argument files: invalid unicode value: '\xe9\xa3\x9e\xe9\xbb\x84\xe8\x85\xbe\xe8\xbe\xbe.mp4'

This PR fixes this by explicitly specifying encoding when handling files arg. It's only effective for Python 2, no behavior change is expected for Python 3.

Note that this issue might happen to other arguments using type=unicode (e.g. url) but I suppose fixing files arg should be the most useful case. I haven't tested for other args yet.

This PR complies with the DCO; https://developercertificate.org/

@9001 9001 merged commit 9c19753 into 9001:hovudstraum Aug 1, 2025
@9001
Copy link
Owner

9001 commented Aug 1, 2025

good catch! thanks :>

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.

2 participants