在python中检测C类型限制("limits.h")? [英] Detecting C types limits ("limits.h") in python?
问题描述
在很多情况下,我已经在python中制作了一个小测试程序来测试某些C函数.此python程序使用ctypes
导入和使用我的C函数.
I've made a little test program in python to test some C functions in many cases. This python program use ctypes
to import and use my C functions.
但是我还想测试最小值和最大值的情况(例如limits.h
的ULLONG_MAX
).
But I'd also like to test minimum and maximum values cases (limits.h
's ULLONG_MAX
for instance).
但是,由于其中一些限制可能取决于系统,因此,我宁愿避免在程序中对其进行硬编码.我宁愿动态地获取它.
But since some of these limits can be system dependant, I'd prefer to avoid hard coding it in my program; I'd rather dynamically get it.
是否可以在python中获得这些极限值?
Is it possible to get these limits values in python?
推荐答案
我相信您可以找到的最接近的
I believe the closest you can get is
ctypes.sizeof(whatever_type)
给出类型的大小(以字节为单位).例如,您可以使用ctypes.sizeof(ctypes.c_int)
来确定一个字节是4个字节,8个字节还是其他一些奇怪的数字.
which gives you the size of the type in bytes. For example, you can use ctypes.sizeof(ctypes.c_int)
to figure out whether a byte is 4 bytes, 8, or some other weird number.
这篇关于在python中检测C类型限制("limits.h")?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!