一个提供操作Python对象底层工具的模块。A utility tool with some submodules for operating internal python objects.
Project description
pyobject - 一个提供操作Python对象底层工具的Python包, 包含一些子模块。A utility tool with some submodules for operating internal python objects.
The English introduction is shown below the Chinese version.
所包含模块:
pyobject.__init__ - 显示和输出Python对象的各个属性值
pyobject.browser - 以图形界面浏览Python对象
pyobject.code_ - Python 字节码(bytecode)的操作工具
pyobject.search - 以一个起始对象为起点,查找和搜索能到达的所有python对象
pyobject.newtypes - 定义一些新的类型 (实验性)
pyobj_extension - C扩展模块, 提供操作Python对象底层的函数
包含的函数:
describe(obj, level=0, maxlevel=1, tab=4, verbose=False, file=sys.stdout):
"描述"一个对象,即打印出对象的各个属性。 参数说明: maxlevel:打印对象属性的层数。 tab:缩进的空格数,默认为4。 verbose:一个布尔值,是否打印出对象的特殊方法(如__init__)。 file:一个类似文件的对象,用于打印输出。
browse(object, verbose=False, name=’obj’):
以图形方式浏览一个Python对象。 verbose:与describe相同,是否打印出对象的特殊方法(如__init__)
函数browse()的图形界面如下所示 (中文界面的版本可在模块目录内的test/browser_chs_locale.py中找到):
objectname(obj):
objectname(obj) - 返回一个对象的名称,形如xxmodule.xxclass。 如:objectname(int) -> 'builtins.int'
bases(obj, level=0, tab=4):
bases(obj) - 打印出该对象的基类 tab:缩进的空格数,默认为4。
用于对象搜索的函数:
make_list(start_obj, recursions=2, all=False):
创建一个对象的列表, 列表中无重复的对象。 start:开始搜索的对象 recursion:递归次数 all:是否将对象的特殊属性(如__init__)加入列表
make_iter(start_obj, recursions=2, all=False):
功能、参数与make_list相同, 但创建迭代器, 且迭代器中可能有重复的对象。
search(obj, start, recursions=3):
从一个起点开始搜索对象 obj:待搜索的对象 start:起点对象 recursion:递归次数
类: pyobject.Code
类Code用于包装Python字节码对象(bytecode),提供一个便利操作Python字节码的接口。
Python底层的bytecode对象,如func.__code__,是不可变的,鉴于此,Code类提供了一个可变的字节码对象,以及一系列操作字节码的函数,使得操作底层字节码对象变得更容易。
示例用法: (从模块的doctest中摘取):
>>> 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!
模块: pyobj_extension
本模块使用了C语言编写。可直接使用import pyobj_extension, 导入该独立模块。其中包含的函数如下:
convptr(pointer):
将整数指针转换为Python对象,与id()相反。
py_decref(object, n):
将对象的引用计数减小1。
py_incref(object, n):
将对象的引用计数增加1。
getrealrefcount(obj):
获取调用本函数前对象的实际引用计数。和sys.getrefcount()不同,不考虑调用时新增的引用计数。 如:getrealrefcount([])会返回0,因为退出getrealrefcount后列表[]不再被任何对象引用,而sys.getrefcount([])会返回1。 另外,a=[];getrealrefcount(a)会返回1而不是2。
setrefcount(obj, n):
设置对象的实际引用计数(调用函数前)为n,和getrealrefcount()相反,同样不考虑调用时新增的引用计数。
警告: 不恰当地调用这些函数可能导致Python崩溃。
list_in(obj, lst):
判断obj是否在列表或元组lst中。与Python内置的obj in lst调用多次==运算符(__eq__)相比, 本函数直接比较对象的指针,提高了效率。
Submodules:
pyobject.__init__ - Displays and outputs attribute values of Python objects.
pyobject.browser - Provides a visual interface to browse Python objects.
pyobject.code_ - Provides tools for manipulating Python native bytecode.
pyobject.search - Implements the utility for locating the path to a specific object.
pyobject.newtypes - Defines a few new types. (Experimental)
pyobj_extension - A C extension module offering functions to manipulate low-level Python objects.
Functions:
describe(obj, level=0, maxlevel=1, tab=4, verbose=False, file=sys.stdout):
"Describes" an object by printing its attributes. Parameters: - maxlevel: The depth of attribute levels to print. - tab: Number of spaces for indentation, default is 4. - verbose: Boolean indicating whether to print special methods (e.g., __init__). - file: A file-like object for output.
browse(object, verbose=False, name=’obj’):
Graphically browse a Python object. - verbose: Same as in describe, whether to print special methods.
The graphical interface of the browse() function is shown below:
objectname(obj):
Returns the name of an object in the format xxmodule.xxclass. Example: objectname(int) -> 'builtins.int'.
bases(obj, level=0, tab=4):
Prints the base classes of the object. - tab: Number of spaces for indentation, default is 4.
Functions for searching objects:
make_list(start_obj, recursions=2, all=False):
Creates a list of objects without duplicates. - start: The object to start searching from. - recursion: Number of recursions. - all: Whether to include special attributes (e.g., __init__) in the list.
make_iter(start_obj, recursions=2, all=False):
Similar to make_list, but creates an iterator, which may contain duplicates.
search(obj, start, recursions=3):
Searches for objects starting from a specified point. - obj: The object to search for. - start: The starting object. - recursion: Number of recursions.
Class: pyobject.Code
The Code class wraps Python bytecode objects, providing a convenient interface for manipulation.
Python’s underlying bytecode objects, such as func.__code__, are immutable. The Code class offers a mutable bytecode object and a suite of functions to manipulate bytecode, simplifying operations on these objects.
Example usage: (excerpted from the module’s doctest):
>>> def f():print("Hello") >>> c=Code.fromfunc(f) # or 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!
Module: pyobj_extension
This module is written in C and can be imported directly using import pyobj_extension. It includes the following functions:
convptr(pointer):
Converts an integer pointer to a Python object, as a reverse of id().
py_decref(obj):
Decreases the reference count of an object.
py_incref(obj):
Increases the reference count of an object.
getrealrefcount(obj):
Get the actual reference count of the object before calling this function. Unlike sys.getrefcount(), this function does not consider the additional reference count that is created when the function is called. For example, getrealrefcount([]) will return 0, because after exiting getrealrefcount, the list [] is no longer referenced by any object, whereas sys.getrefcount([]) will return 1. Additionally, a=[]; getrealrefcount(a) will return 1 instead of 2.
setrefcount(obj, n):
Set the actual reference count of the object (before calling the function) to n. This is the opposite of getrealrefcount() and also does not consider the additional reference count created when the function is called.
Warning: Improper use of these functions above may lead to crashes.
list_in(obj, lst):
Determine whether obj is in the sequence lst. Compared to the built-in Python call "obj in lst" that invokes the "==" operator (__eq__) multiple times, this function directly compares the pointers to improve efficiency.
版本 Version:1.2.5
更新日志:
2024-10-24(v1.2.5):修复了pyobject.browser在Windows下的高DPI支持,修改了pyobj_extension模块,以及其他改进。
2024-8-12(v1.2.4):针对pyobject.code_增加了对3.10及以上版本的支持;进一步优化了search模块的搜索性能,以及一些其他修复和改进。
2024-6-20(v1.2.3):更新了包内test目录下的.pyc文件加壳工具,并更新了pyobject.browser中的对象浏览器,添加了显示列表和字典项,后退、前进、刷新页面,以及新增、编辑和删除项等新特性。
2022-7-25(v1.2.2):增加了操作Python底层对象引用, 以及对象指针的C语言模块pyobj_extension。
2022-2-2(v1.2.0):修复了一些bug,优化了search模块的性能; code_中增加了Code类, browser中增加编辑属性功能, 增加了Code类的doctest。
源码:见 https://github.com/qfcy/pyobject
作者 Author: 七分诚意 qq:3076711200
作者CSDN主页: https://blog.csdn.net/qfcy_/
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
File details
Details for the file pyobject-1.2.5.tar.gz
.
File metadata
- Download URL: pyobject-1.2.5.tar.gz
- Upload date:
- Size: 51.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5fe54edc29b0cde42ac572f5e85f1720528a0ce086272304b75cca9682020235 |
|
MD5 | 80ef9e88c484a51fd2021c516a715db8 |
|
BLAKE2b-256 | 414a1ea5c1413e7c30a1f7c9b6f76ed86c7c68d5bdfc3b900dcf3f82c9f16ce5 |
File details
Details for the file pyobject-1.2.5-cp314-cp314-win_amd64.whl
.
File metadata
- Download URL: pyobject-1.2.5-cp314-cp314-win_amd64.whl
- Upload date:
- Size: 65.0 kB
- Tags: CPython 3.14, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cbd06eabcf5a734985199e477a88353915a205d442b750f90638585fef308c76 |
|
MD5 | cf97ef99ed102f6d55f73aee628e2877 |
|
BLAKE2b-256 | 5cc85c05c5986a7a6539a6ed5c353288d504c22e06481a432de84427664ff56c |
File details
Details for the file pyobject-1.2.5-cp314-cp314-win32.whl
.
File metadata
- Download URL: pyobject-1.2.5-cp314-cp314-win32.whl
- Upload date:
- Size: 64.6 kB
- Tags: CPython 3.14, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a71f47db7748bf7408f5c0da9e45d66bfb1c452af43d263096d08984341c0621 |
|
MD5 | 8c0a45bb761cbf11813bf611ed103bb5 |
|
BLAKE2b-256 | 6adbe33a1c790ac32d787fb98c24370efbe6adcc09ba297b711e8ce625ec0836 |
File details
Details for the file pyobject-1.2.5-cp313-cp313-win_amd64.whl
.
File metadata
- Download URL: pyobject-1.2.5-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 65.0 kB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 05152a43b939226f0e618912f0ed7cf59dfa676dc1df59f73787dde1783ab49e |
|
MD5 | 109a2a1e059bf08c25ebb09d0cb97e55 |
|
BLAKE2b-256 | ee6e37974ff673ab45561e37dd67bc8e433440aecaef5a2db06b0585b999b41b |
File details
Details for the file pyobject-1.2.5-cp313-cp313-win32.whl
.
File metadata
- Download URL: pyobject-1.2.5-cp313-cp313-win32.whl
- Upload date:
- Size: 64.6 kB
- Tags: CPython 3.13, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95869c0c724573642d7beb944b3796be41693e30c1ca48d9f7d45385eef3942b |
|
MD5 | 3fea85a430cc5af98791e6c4c04f5ca5 |
|
BLAKE2b-256 | 050bac77cdc7be97c3f6835b1fd5e7588fc1057164f3bf958b85018eec76df39 |
File details
Details for the file pyobject-1.2.5-cp312-cp312-win_amd64.whl
.
File metadata
- Download URL: pyobject-1.2.5-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 65.0 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0bf2e38cd85eafa8758b6e64a33a4aaeb801f79650f7b6a2780447d265352996 |
|
MD5 | f9bec12260b34a578f63982515b504f5 |
|
BLAKE2b-256 | 70fbdb4c4b5017609a1b9ef21117ef6277a87952e45a23556499bf5b5a7cb590 |
File details
Details for the file pyobject-1.2.5-cp312-cp312-win32.whl
.
File metadata
- Download URL: pyobject-1.2.5-cp312-cp312-win32.whl
- Upload date:
- Size: 64.6 kB
- Tags: CPython 3.12, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e89c329dbd95611d24d191340c584b3d518420adc1b3850832b2ad4cf7584d3c |
|
MD5 | 743c7477d1d75be906a5dfa60e933eff |
|
BLAKE2b-256 | dcf3c68d7089f314d0e49d22d22f1607c381b77870e1483bd69de151a11d9459 |
File details
Details for the file pyobject-1.2.5-cp311-cp311-win_amd64.whl
.
File metadata
- Download URL: pyobject-1.2.5-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 65.0 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 09c1150a098e362d408c3f9f1de9ded9a2cf359df013e5d398a345d33b21b6b6 |
|
MD5 | ecc8742c98926528fa6d4148efe87dcd |
|
BLAKE2b-256 | d94a606ef93ed7ae0005a97a178ed7a4fe1c04179009aded3fcc585e75e74dd7 |
File details
Details for the file pyobject-1.2.5-cp311-cp311-win32.whl
.
File metadata
- Download URL: pyobject-1.2.5-cp311-cp311-win32.whl
- Upload date:
- Size: 64.5 kB
- Tags: CPython 3.11, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b5e323b385b3ed0ffb43ae316a82cfaff5caa909420bf9f09d25f5dd50b9f63 |
|
MD5 | 11917937bc9feef0c98758115e3cdfc3 |
|
BLAKE2b-256 | 4df520ad811d384a1b75669fbb0964fec315412cc26c41d47d3935aaccaba64d |
File details
Details for the file pyobject-1.2.5-cp310-cp310-win_amd64.whl
.
File metadata
- Download URL: pyobject-1.2.5-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 65.1 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ca20534fcfdad3329a4ce8f8c47f1770c3065e258d5cf5178770a3d8f1340d0 |
|
MD5 | 18c16fa61be1b025cdb911150865dff5 |
|
BLAKE2b-256 | 931cc0ee88d2fa1e1e2d83b06ba5530cea757d5d35f0644561c5ccaaea01c9ef |
File details
Details for the file pyobject-1.2.5-cp310-cp310-win32.whl
.
File metadata
- Download URL: pyobject-1.2.5-cp310-cp310-win32.whl
- Upload date:
- Size: 64.5 kB
- Tags: CPython 3.10, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e7d30b46f8cc4b921da976ceca38a6f2c0eeedaf324a7360637a0286d5c2c2b1 |
|
MD5 | bf22c433ee0991891e071a794eb0d7ec |
|
BLAKE2b-256 | aa0a80a54a14f89d77e906458d8f7a066e7d03987d76587b9f9c5ddcf31ae7fe |
File details
Details for the file pyobject-1.2.5-cp39-cp39-win_amd64.whl
.
File metadata
- Download URL: pyobject-1.2.5-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 65.0 kB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef1295f8beca25157172b376329d20b7612886638cb614fb8f07c676806b7b18 |
|
MD5 | 0715d671b3830c271292030426ad1278 |
|
BLAKE2b-256 | f60da7097ace5f1d5821ab4d004e63df6ac2ee0fb06613851f2c638b7dbb7e11 |
File details
Details for the file pyobject-1.2.5-cp39-cp39-win32.whl
.
File metadata
- Download URL: pyobject-1.2.5-cp39-cp39-win32.whl
- Upload date:
- Size: 64.5 kB
- Tags: CPython 3.9, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4f8b9fd9b78dae55caf8f280a99d748dfa481372ca39d77e7644d2cffc8cdfc3 |
|
MD5 | a57883086bf32d0a8de7964832985a81 |
|
BLAKE2b-256 | 6cfa583380edcfcd4b3ee83d734d233ea997aa844330d4cbdff1b5650d5ea793 |
File details
Details for the file pyobject-1.2.5-cp38-cp38-win_amd64.whl
.
File metadata
- Download URL: pyobject-1.2.5-cp38-cp38-win_amd64.whl
- Upload date:
- Size: 65.0 kB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e08c8ecce05af80494814381d91c9509c6b166e4e42c0071b1931824ae5773b5 |
|
MD5 | cd98303a7bacccc1fa2a56e3ac188838 |
|
BLAKE2b-256 | f55db1e3065e8b6d3b162d43472b89d4a2b65a5a107a7672d5b0d522633fb174 |
File details
Details for the file pyobject-1.2.5-cp38-cp38-win32.whl
.
File metadata
- Download URL: pyobject-1.2.5-cp38-cp38-win32.whl
- Upload date:
- Size: 64.6 kB
- Tags: CPython 3.8, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f54e56c23520c7e40b82639275d8427cfe0d6425c734f9155b1b42c8dc06080 |
|
MD5 | 583a859e4237c1ec8c511bc29b984fea |
|
BLAKE2b-256 | cbd0ec350963ec80fa004df1482383e536420b55b11b6b62b331ca5cd51a6a7d |
File details
Details for the file pyobject-1.2.5-cp37-cp37m-win_amd64.whl
.
File metadata
- Download URL: pyobject-1.2.5-cp37-cp37m-win_amd64.whl
- Upload date:
- Size: 65.0 kB
- Tags: CPython 3.7m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bbf20caec285c2c0ab901b8c99f80609a3db5363b9d14435a43b11ec75d7b821 |
|
MD5 | c094f4a120f8a4a5e55e01e4586e755c |
|
BLAKE2b-256 | 6a9d6d464978dab6e6b9bde4e6aae0e07cfc141c84ef4687a4949ac9cb37eaae |
File details
Details for the file pyobject-1.2.5-cp37-cp37m-win32.whl
.
File metadata
- Download URL: pyobject-1.2.5-cp37-cp37m-win32.whl
- Upload date:
- Size: 64.5 kB
- Tags: CPython 3.7m, Windows x86
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f3dae0362dc7e41e41e399d57533c294886789cd401f9ef74e7e254b1cee6e25 |
|
MD5 | b570b16bb527ad259fb68b7c696a4d20 |
|
BLAKE2b-256 | 0439c7215b52c9001c614ee20bc6d550a1d9ca509904e2d741aa6997862f63ee |
File details
Details for the file pyobject-1.2.5-cp36-cp36m-win_amd64.whl
.
File metadata
- Download URL: pyobject-1.2.5-cp36-cp36m-win_amd64.whl
- Upload date:
- Size: 65.1 kB
- Tags: CPython 3.6m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5ab29ad2a9a63d3acdbaa8377d35d381dcced0fdd0560efec5b9b0db76496d07 |
|
MD5 | 2b08cebc8bd28b8a12b7f846dfebfc70 |
|
BLAKE2b-256 | e5b949e7217f78444d966523466b26501dd3733b6a2b74333e08a58ceda28521 |
File details
Details for the file pyobject-1.2.5-cp35-cp35m-win_amd64.whl
.
File metadata
- Download URL: pyobject-1.2.5-cp35-cp35m-win_amd64.whl
- Upload date:
- Size: 65.1 kB
- Tags: CPython 3.5m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.7.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4726d661b8bdb98b22d695b8a9cbcbbc6cb7d42567204510cae83425d0fdd823 |
|
MD5 | 1dd9293c174caf4dc937aa9ef5a43fcc |
|
BLAKE2b-256 | 31fbff50a5d88694f728fae28969d0d20a801d751c35453545b8582946180f3e |