Repository: RustPython/RustPython (language: rust)

Issue:
Weird message at build time
For some reason, after I pull the `main` branch after commit `4d464cc9fb3052c3e9ffb70fa739cabd952cbe41`, I got this weird message. I tried to set my `RUSTPYTHONPATH` to `/path/to/Lib` but still got this error. Here is the spec of MacOS ventura version 13.1. 

<details>
<summary>Stack trace</summary>

```text
    Finished dev [unoptimized + debuginfo] target(s) in 2.32s
     Running `target/debug/rustpython`
encodings initialization failed. Only utf-8 encoding will be supported.
Traceback (most recent call last):
  File "_frozen_importlib", line 1286, in __import__
  File "_frozen_importlib", line 1207, in _gcd_import
  File "_frozen_importlib", line 1176, in _find_and_load
  File "_frozen_importlib", line 1179, in _find_and_load
  File "_frozen_importlib", line 1153, in _find_and_load_unlocked
  File "_frozen_importlib", line 1150, in _find_and_load_unlocked
  File "_frozen_importlib", line 1150, in _find_and_load_unlocked
  File "_frozen_importlib", line 706, in _load_unlocked
  File "_frozen_importlib", line 704, in _load_unlocked
  File "_frozen_importlib", line 697, in _load_unlocked
  File "_frozen_importlib", line 691, in _load_unlocked
  File "_frozen_importlib_external", line 936, in exec_module
  File "_frozen_importlib_external", line 1071, in get_code
  File "_frozen_importlib_external", line 729, in _compile_bytecode
EOFError: marshal data too short

The above exception was the direct cause of the following exception:

RuntimeError: Could not import encodings. Is your RUSTPYTHONPATH set? If you don't have access to a consistent external environment (e.g. if you're embedding rustpython in another application), try enabling the freeze-stdlib feature
Welcome to the magnificent Rust Python 0.2.0 interpreter 😱 🖖
```
</details>


Discussion / hints:
It happens because our buils system is not good at detecting frozen lib changes.
Try clean build including removing pylib directory entirely. It helped
@coolreader18 do you have any idea why it fails to rebuild well?
I think it's not actually at build time, this kind of error can be fixed by deleting all the .pyc files at build time. We should really start bumping the bytecode version so it automatically detects that it's incompatible
omg that makes sense

Fix patch:
diff --git a/pylib/build.rs b/pylib/build.rs
index 2678b92dca..1ea80e25a5 100644
--- a/pylib/build.rs
+++ b/pylib/build.rs
@@ -1,11 +1,11 @@
 fn main() {
-    rerun_if_changed("../Lib/python_builtins/*");
+    process_python_libs("../Lib/python_builtins/*");
 
     #[cfg(not(feature = "stdlib"))]
-    rerun_if_changed("../Lib/core_modules/*");
+    process_python_libs("../Lib/core_modules/*");
 
     #[cfg(feature = "stdlib")]
-    rerun_if_changed("../../Lib/**/*");
+    process_python_libs("../../Lib/**/*");
 
     if cfg!(windows) {
         if let Ok(real_path) = std::fs::read_to_string("Lib") {
@@ -14,7 +14,8 @@ fn main() {
     }
 }
 
-fn rerun_if_changed(pattern: &str) {
+// remove *.pyc files and add *.py to watch list
+fn process_python_libs(pattern: &str) {
     let glob = glob::glob(pattern).unwrap_or_else(|e| panic!("failed to glob {pattern:?}: {e}"));
     for entry in glob.flatten() {
         if entry.is_dir() {
@@ -22,6 +23,9 @@ fn rerun_if_changed(pattern: &str) {
         }
         let display = entry.display();
         if display.to_string().ends_with(".pyc") {
+            if std::fs::remove_file(&entry).is_err() {
+                println!("cargo:warning=failed to remove {display}")
+            }
             continue;
         }
         println!("cargo:rerun-if-changed={display}");


Test patch: