正确使用ctypes调用_Py_Mangle? [英] Proper use of ctypes to call _Py_Mangle?

查看:92
本文介绍了正确使用ctypes调用_Py_Mangle?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

坐在蘑菇上并考虑编写一个函数来实现Python名称处理算法的复杂性时,我的脑海中出现了一个惊人的更好的主意。为什么不使用已经为该语言编写的配方来实现这一目标呢?因此,我从书包中拉出 ctypes 来帮助自己,并执行了 ctypes.pythonapi._Py_Mangle('Demo','__test')。瞧,突然出现了一个错误,提示 OSError:异常:读取0x00000A65646F00A8 的访问冲突,没有再多解释这个难题了。

While sitting on a mushroom and contemplating the intricacies of inscribing a function to implement Python's name mangling algorithm, a stupendously better idea came into my noggin. Why not use the recipe already crafted into the language to accomplish such a goal? So I pulled ctypes out of my satchel to help with the endeavor and executed ctypes.pythonapi._Py_Mangle('Demo', '__test'). Lo and behold, an error appeared out of thin air saying OSError: exception: access violation reading 0x00000A65646F00A8 and did not bother to explain the conundrum any more than that.

与解释器的完整交互如下:

The full interaction with the interpreter is as follows:

Python 3.4.2 (v3.4.2:ab2c023a9432, Oct  6 2014, 22:16:31) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import ctypes
>>> ctypes.pythonapi._Py_Mangle('Demo', '__test')
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    ctypes.pythonapi._Py_Mangle('Demo', '__test')
OSError: exception: access violation reading 0x00000A65646F00A8

有人知道要成功调用mangling函数需要进行哪些更改吗?

Does anyone know what needs to be changed in order to call the mangling function successfully?

推荐答案

感谢 eryksun 的评论,问题的答案非常简单:

Thanks to comments by eryksun, the answer to the problem is rather simple:

>>> from ctypes import pythonapi, py_object
>>> py_mangle = pythonapi._Py_Mangle
>>> py_mangle.argtypes = py_object, py_object
>>> py_mangle.restype = py_object
>>> py_mangle('Demo', '__test')
'_Demo__test'

这篇关于正确使用ctypes调用_Py_Mangle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆