Python Ctypes - Memmove 无法正常工作

Python Ctypes - Memmove not working correctly

我想将数据从一个变量移动到另一个变量。

我有以下代码:

a = 'a' # attempting to move the contents of b into here
b = 'b'

obj = ctypes.py_object.from_address(id(a))
obj2 = ctypes.py_object.from_address(id(b))

ptr = ctypes.pointer(obj)
ptr2 = ctypes.pointer(obj2)

ctypes.memmove(ptr, ptr2, ctypes.sizeof(obj2))
print(a, b) # expected result: b b

a 没有变化,也没有报错。

这是不可能的,还是我做错了什么?

不推荐但是对学习很有趣...

由于 id(obj) returns 内部 PyObject 地址的实现细节,在 CPython 上是可能的,但这是一个非常糟糕的主意。 Python 字符串是不可变的,因此破坏它们的内部工作机制将会破坏一切。 Python 对象有内部数据,如引用计数、类型、长度,盲目复制它们会被破坏。

import ctypes as ct
import sys

# Using strings that are more unique and less likely to be used inside Python
# (lower reference counts).
a = '123'
b = '456'

# Create ctypes byte buffers that reference the same memory as a and b
bytes_a = (ct.c_ubyte * sys.getsizeof(a)).from_address(id(a))
bytes_b = (ct.c_ubyte * sys.getsizeof(b)).from_address(id(b))

# View the bytes as hex.  The first bytes are the reference counts.
# The last bytes are the ASCII bytes of the strings.
print(bytes(bytes_a).hex())
print(bytes(bytes_b).hex())
ct.memmove(bytes_b, bytes_a, len(bytes_a))

# Does what you want, but Python crashes on exit in my case
print(a,b)

输出:

030000000000000060bc9563fc7f00000300000000000000bf4fda89331c3232e5a5a97d1b020000000000000000000031323300
030000000000000060bc9563fc7f00000300000000000000715a1b84492b4696e5feaf7d1b020000000000000000000034353600
123 123
Exception ignored deletion of interned string failed:
KeyError: '123'
111

制作内存副本并查看它的安全方法

import ctypes as ct
import sys

a = '123'
# Copy memory at address to a Python bytes object.
bytes_a = ct.string_at(id(a), sys.getsizeof(a))
print(bytes_a.hex())

输出:

020000000000000060bc5863fc7f000003000000000000001003577d19c6d60be59f53919b010000000000000000000031323300