是否有比“marshal”更低层的对象操作函数?或“cPickle”在Python中?

发布于 2024-11-27 22:42:59 字数 651 浏览 0 评论 0原文

例如,根据 Python 源代码,Marshal 仍在解析输入数据。

.....
case TYPE_FALSE:
    Py_INCREF(Py_False);
    retval = Py_False;
    break;

case TYPE_TRUE:
    Py_INCREF(Py_True);
    retval = Py_True;
    break;

case TYPE_INT:
    retval = PyInt_FromLong(r_long(p));
    break;

case TYPE_INT64:
    retval = r_long64(p);
    break;

case TYPE_LONG:
    retval = r_PyLong(p);
    break;

case TYPE_FLOAT:
.......

Python中是否有比“marshal”或“c​​Pickle”更低层的对象操作函数?

例如,我已经将转储的数据加载到内存中,我只想像在 C/C++ 中那样进行类型转换,(PyObject *) data_loaded_in_memory;

编辑:如果这不能直接在 python 中完成,那么任何有关 C 函数来编写该功能的提示都会很棒。

For example, Marshal is still parsing the input data, according to Python source.

.....
case TYPE_FALSE:
    Py_INCREF(Py_False);
    retval = Py_False;
    break;

case TYPE_TRUE:
    Py_INCREF(Py_True);
    retval = Py_True;
    break;

case TYPE_INT:
    retval = PyInt_FromLong(r_long(p));
    break;

case TYPE_INT64:
    retval = r_long64(p);
    break;

case TYPE_LONG:
    retval = r_PyLong(p);
    break;

case TYPE_FLOAT:
.......

Is there any lower layer object manipulation function than "marshal" or "cPickle" in Python?

For example, I already loaded the dumped data to memory, which I just want to type casting like we can do in C/C++, (PyObject *) data_loaded_in_memory;

Edit: If this cannot be done in python directly, any hints about C functions to write that ability would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你另情深 2024-12-04 22:42:59

不要认为你可以只获取 python 对象的内存映像,存储它,然后将其加载回不同的解释器,甚至稍后加载到相同的解释器中,并期望它有意义。不会的。整数和浮点数可能完全包含在对象结构中,但字符串已经有一个单独分配的数据缓冲区,甚至很长。

换句话说,cPickle是可能的最低层(cPicklemarshal级别低,因为后者保持了版本和平台之间的兼容性,这使得cPickle 不允许存储对象并将它们加载到另一个解释器或同一解释器中(如果它们之间从内存中释放)。

Don't think you could just take the memory image of python object, store it, load it back in different interpreter or even the same one later and expect it to make sense. It will not. Integers and floats may be fully contained in the object structure, but string already has a separately-allocated buffer for the data and even a long does.

In another words, cPickle is the lowest possible layer (cPickle is lower level than marshal, because the later maintains compatibility between versions and platforms, which cPickle does not) allowing to store the objects and load them in another interpreter or the same interpreter if they were released from memory in between.

半城柳色半声笛 2024-12-04 22:42:59

如果你实际上并不需要序列化一般的Python对象,只需要编码和解码某些特定的东西,那么你可以看看struct 模块。该模块将用于直接处理表示 C 结构的字节串(例如:DNS 协议数据包)。与 Perl 中的 packunpack 类似。

If you do not really need to serialize Python objects in general, only encode and decode certain specific things, then you might look at the struct module. This module would be used to work directly with byte-strings that represent C structs (example: DNS protocol packets). Similar idea to pack and unpack in Perl.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文