Skip to main content

用于解析、编辑Python 字节码(bytecode)的工具。A tool which parses and edits Python bytecode object.

Project description

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

Pycodeobject is a tool which parses and edits 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官方文档 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.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.4

作者 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.4.tar.gz (10.2 kB view hashes)

Uploaded Source

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