Skip to main content

用于解析、编辑和处理Python字节码(bytecode)的工具。A tool for parsing and processing Python bytecode object.

Project description

pycodeobject库是一个用于解析、编辑和处理Python字节码(bytecode)的工具。

PyCodeObject is a tool for parsing and processing Python bytecode object.

详细介绍

字节码的结构

众所周知, Python中的字节码(bytecode) 是一种数据类型, Python代码的编译结果就是bytecode对象。

bytecode对象可以由虚拟机加载后直接运行,而pyc文件就是bytecode在硬盘上的保存形式。 先通过一个示例, 分析什么是字节码:

.. code-block:: python
>>> import dis
>>> def f(x):print('hello',x)
>>> f.__code__
<code object f at 0x02B27498, file "<pyshell#2>", line 1>
>>> f.__code__.co_code
b't\x00d\x01|\x00\x83\x02\x01\x00d\x00S\x00'
>>> dis.dis(f)
  1           0 LOAD_GLOBAL              0 (print)
              2 LOAD_CONST               1 ('hello')
              4 LOAD_FAST                0 (x)
              6 CALL_FUNCTION            2
              8 POP_TOP
             10 LOAD_CONST               0 (None)
             12 RETURN_VALUE
>>>

上述示例中f.__code__就是bytecode对象, f.__code__.co_code就是二进制的字节码, 通过dis模块可以反编译、分析这些二进制代码。

Python解释执行代码时, 会首先将原始的源代码翻译成bytecode形式, 再直接执行bytecode, 以提高性能。

字节码结构图

(在 Python 3.8中, 增加了一个属性 co_posonlyargcount, 而Python 3.11的字节码有较大的改动)

详尽的说明参见Python官方文档 https://docs.python.org/zh-cn/3.7/library/dis.html

包装字节码

在python中, bytecode对象的属性是不可修改的。如:

.. code-block:: python
>>> def f():pass
>>> f.__code__.co_code = b''
Traceback (most recent call last): ... ...
AttributeError: readonly attribute

为了使bytecode对象更易用, 本项目中的Code类, 用于包装 (wrap)字节码对象。

下面的示例是从doctest中摘取的:

.. code-block:: python
>>> def f():print("Hello")
>>> c=Code.fromfunc(f) # 或 c=Code(f.__code__)
>>> c.co_consts
(None, 'Hello')
>>> c.co_consts=(None, 'Hello World!')
>>> c.exec()
Hello World!
>>>
>>> import os,pickle
>>> temp=os.getenv('temp')
>>> with open(os.path.join(temp,"temp.pkl"),'wb') as f:
...     pickle.dump(c,f)
...
>>> f=open(os.path.join(temp,"temp.pkl"),'rb')
>>> pickle.load(f).to_func()()
Hello World!
>>>
>>> c.to_pycfile(os.path.join(temp,"temppyc.pyc"))
>>> sys.path.append(temp)
>>> import temppyc
Hello World!
>>> Code.from_pycfile(os.path.join(temp,"temppyc.pyc")).exec()
Hello World!

版本 VERSION

1.0.5 (更新: 增加了对3.10及以上版本的支持。)

本模块与pyobject库的关系

pycodeobject库最初是作者单独开发的项目。后来因pyobject库的开发,pycodeobject库被整合到了pyobject.code_模块中。现今的pyobject的Code类和本模块的Code类完全相同。

作者 AUTHOR

qfcy qq:3076711200 贴吧账号:qfcy_

作者CSDN主页: https://blog.csdn.net/qfcy_

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pycodeobject-1.0.5.tar.gz (13.3 kB view details)

Uploaded Source

File details

Details for the file pycodeobject-1.0.5.tar.gz.

File metadata

  • Download URL: pycodeobject-1.0.5.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.8

File hashes

Hashes for pycodeobject-1.0.5.tar.gz
Algorithm Hash digest
SHA256 f2105dfe9d03fc86d9f2169ca30a6e318cd2d5041020558094b778cb3f42691a
MD5 339fdebc48ac211f141a74de61675652
BLAKE2b-256 2f82c0966747f1c8a51a046b50d9f779efa985a88a2c3673b9270b1a26502cfe

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page