Repository navigation
Conversation
open() and Path.read_text() fall back to the platform default encoding. On Windows that is the ANSI code page (cp1252, cp1254, cp936, ...), so UTF-8 text is either rejected (UnicodeDecodeError on CJK code pages) or silently turned into different text (cp1252/cp1254): - load_js_script() reads the bundled snippets, which contain em dashes - compile_file() / C4ACompiler.compile_file() and USE includes in .c4a scripts, so TYPE "<non-ASCII>" came out as mojibake in the JS - load_config_file() / get_global_config() in the CLI - the Google search crawler's script.js Pass encoding="utf-8" at each of these call sites. The new tests run the real functions in a child interpreter with a non-UTF-8 default encoding, so they fail without the change on every platform, not only on Windows.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A few places read text files with
open()/Path.read_text()and noencoding, so they use the platform default. On Windows that is the ANSI code page (cp1252, cp1254, cp936, ...), on Linux it followsLC_ALL. The files themselves are UTF-8, so depending on the code page the read either raises or quietly returns different text.What I saw:
load_js_script(): the bundled snippets contain a few em dashes. Under a GBK localeload_js_script("flatten_shadow_dom")raisesUnicodeDecodeError: 'gbk' codec can't decode byte 0x94 in position 2648. Under cp1252/cp1254 it returns the file with mojibake in two comments.compile_file()/C4ACompiler.compile_file()andUSEincludes in.c4ascripts:TYPE "你好"read from a file compiles to JS that types浣犲ソunder a GBK locale (ä½\xa0好on cp1254), whilecompile_string()on the same text gives你好.load_config_file()andget_global_config()in the CLI: a--browser-config/--crawler-config/--extraction-configJSON or YAML file with non-ASCII values has the same problem.script.jsthe same way.This only touches reads.
List of files changed and why
crawl4ai/js_snippet/__init__.py:load_js_scriptopens the snippet as UTF-8.crawl4ai/script/c4ai_script.py:compile_fileandUSEincludes read as UTF-8.crawl4ai/script/c4a_compile.py:C4ACompiler.compile_filereads as UTF-8.crawl4ai/cli.py:get_global_configandload_config_fileread as UTF-8.crawl4ai/crawlers/google_search/crawler.py:script.jsread as UTF-8.tests/general/test_utf8_text_files.py: new tests.How Has This Been Tested?
The new tests run the real function in a child interpreter with a non-UTF-8 default (
-X utf8=0, plusLC_ALL=CandPYTHONCOERCECLOCALE=0on POSIX), so they fail without the change on any platform and not only on a Windows machine.develop, 4 passed with this change.develop, 4 passed with this change.localedef -i zh_CN -f GBK zh_CN.GBK,LC_ALL=zh_CN.GBK): ondevelop,load_js_script("flatten_shadow_dom")raises theUnicodeDecodeErrorabove andcompile_fileofTYPE "你好"produces浣犲ソ; with this change the snippet loads (3625 characters) and the compiled JS contains你好.tests/cli/test_cli.pyandtests/browser/test_init_script_dedup.pyshow the same 11 failures (17 pass) with and without this change on my machine, so I left them out of the "pass locally" box below.Checklist: