- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我找不到任何可以帮助我解决此类问题的方法:我正在尝试获取作为嵌套结构一部分的属性的偏移量,例如:
数据类型.py
class FirstStructure (ctypes.Structure):
_fields_ = [('Junk', ctypes.c_bool),
('ThisOneIWantToGet', ctypes.c_int8)
]
class SecondStructure (ctypes.Structure):
_fields_ = [('Junk', ctypes.c_double),
('Example', FirstStructure)
]
SecondStructure
我完全不知道那里可以有多少嵌套结构。
ThisOneIWantToGet
的偏移量从
SecondStructure
开头的属性.
ctypes.adressof
适用于 ctypes 对象的方法。有没有什么简单的方法来获取嵌套参数的对象,所以我可以做这样的事情:
import data_types as dt
par_struct_obj = getattr(dt, 'SecondStructure')
par_obj = getattr(par_struct_obj , 'ThisOneIWantToGet')
print ctypes.addressof(parameter) - ctypes.addressof(parent_structure)
最佳答案
我将首先指出 ctypes 官方文档:[Python 3.5]: ctypes - A foreign function library for Python .
我定义了一个更复杂的结构树(2 个嵌套级别)。
数据类型.py:
import ctypes
PRAGMA_PACK = 0
class Struct2(ctypes.Structure):
if PRAGMA_PACK:
_pack_ = PRAGMA_PACK
_fields_ = [
("c_0", ctypes.c_char), # 1B
("s_0", ctypes.c_short), # 2B
("wanted", ctypes.c_int), # 4B
]
class Struct1(ctypes.Structure):
if PRAGMA_PACK:
_pack_ = PRAGMA_PACK
_fields_ = [
("d_0", ctypes.c_double), # 8B
("c_0", ctypes.c_char), # 1B
("struct2_0", Struct2),
]
class Struct0(ctypes.Structure):
if PRAGMA_PACK:
_pack_ = PRAGMA_PACK
_fields_ = [
("i_0", ctypes.c_int), # 4B
("s_0", ctypes.c_short), # 2B
("struct1_0", Struct1),
]
import sys
import ctypes
import data_types
OFFSET_TEXT = "Offset of '{:s}' member in '{:s}' instance: {:3d} (0x{:08X})"
def offset_addressof(child_structure_instance, parent_structure_instance):
return ctypes.addressof(child_structure_instance) - ctypes.addressof(parent_structure_instance)
def print_offset_addressof_data(child_structure_instance, parent_structure_instance):
offset = offset_addressof(child_structure_instance, parent_structure_instance)
print(OFFSET_TEXT.format(child_structure_instance.__class__.__name__, parent_structure_instance.__class__.__name__, offset, offset))
def main():
s0 = data_types.Struct0()
s1 = s0.struct1_0
s2 = s1.struct2_0
print("PRAGMA_PACK: {:d} {:s}\n".format(data_types.PRAGMA_PACK, "" if data_types.PRAGMA_PACK else "(default)"))
print_offset_addressof_data(s1, s0)
print_offset_addressof_data(s2, s1)
print_offset_addressof_data(s2, s0)
print("\nAlignments and sizes:\n\t'{:s}': {:3d} - {:3d}\n\t'{:s}': {:3d} - {:3d}\n\t'{:s}': {:3d} - {:3d}".format(
s0.__class__.__name__, ctypes.alignment(s0), ctypes.sizeof(s0),
s1.__class__.__name__, ctypes.alignment(s1), ctypes.sizeof(s1),
s2.__class__.__name__, ctypes.alignment(s2), ctypes.sizeof(s2)
)
)
#print("Struct0().i_0 type: {:s}".format(s0.i_0.__class__.__name__))
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
ctypes.Structure
中, 和 ctypes.addressof
如果收到这样的参数会引发 TypeError(检查 main 中的注释打印)ctypes.c_long
,它在 Lnx 上为 8 个字节,在 Win 上为 4 个字节(当然是指 64 位版本))(py35x64_test) e:\Work\Dev\StackOverflow\q050304516>python test_addressof.py
Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32
PRAGMA_PACK: 0 (default)
Offset of 'Struct1' member in 'Struct0' instance: 8 (0x00000008)
Offset of 'Struct2' member in 'Struct1' instance: 12 (0x0000000C)
Offset of 'Struct2' member in 'Struct0' instance: 20 (0x00000014)
Alignments and sizes:
'Struct0': 8 - 32
'Struct1': 8 - 24
'Struct2': 4 - 8
(py35x64_test) e:\Work\Dev\StackOverflow\q050304516>rem change PRAGMA_PACK = 1 in data_types.py
(py35x64_test) e:\Work\Dev\StackOverflow\q050304516>python test_addressof.py
Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32
PRAGMA_PACK: 1
Offset of 'Struct1' member in 'Struct0' instance: 6 (0x00000006)
Offset of 'Struct2' member in 'Struct1' instance: 9 (0x00000009)
Offset of 'Struct2' member in 'Struct0' instance: 15 (0x0000000F)
Alignments and sizes:
'Struct0': 1 - 22
'Struct1': 1 - 16
'Struct2': 1 - 7
import sys
import ctypes
import data_types
WANTED_MEMBER_NAME = "wanted"
FIELDS_MEMBER_NAME = "_fields_"
def _get_padded_size(sizes, align_size):
padded_size = temp = 0
for size in sizes:
if temp >= align_size:
padded_size += temp
temp = size
elif temp + size > align_size:
padded_size += align_size
temp = size
else:
temp += size
if temp:
padded_size += max(size, align_size)
return padded_size
def _get_array_type_sizes(array_type):
if issubclass(array_type._type_, ctypes.Array):
return _get_array_type_sizes(array_type._type_) * array_type._type_._length_
else:
return [array_type._type_] * array_type._length_
def get_nested_offset_recursive(struct_instance, wanted_member_name):
if not isinstance(struct_instance, ctypes.Structure):
return -1
align_size = ctypes.alignment(struct_instance)
base_address = ctypes.addressof(struct_instance)
member_sizes = list()
for member_name, member_type in getattr(struct_instance, FIELDS_MEMBER_NAME, list()):
if member_name == wanted_member_name:
return _get_padded_size(member_sizes, align_size)
if issubclass(member_type, ctypes.Structure):
nested_struct_instance = getattr(struct_instance, member_name)
inner_offset = get_nested_offset_recursive(nested_struct_instance, wanted_member_name)
if inner_offset != -1:
return ctypes.addressof(nested_struct_instance) - base_address + inner_offset
else:
member_sizes.append(ctypes.sizeof(member_type))
else:
if issubclass(member_type, ctypes.Array):
member_sizes.extend(_get_array_type_sizes(member_type))
else:
member_sizes.append(ctypes.sizeof(member_type))
return -1
def _get_struct_instance_from_name(struct_name):
struct_class = getattr(data_types, struct_name, None)
if struct_class:
return struct_class()
def get_nested_offset(struct_name, wanted_member_name):
struct_instance = _get_struct_instance_from_name(struct_name)
return get_nested_offset_recursive(struct_instance, wanted_member_name)
def main():
struct_names = [
"Struct2",
"Struct1",
"Struct0"
]
wanted_member_name = WANTED_MEMBER_NAME
print("PRAGMA_PACK: {:d} {:s}\n".format(data_types.PRAGMA_PACK, "" if data_types.PRAGMA_PACK else "(default)"))
for struct_name in struct_names:
print("'{:s}' offset in '{:s}' (size: {:3d}): {:3d}".format(wanted_member_name,
struct_name,
ctypes.sizeof(_get_struct_instance_from_name(struct_name)),
get_nested_offset(struct_name, wanted_member_name)))
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
ctypes.addressof
)char c[10];
成员可以替换为 char c0, c1, ..., c9;
.这就是这个函数所做的(递归)(py35x64_test) e:\Work\Dev\StackOverflow\q050304516>python struct_util.py
Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32
PRAGMA_PACK: 0 (default)
'wanted' offset in 'Struct2' (size: 8): 4
'wanted' offset in 'Struct1' (size: 24): 16
'wanted' offset in 'Struct0' (size: 32): 24
(py35x64_test) e:\Work\Dev\StackOverflow\q050304516>rem change PRAGMA_PACK = 1 in data_types.py
(py35x64_test) e:\Work\Dev\StackOverflow\q050304516>python struct_util.py
Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32
PRAGMA_PACK: 1
'wanted' offset in 'Struct2' (size: 7): 3
'wanted' offset in 'Struct1' (size: 16): 12
'wanted' offset in 'Struct0' (size: 22): 18
class Struct0_1(ctypes.Structure):
if PRAGMA_PACK:
_pack_ = PRAGMA_PACK
_fields_ = [
("i_0", ctypes.c_int), # 4B
("s_0", ctypes.c_short), # 2B
("struct1_0_2", Struct1 * 2),
("i_1", ctypes.c_int * 2), # 2 * 4B
("struct1_1", Struct1),
("i_2", ctypes.c_int), # 4B
("struct1_2_3", Struct1 * 3),
]
import sys
import ctypes
import data_types
WANTED_MEMBER_NAME = "wanted"
def _get_nested_offset_recursive_struct(struct_ctype, member_name):
for struct_member_name, struct_member_ctype in struct_ctype._fields_:
struct_member = getattr(struct_ctype, struct_member_name)
offset = struct_member.offset
if struct_member_name == member_name:
return offset
else:
if issubclass(struct_member_ctype, ctypes.Structure):
inner_offset = _get_nested_offset_recursive_struct(struct_member_ctype, member_name)
elif issubclass(struct_member_ctype, ctypes.Array):
inner_offset = _get_nested_offset_recursive_array(struct_member_ctype, member_name)
else:
inner_offset = -1
if inner_offset != -1:
return inner_offset + offset
return -1
def _get_nested_offset_recursive_array(array_ctype, member_name):
array_base_ctype = array_ctype._type_
for idx in range(array_ctype._length_):
if issubclass(array_base_ctype, ctypes.Structure):
inner_offset = _get_nested_offset_recursive_struct(array_base_ctype, member_name)
elif issubclass(array_base_ctype, ctypes.Array):
inner_offset = _get_nested_offset_recursive_array(array_base_ctype, member_name)
else:
inner_offset = -1
return inner_offset
def get_nested_offset_recursive(ctype, member_name, nth=1):
if issubclass(ctype, ctypes.Structure):
return _get_nested_offset_recursive_struct(ctype, member_name)
elif issubclass(ctype, ctypes.Array):
return _get_nested_offset_recursive_array(ctype, member_name)
else:
return -1
def main():
struct_names = [
"Struct2",
"Struct1",
"Struct0",
"Struct0_1",
]
member_name = WANTED_MEMBER_NAME
print("PRAGMA_PACK: {:d} {:s}\n".format(data_types.PRAGMA_PACK, "" if data_types.PRAGMA_PACK else "(default)"))
for struct_name in struct_names:
struct_ctype = getattr(data_types, struct_name)
print("'{:s}' offset in '{:s}' (size: {:3d}): {:3d}".format(member_name,
struct_name,
ctypes.sizeof(struct_ctype),
get_nested_offset_recursive(struct_ctype, member_name)))
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
(py35x64_test) e:\Work\Dev\StackOverflow\q050304516>python struct_util_v2.py
Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32
PRAGMA_PACK: 0 (default)
'wanted' offset in 'Struct2' (size: 8): 4
'wanted' offset in 'Struct1' (size: 24): 16
'wanted' offset in 'Struct0' (size: 32): 24
'wanted' offset in 'Struct0_1' (size: 168): 24
(py35x64_test) e:\Work\Dev\StackOverflow\q050304516>rem change PRAGMA_PACK = 1 in data_types.py
(py35x64_test) e:\Work\Dev\StackOverflow\q050304516>python struct_util_v2.py
Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32
PRAGMA_PACK: 1
'wanted' offset in 'Struct2' (size: 7): 3
'wanted' offset in 'Struct1' (size: 16): 12
'wanted' offset in 'Struct0' (size: 22): 18
'wanted' offset in 'Struct0_1' (size: 114): 18
import sys
import ctypes
import data_types
WANTED_MEMBER_NAME = "wanted"
OFFSET_INVALID = -1
def _get_nested_offset_recursive_struct(struct_ctype, member_name, index):
current_index = 0
for struct_member_name, struct_member_ctype in struct_ctype._fields_:
struct_member = getattr(struct_ctype, struct_member_name)
offset = struct_member.offset
if struct_member_name == member_name:
if index == 0:
return offset, 0
else:
current_index += 1
else:
if issubclass(struct_member_ctype, ctypes.Structure):
inner_offset, occurences = _get_nested_offset_recursive_struct(struct_member_ctype, member_name, index - current_index)
elif issubclass(struct_member_ctype, ctypes.Array):
inner_offset, occurences = _get_nested_offset_recursive_array(struct_member_ctype, member_name, index - current_index)
else:
inner_offset, occurences = OFFSET_INVALID, 0
if inner_offset != OFFSET_INVALID:
return inner_offset + offset, 0
else:
current_index += occurences
return OFFSET_INVALID, current_index
def _get_nested_offset_recursive_array(array_ctype, member_name, index):
array_base_ctype = array_ctype._type_
array_base_ctype_size = ctypes.sizeof(array_base_ctype)
current_index = 0
for idx in range(array_ctype._length_):
if issubclass(array_base_ctype, ctypes.Structure):
inner_offset, occurences = _get_nested_offset_recursive_struct(array_base_ctype, member_name, index - current_index)
elif issubclass(array_base_ctype, ctypes.Array):
inner_offset, occurences = _get_nested_offset_recursive_array(array_base_ctype, member_name, index - current_index)
else:
inner_offset, occurences = OFFSET_INVALID, 0
if inner_offset != OFFSET_INVALID:
return array_base_ctype_size * idx + inner_offset, 0
else:
if occurences == 0:
return OFFSET_INVALID, 0
else:
current_index += occurences
return OFFSET_INVALID, current_index
def get_nested_offset_recursive(ctype, member_name, index=0):
if index < 0:
return OFFSET_INVALID
if issubclass(ctype, ctypes.Structure):
return _get_nested_offset_recursive_struct(ctype, member_name, index)[0]
elif issubclass(ctype, ctypes.Array):
return _get_nested_offset_recursive_array(ctype, member_name, index)[0]
else:
return OFFSET_INVALID
def main():
struct_names = [
"Struct2",
"Struct1",
"Struct0",
"Struct0_1",
]
member_name = WANTED_MEMBER_NAME
print("PRAGMA_PACK: {:d} {:s}\n".format(data_types.PRAGMA_PACK, "" if data_types.PRAGMA_PACK else "(default)"))
for struct_name in struct_names:
struct_ctype = getattr(data_types, struct_name)
nth = 1
ofs = get_nested_offset_recursive(struct_ctype, member_name, index=nth - 1)
while ofs != OFFSET_INVALID:
print("'{:s}' offset (#{:03d}) in '{:s}' (size: {:3d}): {:3d}".format(member_name,
nth,
struct_name,
ctypes.sizeof(struct_ctype),
ofs))
nth += 1
ofs = get_nested_offset_recursive(struct_ctype, member_name, index=nth - 1)
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
(py35x64_test) e:\Work\Dev\StackOverflow\q050304516>python struct_util_v3.py
Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32
PRAGMA_PACK: 0 (default)
'wanted' offset (#001) in 'Struct2' (size: 8): 4
'wanted' offset (#001) in 'Struct1' (size: 24): 16
'wanted' offset (#001) in 'Struct0' (size: 32): 24
'wanted' offset (#001) in 'Struct0_1' (size: 192): 24
'wanted' offset (#002) in 'Struct0_1' (size: 192): 48
'wanted' offset (#003) in 'Struct0_1' (size: 192): 72
'wanted' offset (#004) in 'Struct0_1' (size: 192): 104
'wanted' offset (#005) in 'Struct0_1' (size: 192): 136
'wanted' offset (#006) in 'Struct0_1' (size: 192): 160
(py35x64_test) e:\Work\Dev\StackOverflow\q050304516>rem change PRAGMA_PACK = 1 in data_types.py
(py35x64_test) e:\Work\Dev\StackOverflow\q050304516>python struct_util_v3.py
Python 3.5.4 (v3.5.4:3f56838, Aug 8 2017, 02:17:05) [MSC v.1900 64 bit (AMD64)] on win32
PRAGMA_PACK: 1
'wanted' offset (#001) in 'Struct2' (size: 7): 3
'wanted' offset (#001) in 'Struct1' (size: 16): 12
'wanted' offset (#001) in 'Struct0' (size: 22): 18
'wanted' offset (#001) in 'Struct0_1' (size: 130): 18
'wanted' offset (#002) in 'Struct0_1' (size: 130): 34
'wanted' offset (#003) in 'Struct0_1' (size: 130): 50
'wanted' offset (#004) in 'Struct0_1' (size: 130): 74
'wanted' offset (#005) in 'Struct0_1' (size: 130): 94
'wanted' offset (#006) in 'Struct0_1' (size: 130): 110
关于python - 通过自省(introspection)从 ctype 结构中获取元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50304516/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!