From defe8ede9c84fa7c08f8723fccf6c8e6a1c72139 Mon Sep 17 00:00:00 2001 From: dltsum Date: Sat, 19 Sep 2026 17:36:19 +0800 Subject: [PATCH 1/2] Pin utf-8 in add-new-model-like file IO (Windows non-UTF-8 locale fix) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On Windows with a non-UTF-8 locale (e.g. cp936), the CLI crashes writing the generated doc file whose header contains '⚠️': src/transformers/cli/add_new_model_like.py:599 in add_new_model_like E UnicodeEncodeError: 'gbk' codec can't encode character '\u26a0' All generated/read repo files are UTF-8; pin the encoding on every open() in the command instead of relying on the locale. Co-Authored-By: Claude Code --- src/transformers/cli/add_new_model_like.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/transformers/cli/add_new_model_like.py b/src/transformers/cli/add_new_model_like.py index b46d29a9cc12..203035ee7c80 100644 --- a/src/transformers/cli/add_new_model_like.py +++ b/src/transformers/cli/add_new_model_like.py @@ -330,7 +330,7 @@ def insert_model_in_doc_toc( The fully cased name (as in the official paper name) of the new model. """ toc_file = repo_path / "docs" / "source" / "en" / "_toctree.yml" - with open(toc_file, "r") as f: + with open(toc_file, "r", encoding="utf-8") as f: content = f.read() toc_match = re.search(rf"- local: model_doc/{old_lowercase_name}\n {{8}}title: .*?\n", content) @@ -514,7 +514,7 @@ def create_test_files( # Sometimes, tests may not exist if not original_test_path.is_file(): continue - with open(original_test_path, "r") as f: + with open(original_test_path, "r", encoding="utf-8") as f: test_code = f.read() # Remove old copyright and add new one test_lines = test_code.split("\n") @@ -564,12 +564,12 @@ def _add_new_model_like_internal( modular_file, public_classes = create_modular_file( repo_path, old_model_infos, new_lowercase_name, filenames_to_add ) - with open(new_module_folder / f"modular_{new_lowercase_name}.py", "w") as f: + with open(new_module_folder / f"modular_{new_lowercase_name}.py", "w", encoding="utf-8") as f: f.write(modular_file) # 3. Create and add the __init__.py init_file = create_init_file(old_lowercase_name, new_lowercase_name, filenames_to_add) - with open(new_module_folder / "__init__.py", "w") as f: + with open(new_module_folder / "__init__.py", "w", encoding="utf-8") as f: f.write(init_file) # 4. Add new model to the models init @@ -586,16 +586,16 @@ def _add_new_model_like_internal( tests_folder = repo_path / "tests" / "models" / new_lowercase_name os.makedirs(tests_folder, exist_ok=True) # Add empty __init__.py - with open(tests_folder / "__init__.py", "w"): + with open(tests_folder / "__init__.py", "w", encoding="utf-8"): pass test_files = create_test_files(repo_path, old_model_infos, new_lowercase_name, filenames_to_add) for filename, content in test_files.items(): - with open(tests_folder / filename, "w") as f: + with open(tests_folder / filename, "w", encoding="utf-8") as f: f.write(content) # 7. Add doc file doc_file = create_doc_file(new_model_paper_name, public_classes) - with open(repo_path / "docs" / "source" / "en" / "model_doc" / f"{new_lowercase_name}.md", "w") as f: + with open(repo_path / "docs" / "source" / "en" / "model_doc" / f"{new_lowercase_name}.md", "w", encoding="utf-8") as f: f.write(doc_file) insert_model_in_doc_toc(repo_path, old_lowercase_name, new_lowercase_name, new_model_paper_name) From 04fe2ac7ca6d29325c6a48f9e6c9c04ce7e5fad3 Mon Sep 17 00:00:00 2001 From: dltsum Date: Sat, 19 Sep 2026 19:46:51 +0800 Subject: [PATCH 2/2] Style: ruff format Co-Authored-By: Claude Code --- src/transformers/cli/add_new_model_like.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/transformers/cli/add_new_model_like.py b/src/transformers/cli/add_new_model_like.py index 203035ee7c80..dc8cc7cf5f09 100644 --- a/src/transformers/cli/add_new_model_like.py +++ b/src/transformers/cli/add_new_model_like.py @@ -595,7 +595,9 @@ def _add_new_model_like_internal( # 7. Add doc file doc_file = create_doc_file(new_model_paper_name, public_classes) - with open(repo_path / "docs" / "source" / "en" / "model_doc" / f"{new_lowercase_name}.md", "w", encoding="utf-8") as f: + with open( + repo_path / "docs" / "source" / "en" / "model_doc" / f"{new_lowercase_name}.md", "w", encoding="utf-8" + ) as f: f.write(doc_file) insert_model_in_doc_toc(repo_path, old_lowercase_name, new_lowercase_name, new_model_paper_name)