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官方文档 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

与pyobject库的关系

pycodeobject库最初是作者单独开发的项目。后来因pyobject库的开发,pycodeobject库被合并到了pyobject.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.4.2.tar.gz (12.9 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: pycodeobject-1.0.4.2.tar.gz
  • Upload date:
  • Size: 12.9 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.4.2.tar.gz
Algorithm Hash digest
SHA256 b4ff09bb23463ad01ba682f1ae8ae1b9d46e0525c106944c9648ef9faf403e88
MD5 cfc65f605032653b67be6962cc9b9cc4
BLAKE2b-256 f788e21817428b0fcdaca25d6ba099a4324e4bbc44527216f748a3d56e7bf6ae

See more details on using hashes here.

Provenance

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