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

Tagging using git fails with UnicodeDecodeError if Python's default encoding is "cp950" #284

Closed
Glinte opened this issue Jan 26, 2025 · 2 comments · Fixed by #285
Closed

Comments

@Glinte
Copy link

Glinte commented Jan 26, 2025

@classmethod
def tag(cls, name: str, sign: bool = False, message: Optional[str] = None) -> None:
"""
Create a tag of the new_version in VCS.
If only name is given, bumpversion uses a lightweight tag.
Otherwise, it uses an annotated tag.
Args:
name: The name of the tag
sign: True to sign the tag
message: An optional message to annotate the tag.
"""
command = ["git", "tag", name]
if sign:
command += ["--sign"]
if message:
command += ["--message", message]
run_command(command)

My python's default encoding is "cp950" for some reason that I haven't had the time to check. But bump-my-version shouldn't rely on the user's encoding being utf-8 to work. I did the following to fix it for me.

- result = subprocess.run(command, text=True, check=True, capture_output=True, env=env)  # NOQA: S603
+ result = subprocess.run(command, text=True, check=True, capture_output=True, env=env, encoding="utf-8")  # NOQA: S603

Full traceback

(.venv) D:\Code\Redstone-Squid>bump-my-version bump minor
Exception in thread Thread-19 (_readerthread):
Traceback (most recent call last):
  File "C:\Python312\Lib\threading.py", line 1075, in _bootstrap_inner
    self.run()
  File "C:\Python312\Lib\threading.py", line 1012, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Python312\Lib\subprocess.py", line 1599, in _readerthread
    buffer.append(fh.read())
                  ^^^^^^^^^
UnicodeDecodeError: 'cp950' codec can't decode byte 0xe2 in position 40: illegal multibyte sequence
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "D:\Code\Redstone-Squid\.venv\Scripts\bump-my-version.exe\__main__.py", line 7, in <module>
  File "D:\Code\Redstone-Squid\.venv\Lib\site-packages\rich_click\rich_command.py", line 367, in __call__
    return super().__call__(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Code\Redstone-Squid\.venv\Lib\site-packages\click\core.py", line 1161, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Code\Redstone-Squid\.venv\Lib\site-packages\rich_click\rich_command.py", line 152, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "D:\Code\Redstone-Squid\.venv\Lib\site-packages\click\core.py", line 1697, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Code\Redstone-Squid\.venv\Lib\site-packages\click\core.py", line 1443, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Code\Redstone-Squid\.venv\Lib\site-packages\click\core.py", line 788, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Code\Redstone-Squid\.venv\Lib\site-packages\bumpversion\cli.py", line 309, in bump
    do_bump(version_part, new_version, config, found_config_file, dry_run)
  File "D:\Code\Redstone-Squid\.venv\Lib\site-packages\bumpversion\bump.py", line 121, in do_bump
    commit_and_tag(config, config_file, configured_files, ctx, dry_run)
  File "D:\Code\Redstone-Squid\.venv\Lib\site-packages\bumpversion\bump.py", line 155, in commit_and_tag
    config.scm_info.tool.tag_in_scm(config, ctx, dry_run)
  File "D:\Code\Redstone-Squid\.venv\Lib\site-packages\bumpversion\scm.py", line 230, in tag_in_scm
    cls.tag(tag_name, sign_tags, tag_message)
  File "D:\Code\Redstone-Squid\.venv\Lib\site-packages\bumpversion\scm.py", line 383, in tag
    run_command(command)
  File "D:\Code\Redstone-Squid\.venv\Lib\site-packages\bumpversion\utils.py", line 126, in run_command
    result = subprocess.run(command, text=True, check=True, capture_output=True, env=env)  # NOQA: S603
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Python312\Lib\subprocess.py", line 571, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['git', 'tag', 'v1.6.0', '--sign', '--message', 'Bump version: 1.5.7 to 1.6.0']' returned non-zero exit status 128.

Also, looking at my error from the previous issue, I think bump-my-version is generally trying to catch subprocess.CalledProcessError and reformat the output? But it seem to forgot to catch the exception here.

@Glinte Glinte changed the title Tagging using git fails with UnicodeDecodeError: 'cp950' codec can't decode byte 0xe2 in position 40 Tagging using git fails with UnicodeDecodeError if Python's default encoding is "cp950" Jan 26, 2025
@coordt
Copy link
Member

coordt commented Jan 27, 2025

I added your change, but I don't know a way to make a test specific for this situation.

Any ideas? Otherwise I'll just merge it.

@Glinte
Copy link
Author

Glinte commented Jan 27, 2025

Not sure, sorry. Changing the system default encoding isnt very feasible at runtime
https://stackoverflow.com/questions/2276200/changing-default-encoding-of-python

@coordt coordt closed this as completed in 6c856b6 Jan 30, 2025
coordt added a commit that referenced this issue Jan 30, 2025
Fixes #284. Add UTF-8 encoding to subprocess.run in run_command
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 a pull request may close this issue.

2 participants