gpt4 book ai didi

python - 名称和函数体如何存储在 Python 代码对象中?

转载 作者:太空宇宙 更新时间:2023-11-03 23:41:21 25 4
gpt4 key购买 nike

我有一个 python 脚本。

def hello(self):
return 6
print hello()

在 CPython 中编译后反汇编

>>> c = compile(open('hello.py').read(), 'hello.py', 'exec')
>>> import dis
>>> dis.dis(c)
1 0 LOAD_CONST 0 (<code object hello at 0x1006c9230, file "hello.py", line 1>)
3 MAKE_FUNCTION 0
6 STORE_NAME 0 (hello)

3 9 LOAD_NAME 0 (hello)
12 CALL_FUNCTION 0
15 PRINT_ITEM
16 PRINT_NEWLINE
17 LOAD_CONST 1 (None)
20 RETURN_VALUE

我很好奇 <code object hello at 0x1006c9230 ...> 是怎么来的存储在 CPython 代码对象中。有 co_code函数,但只打印出字节码指令。如果我序列化我得到的 CPython 代码对象

>>> import marshal
>>> marshal.dumps(c)
'c\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00@\x00\x00\x00s\x15\x00\x00\x00d\x00\x00\x84\x00\x00Z\x00\x00e\x00\x00\x83\x00\x00GHd\x01\x00S(\x02\x00\x00\x00c\x01\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00C\x00\x00\x00s\n\x00\x00\x00d\x01\x00}\x01\x00|\x01\x00S(\x02\x00\x00\x00Ni\x06\x00\x00\x00(\x00\x00\x00\x00(\x02\x00\x00\x00t\x04\x00\x00\x00selft\x01\x00\x00\x00x(\x00\x00\x00\x00(\x00\x00\x00\x00s\x08\x00\x00\x00hello.pyt\x05\x00\x00\x00hello\x01\x00\x00\x00s\x04\x00\x00\x00\x00\x01\x06\x01N(\x01\x00\x00\x00R\x02\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00(\x00\x00\x00\x00s\x08\x00\x00\x00hello.pyt\x08\x00\x00\x00<module>\x01\x00\x00\x00s\x02\x00\x00\x00\t\x03'

我知道

def hello(self):
return 6

存储在转储中的某处,因为如果我将其更改为 return 5 ,转储中的一个字节从 6 切换到 5。

1) 有没有一种方法可以从 CPython 代码对象访问函数体。我能拿到的最近的c.names但这只会打印出一个字符串。我假设在幕后它是一个被序列化为字符串的 PyObject。我还想确认函数体确实存储在 c.names 中.

2) marshal dump 将函数存储为字节码指令还是未编译的文字?当我搜索操作码\x83 (RETURN_VALUE) 时,我倾向于未编译的文字,它只在转储中出现一次。我相信这意味着只有一个 return 语句,而应该有两个:一次退出函数 hello,一次返回 None 以退出脚本。

版本

Python 2.7.13+ (heads/2.7:96f5020597, May 26 2017, 15:26:13)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

最佳答案

让我们分解一下。

首先,让我澄清一下 CPython 究竟是如何存储函数的。解析函数时,CPython 将函数的数据存储在代码对象 中。 CPython 使用代码对象来存储函数、类和模块。然后将表示函数的代码对象序列化为特定的字节代码格式。

函数的代码对象存储在它们的__code__中。属性:

>>> def foo():
pass

>>>
>>> foo.__code__
<code object foo at 0x7f8bd86ce5d0, file "<pyshell#14>", line 1>
>>>

这些代码对象包含与函数相关的各种数据,例如函数参数、引用的常量(例如 1"Hello" )和名称。函数的字节码存储在.co_code中属性。这是 CPython 运行您的函数时实际执行的内容:

>>> def foo():
pass

>>> foo.__code__.co_code
b'd\x00\x00S' # bytecode for foo
>>>

现在您了解了 CPython 的基本功能,我们可以解决您的具体问题。

Is there a way I can access the function body from the CPython code object. The closest I can get it c.names but that only prints out a string. I'm assuming there behind the scenes it is a PyObject that is being serialized as a string. I would also like a confirmation that the function body is indeed stored in c.names.

函数体没有存储在co_name中代码对象的属性。它存储在 .co_code 中属性如上所述。您的其他假设也有点偏离。从技术上讲,由于 Python 中的所有对象都“继承”自 PyObject , 说函数体被序列化为 PyObject 是正确的序列化为字符串。但是,最好说它被序列化为PyStringObject。这是表示字符串的特定类型。

Does marshal dump store the function as bytecode instructions or as a uncompiled literal? I'm leaning toward uncompiled literal as I searched for the opcode \x83 (RETURN_VALUE) and it only appears once in the dump. I believe this implies that there is only one return statement when there should be two: once to exit out of the function hello and once to return None for exiting the script.

两者都不做。 marhsal.dumps()接受一个代码对象,将整个代码对象序列化为 CPython 特定格式,并返回一个表示序列化代码对象的字节对象。但是,您的第二个陈述是正确的。在每个 Python 脚本的末尾,隐含 None被退回。这可以通过将空参数传递给 dis.dis() 来观察。 :

>>> import dis
>>> dis.dis("")
1 0 LOAD_CONST 0 (None)
3 RETURN_VALUE
>>>

I know for a fact that <code object hello at 0x1006c9230 ...> is not stored in the co_code attribute of the original c. This is because no matter how I change the inside of def hello() the same disassembler output is given. To be clear this is a function inside a function/script not just a function as you gave in your example.

在您的具体示例中,变量 c是一个代码对象,代表模块——而不是函数——“hello.py”。你的权利,函数的代码对象hello()不在 co_code 中.它存储在模块的代码对象的 co_consts 中。属性:

>>> co = compile(open('hello.py').read(), 'hello.py', 'exec')
>>> co.co_consts
(<code object hello at 0x7fedcbd3dc00, file "hello.py", line 1>, 'hello', None)
>>>

这是因为 Python 执行代码的方式。常量不直接存储在代码对象的字节码中。相反,它们存储在自己单独的元组中。每当在函数代码中引用常量时,实际常量存储在 co_consts 中和一个索引,它对应于所述常量在co_consts中的位置放在字节码中。

为什么你的反汇编器输出 hello() 的原因的代码对象永远不会改变是因为所有 dis.dis()正在做的只是显示 hello()字符串表示代码对象。 hello() 的代码对象确实在您更改代码时发生了变化,但该变化由 dis 显示.它不显示 hello() 的实际更改 属性s 代码对象。

关于python - 名称和函数体如何存储在 Python 代码对象中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44212297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com