【问题标题】:Lowlevel introspection in python3?python3中的低级自省?
【发布时间】:2017-07-18 18:50:46
【问题描述】:

是否有一些自省方法可以可靠地获取对象实例的底层数据结构,不受任何自定义的影响?

在 Python 3 中,对象的低级实现可能会被深深地掩盖:可以自定义属性查找,甚至 __dict____slots__ 属性也可能无法提供完整的图片,因为它们是可写的。 dir() 明确表示显示“有趣”属性而不是实际属性,甚至 inspect 模块似乎也没有提供这样的功能。

不是重复的。此问题已被标记为与Is there a built-in function to print all the current properties and values of an object? 重复。然而,另一个问题只讨论了自省类的标准方法,这里明确列出了在较低级别上不可靠。

作为示例考虑以下带有故意隐藏类的脚本。

import inspect

actual_members = None  # <- For showing the actual contents later.

class ObscuredClass:
    def __init__(self):
        global actual_members
        actual_members = dict()
        self.__dict__ = actual_members
        self.actual_field = "actual_value"
    def __getattribute__(self, name):
        if name == "__dict__":
            return { "fake_field": "fake value - shown in __dict__" }
        else:
            return "fake_value - shown in inspect.getmembers()"

obj = ObscuredClass()
print(f"{actual_members          = }")
print(f"{dir(obj)                = }")
print(f"{obj.__dict__            = }")
print(f"{inspect.getmembers(obj) = }")

产生输出

actual_members          = {'actual_field': 'actual_value'}
dir(obj)                = ['fake_field']
obj.__dict__            = {'fake_field': 'fake value - shown in __dict__'}
inspect.getmembers(obj) = [('fake_field', 'fake_value - shown in inspect.getmembers()')]

【问题讨论】:

  • “标准”?甚至inspect.get_members 也不起作用?
  • @user202729 我添加了一个示例来演示。甚至 inspect.get_members 也依赖于对象而不是故意隐藏其内部。
  • 想法:类似于object.__getattribute__ 的东西可以按照stackoverflow.com/questions/371753/… 中的建议使用。当然它不适用于用 C 实现的对象。(话虽如此,尽管有很多 Python 类覆盖了__getattr__,但我没有看到太多覆盖__getattributes__
  • @user202729 这似乎有效。
  • 您可能希望将其作为答案发布,而不是将其编辑到问题中。

标签: python python-3.x introspection


【解决方案1】:

没有什么是完全通用的,特别是对于用 C 实现的对象。Python 类型只是没有为通用解决方案存储足够的实例布局元数据。也就是说,gc.get_referents 非常可靠,即使面对非常奇怪的 Python 级修改,包括删除或隐藏的插槽描述符和删除或隐藏的 __dict__ 描述符。

gc.get_referents 会给所有引用一个对象报告给垃圾回收系统。但是,它不会告诉你为什么一个对象有一个特定的引用——它不会告诉你一个字典是__dict__,一个字典是一个不相关的插槽,恰好有一个字典它。

例如:

import gc

class Foo:
    __slots__ = ('__dict__', 'a', 'b')
    __dict__ = None
    def __init__(self):
        self.x = 1
        self.a = 2
        self.b = 3

x = Foo()
del Foo.a
del Foo.b

print(gc.get_referents(x))

for name in '__dict__', 'x', 'a', 'b':
    try:
        print(name, object.__getattribute__(x, name))
    except AttributeError:
        print('object.__getattribute__ could not look up', name)

打印出来

[2, 3, {'x': 1}, <class '__main__.Foo'>]
__dict__ None
x 1
object.__getattribute__ could not look up a
object.__getattribute__ could not look up b

gc.get_referents 设法检索真实实例字典以及 ab 插槽,即使相关描述符都丢失了。不幸的是,它没有提供有关它检索到的任何引用的含义的信息。

object.__getattribute__ 无法检索实例字典或 ab 插槽。它确实设法找到x,因为它在检索其他属性时不依赖__dict__ 描述符来查找实例字典,但您需要已经知道x 是一个名称你应该寻找 - object.__getattribute__ 无法发现你应该在这个对象上寻找什么名字。

【讨论】:

  • 对行为不端的对象进行临时分析很有趣,但它没有说明对象的存储位置。对于列表,它返回列表的项目;对于具有__dict__ 数据字段的对象,它返回各种内容,包括__dict__,但没有确保字典__dict__ 字段。对于带有__slots__ 的类,它返回纯值,与字段无关。
  • 这是一件很奇怪的事情。也与stackoverflow.com/questions/4912499/… 相关(这表明理论上可以做到这样的事情)
【解决方案2】:

user202729 在评论中建议使用object.__getattribute__(obj, "field")

围绕这个构建一个自定义函数(使用object.__getattribute__(obj,"__dict__")object.__getattribute__(obj,"__slots__"))对于我将内部数据转储到文档稀少的代码的最初意图似乎是可行的。

然而,也有人指出,事情可能更加模糊,或者对于用 C 实现的类来说是不可访问的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多