是否有比“marshal”更低层的对象操作函数?或“cPickle”在Python中?
例如,根据 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”或“cPickle”更低层的对象操作函数?
例如,我已经将转储的数据加载到内存中,我只想像在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要认为你可以只获取 python 对象的内存映像,存储它,然后将其加载回不同的解释器,甚至稍后加载到相同的解释器中,并期望它有意义。不会的。整数和浮点数可能完全包含在对象结构中,但字符串已经有一个单独分配的数据缓冲区,甚至很长。
换句话说,
cPickle
是可能的最低层(cPickle
比marshal
级别低,因为后者保持了版本和平台之间的兼容性,这使得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 thanmarshal
, 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.如果你实际上并不需要序列化一般的Python对象,只需要编码和解码某些特定的东西,那么你可以看看
struct
模块。该模块将用于直接处理表示 C 结构的字节串(例如:DNS 协议数据包)。与 Perl 中的pack
和unpack
类似。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 topack
andunpack
in Perl.