camel [BUG] 预提交许可证检查因编码问题(GBK vs UTF-8)而失败,

l7mqbcuq  于 2个月前  发布在  其他
关注(0)|答案(7)|浏览(51)

所需先决条件

您使用的camel版本是什么?

0.1.0

系统信息

import sys, camel
print(sys.version, sys.platform)

输出:
3.8.10 (tags/v3.8.10:3d8993a, May 3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)] win32

print(camel.**version**)
输出:
0.1.0

#### 问题描述

根据您的要求,以下是根据您的需求编写的示例问题:
标题:不同系统上UTF-8和GBK编码的问题
问题描述:
我目前正在使用camel版本 x.y.z 的Windows系统。当尝试使用update_license.py脚本运行预提交检查时,遇到了一个错误。这个错误似乎是由于编码不匹配引起的-尽管我的系统默认为GBK,但该脚本似乎期望文件以UTF-8编码。
这个问题特别发生在脚本尝试打开并从文件中读取时。收到的错误消息是:

UnicodeDecodeError: 'gbk' codec can't decode byte 0x9d in position 145: illegal multibyte sequence


预期的行为是让脚本成功从文件中读取并执行预提交检查。然而,由于编码不匹配,这并没有发生。

#### 可重现示例代码

**可重现示例代码**:
**Python代码片段**:

不幸的是,由于不知道您 `update_license.py` 脚本的具体内容,我只能提供可能出现问题的通用示例。问题最有可能发生在脚本尝试读取文件时:

with open("file.txt") as f:
content = f.read()


如果 `file.txt` 以UTF-8编码,但系统默认为GBK,则会引发 `UnicodeDecodeError` 。

**命令行**:

在使用 `update_license.py` 脚本运行预提交检查时遇到此问题:

python update_license.py


**额外依赖项**:

无需安装其他依赖项即可重现此问题。但是,请确保您使用的是正确版本的Python,并且已安装所有必要的软件包。

**重现步骤**:

1. 创建或准备一个以UTF-8编码的文件。
1. 在带有Python安装的Windows机器上尝试运行 `update_license.py` 脚本。
1. 当脚本尝试打开并从文件中读取时,观察 `UnicodeDecodeError` 。

#### 回溯

git -c user.useConfigOnly=true commit --quiet --allow-empty-message --file -
Format code..............................................................Passed
Sort imports.............................................................Passed
Check PEP8...............................................................Passed
Check License............................................................Failed

  • hook id: check-license
  • exit code: 1

Traceback (most recent call last):
File "\camel\licenses\update_license.py", line 118, in
update_license_in_directory(
File ""
\camel\licenses\update_license.py", line 93, in update_license_in_directory
if update_license_in_file(
^^^^^^^^^^^^^^^^^^^^^^^
File ""****\camel\licenses\update_license.py", line 42, in update_license_in_file
content = f.read()
^^^^^^^^
UnicodeDecodeError: 'gbk' codec can't decode byte 0x9d in position 145: illegal multibyte sequence

Expected behavior

Expected Behavior:

The script should successfully read from the file, regardless of the encoding used. It should handle different types of encodings without raising an error, and should carry out the pre-commit checks seamlessly.

Additional context

Potential Solution:

I suggest that the script be modified to explicitly use UTF-8 encoding when opening files, irrespective of the system defaults. This can help avoid such issues in the future, especially considering that UTF-8 is widely used across many systems and platforms.

Another option is to provide a way for users to specify the encoding that should be used by the script. This can be in the form of a command-line argument or a configuration file setting.

Impact:

This issue can disrupt workflows, especially for users working on Windows systems. It can prevent successful execution of pre-commit checks, which can lead to overlooked errors or inconsistencies in the code.

Additional Context:

This issue seems to stem from the fact that different operating systems default to different encodings. For instance, Windows defaults to GBK, while Linux and MacOS default to UTF-8. Given that UTF-8 is widely used and is a standard on many systems, it may be beneficial to align the script's encoding handling with this standard.

mspsb9vt

mspsb9vt1#

嗯...我在Windows上开发,没有遇到这个错误。我通常在Anaconda的PowerShell控制台中运行东西。

e3bfsja2

e3bfsja22#

谢谢。我在Linux上尝试了pre-commit,它也能正常工作。这可能是我的Windows环境或默认设置的问题。当我解决这个问题时,我会把反馈放在这里。

wribegjk

wribegjk3#

你好,我发现这个解决方案对我在Windows 11上有效:
要在Windows中更改默认的字符编码,你需要修改Python的区域设置。Python使用locale库来处理与区域相关的任务,如字符编码、数字和日期格式等。这是一个相对高级的操作,可能会影响你系统上的所有Python程序。
在Python 3.7及更高版本中,你可以在Windows环境中通过将PYTHONUTF8环境变量设置为1来让Python默认使用UTF-8编码。
以下是执行此操作的步骤:

  1. Win+X,然后选择System
  2. 点击About,然后在右侧选择System info
  3. 在左侧的列表中选择Advanced system settings
  4. 在系统属性对话框中,选择Environment Variables
  5. 在环境变量对话框中,点击下面的New,并在新行中输入PYTHONUTF81
    然后点击OK,关闭所有对话框。
    重启你的命令提示符或PowerShell窗口,Python将使用UTF-8作为默认的字符编码。
    请注意,这种方法会改变所有Python程序的默认编码方式。如果有些程序依赖于GBK或其他编码,可能会出现不可预测的问题。你需要确保了解此操作的影响,并知道如何恢复设置,如果出现问题的话。
y1aodyip

y1aodyip4#

这似乎与项目本身无关,但更像是在中英工作环境中贡献者的常见陷阱。也许在工作流程中引入一个Docker容器或VSCode Dev容器可以从根本上消除这些问题。

m2xkgtsf

m2xkgtsf5#

看起来与项目本身无关,但更像是在中英工作环境下贡献者的常见陷阱。也许在工作流程中引入一个Docker容器或VSCode Dev容器可以从根本上消除这些问题。
引入一个Docker容器听起来不错。感谢@kuang-da的建议!

mqxuamgl

mqxuamgl6#

你好,我发现这个解决方案对我在Windows 11上有效:
要更改Windows中的默认字符编码,您需要修改Python的区域设置。Python使用locale库来处理与区域相关的任务,如字符编码、数字和日期格式等。这是一个相对高级的操作,可能会影响您系统上的所有Python程序。
在Python 3.7及更高版本中,您可以通过将PYTHONUTF8环境变量设置为1来全局设置Python在Windows环境中默认使用UTF-8编码。
以下是执行此操作的步骤:

  1. Win+X,然后选择System
  2. 点击About,然后在右侧选择System info
  3. 在左侧的列表中选择Advanced system settings
  4. 在系统属性对话框中,选择Environment Variables
  5. 在环境变量对话框中,点击下面的New,并在新行中输入PYTHONUTF81
    然后点击OK,关闭所有对话框。
    重新启动命令提示符或PowerShell窗口,Python将使用UTF-8作为默认字符编码。
    请注意,此方法将更改所有Python程序的默认编码方法。如果某些程序依赖于GBK或其他编码,可能会出现不可预测的问题。您需要确保了解此操作的影响,并知道如何恢复设置(如果出现问题)。
    非常感谢您的方法!我也是一个Windows 11用户和具有中英文工作环境的贡献者。我遇到了相同的错误,并发现您的解决方案很有用。顺便说一下,为了暂时解决这个问题,在命令提示符或PowerShell窗口中输入' set PYTHONUTF8=1 '之前输入' git commit ... '也是一个方便的解决方案。
r55awzrz

r55awzrz7#

很高兴听到这对你有帮助。

相关问题