pyc file says "source code string cannot contain null bytes"

Viewed 188

Context

I wrote this script 1.py on macOS using vim with the encoding utf-8

import sys
sys.path += ['./modules']
from _modules._signal import *

and then compiled it using

python3 -m py_compile 1.py

The bytecode __pycache__/1.cpython-39.pyc can be executed by

python3 __pycache__/1.cpython-39.pyc

with no problem.


Problem

It is known bytecodes are cross-platform. So I sent the .pyc to Windows 10.

Here is the directory tree.

1/ #We are here at first.
└ 2/
└ 3/
└ __pycache__/
 └ 1.cpython-39.pyc

When I execute

python3 __pycache__/1.cpython-39.pyc

or

cd 2/
python3 ../__pycache__/1.cpython-39.pyc

it works (with ImportError).

However if I execute

cd 3/
python3 ../__pycache__/1.cpython-39.pyc

it unexpectedly fails. Here is the output:

Traceback (most recent call last):
  File "1.py", line 3, in <module>
ValueError: source code string cannot contain null bytes

Why? Is this a bug?

I took the same steps on macOS and Arch Linux and there was no problem. Only Windows 10 gives this error.


Environments

  • Python 3.9.4 (for macOS, Linux and Windows 10)

  • macOS Big Sur

  • Windows10 (tested cmd, powershell and Cygwin)

0 Answers
Related