Python/C API手册提到了从⁽¹⁾到⁽²⁾void指针的转换函数,这似乎是在C中使用任意长度Python整数的唯一方法。
(1) :PyLong_FromVoidPtr()
并用Py_BuildValue()
格式化0&
(2) :PyLong_AsVoidPtr()
和格式0
,0&
和0!
与PyArg_…Parse…()
但是,我在手册中没有找到任何关于如何使用这些空指针来处理C中的任意长整数的指示。
(3) :我尝试搜索«voidptr»、«void*»和«0&;»,但尚未完全阅读。你知道吗
我在哪里可以找到关于它们内部结构的信息,或者在它们上面计算的原语?你知道吗
在
Include/longintrepr.h
中有文档:int
的长度是可变长度部分的长度乘以15/16位-数字要么是30 bits in ^{有一个成员ob_size将以字节表示大小-因此,如果
PYLONG_BITS_IN_DIGIT
是30,那么ob_digit
是一个ob_size / sizeof(uint32_t)
uint32_t
的数组,30位在每个位中是有效的;否则ob_digit
是一个ob_size / sizeof(uint16_t)
uint16_t
的数组,15位在每个位中存储。你知道吗这都是
Include/longintrepr.h
的一部分,但它们只被#ifndef Py_LIMITED_API
揭示!你知道吗实际上,这些函数不是要有“指向任意大整数的指针”,而是字面上的整数值作为} 和^{} 的实现。它只允许您在Python中保存任意指针,以确保正确完成转换。你知道吗
void *
指针,例如,强制转换为void *
类型。参见^{据我所知,从Python中获取任意长整数最实用的方法是使用^{} 和^{} 。实际上有一个内部ishapi ^{} /^{} ,您可以使用它。参见相关问题Python extension - construct and inspect large integers efficiently。你知道吗
注意:有趣的是,似乎没有任何官方或其他的C API来告诉Python整数值的位或字节长度。在Python中有^{} ,但它似乎没有映射到任何公开可用的函数。你知道吗
相关问题 更多 >
编程相关推荐