简体   繁体   中英

Python dis module isn't working in Python 3


Hello,
I have a Python source code file, "test.py", whose content is print("Hello World") .
When I tried to compile it to bytecode using the integrated py_compile module on python3.8, the operation was successful and the .pyc file could be run.
But if I try to read the content using the integrated dis module on python3.8 (with python3.8 -m dis {PATH} ), CPython gives me this error:

Traceback (most recent call last):
 File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
   return _run_code(code, main_globals, None,
 File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
   exec(code, run_globals)
 File "/usr/lib/python3.8/dis.py", line 553, in <module>
   _test()`
 File "/usr/lib/python3.8/dis.py", line 549, in _test
   code = compile(source, args.infile.name, "exec")
ValueError: source code string cannot contain null bytes 

The path of the source code is /home/{USER}/test.py (and C:\\Users\\{USER}\\test.py under Windows).
The path of the .pyc file is /home/{USER}/__pycache__/test.cpython-38.pyc (and C:\\Users\\{USER}\\__pycache__\\test.cpython-38.pyc under Windows).

I found this problem under these circumstances:

  • Python 3.8 and with Python 3.9 in Ubuntu (under WSL1)
  • Python 3.8 under Windows 10
  • Python 3.9 under Linux (Ubuntu 21.04).

Under WSL and under Linux, I updated all packages before the experiment.

Thanks in advance for every reply.

If you look at the source code for the dis module:

https://github.com/python/cpython/blob/3.8/Lib/dis.py#L540

you can see that the -m invocation is looking for a .py source file to compile and then disassemble in one step. It doesn't support giving it the .pyc directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM