python - 使用 Swig 从 Python long int 转换为 unsigned int 64
问题描述
我正在使用 Swig 开发一个 Python 库。我有一个类似于以下的类型图:
%typemap(in) (unsigned long long myLongParam){
$1 = PyInt_AsUnsignedLongLongMask($input);
}
我这样调用目标 Python 函数:
#Calling with a very large integer
myFunc(0xFFFFFFFF11111111L)
这应该是一个 64 位长的无符号整数。但是,该值似乎被截断了 32 位。
解决方案
解决了。问题不存在。问题出在其他地方,与上述代码无关。我正在执行以下错误代码:
%apply int* OUTPUT {Uint64*}
这告诉 swig 将 Uint64 视为与 int 相同,因此结果被截断。它使用 SWIG_From_int 到结果值。
我的解决方案是:
/*Uint64* parameter as reference */
%typemap(in, numinputs=0) Uint64* (Uint64 temp) {
temp = 0;
$1 = &temp;
}
/*Uint64* as function return typemap*/
%typemap(argout) Uint64* {
%append_output( PyInt_FromLong( *$1 ) );
}
推荐阅读
- python - 对 Flask 的字符串 POST 请求
- python - 如何对python中数据框列中的唯一值执行函数
- javascript - 如何从 HTML 标签中删除 CSS 属性
- python - Pandas 在单个 dataRow 中写入具有自定义限制值的 CSV 文件
- c - 文本段如何设置为只读?
- docker - 中间 Docker 镜像会占用磁盘空间吗?
- machine-learning - Julia 创建具有匹配参数的函数数组以在循环中执行
- java - 如何使此代码不返回无限循环?
- javascript - 如何在 JavaScript 中生成 Shift_JIS(SJIS) 百分比编码字符串
- java - 解决java算法的bug