Repository navigation
Expand file tree
/
Copy pathpickler.cpp
More file actions
187 lines (147 loc) · 4.6 KB
/
Copy pathpickler.cpp
File metadata and controls
187 lines (147 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
This source file is part of KBEngine
For the latest info, see http://www.kbengine.org/
Copyright (c) 2008-2016 KBEngine.
KBEngine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KBEngine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "pickler.h"
#include "helper/profile.h"
namespace KBEngine{ namespace script {
PyObject* Pickler::picklerMethod_ = NULL;
PyObject* Pickler::unPicklerMethod_ = NULL;
PyObject* Pickler::pyPickleFuncTableModule_ = NULL;
bool Pickler::isInit = false;
//-------------------------------------------------------------------------------------
bool Pickler::initialize(void)
{
if(isInit)
return true;
PyObject* cPickleModule = PyImport_ImportModule("pickle");
if(cPickleModule)
{
picklerMethod_ = PyObject_GetAttrString(cPickleModule, "dumps");
if (!picklerMethod_)
{
ERROR_MSG("Pickler::initialize: get dumps is error!\n");
PyErr_PrintEx(0);
}
unPicklerMethod_ = PyObject_GetAttrString(cPickleModule, "loads");
if(!unPicklerMethod_)
{
ERROR_MSG("Pickler::init: get loads is error!\n");
PyErr_PrintEx(0);
}
Py_DECREF(cPickleModule);
}
else
{
ERROR_MSG("PyGC::initialize: can't import pickle!\n");
PyErr_PrintEx(0);
}
isInit = picklerMethod_ && unPicklerMethod_;
if(isInit)
{
// 初始化一个unpickle函数表模块, 所有自定义类的unpickle函数都需要在此注册
pyPickleFuncTableModule_ = PyImport_AddModule("_upf");
static struct PyModuleDef moduleDesc =
{
PyModuleDef_HEAD_INIT,
"_upf",
"This module is created by KBEngine!",
-1,
NULL
};
PyObject* m = PyModule_Create(&moduleDesc);
if(m == NULL)
{
isInit = false;
}
}
return isInit;
}
//-------------------------------------------------------------------------------------
void Pickler::finalise(void)
{
Py_XDECREF(picklerMethod_);
Py_XDECREF(unPicklerMethod_);
picklerMethod_ = NULL;
unPicklerMethod_ = NULL;
pyPickleFuncTableModule_ = NULL;
}
//-------------------------------------------------------------------------------------
PyObject* Pickler::getUnpickleFunc(const char* funcName)
{
PyObject* pyfunc = PyObject_GetAttrString(pyPickleFuncTableModule_, funcName);
if(pyfunc == NULL)
{
SCRIPT_ERROR_CHECK();
}
return pyfunc;
}
//-------------------------------------------------------------------------------------
std::string Pickler::pickle(PyObject* pyobj)
{
AUTO_SCOPED_PROFILE("pickle");
PyObject* pyRet = PyObject_CallFunction(picklerMethod_,
const_cast<char*>("(O)"), pyobj);
SCRIPT_ERROR_CHECK();
if(pyRet)
{
std::string str;
str.assign(PyBytes_AsString(pyRet), PyBytes_Size(pyRet));
S_RELEASE(pyRet);
return str;
}
return "";
}
//-------------------------------------------------------------------------------------
std::string Pickler::pickle(PyObject* pyobj, int8 protocol)
{
AUTO_SCOPED_PROFILE("pickleEx");
PyObject* pyRet = PyObject_CallFunction(picklerMethod_,
const_cast<char*>("(Oi)"), pyobj, protocol);
SCRIPT_ERROR_CHECK();
if(pyRet)
{
std::string str;
str.assign(PyBytes_AsString(pyRet), PyBytes_Size(pyRet));
S_RELEASE(pyRet);
return str;
}
return "";
}
//-------------------------------------------------------------------------------------
PyObject* Pickler::unpickle(const std::string& str)
{
AUTO_SCOPED_PROFILE("unpickle");
PyObject* pyRet = PyObject_CallFunction(unPicklerMethod_,
const_cast<char*>("(y#)"), str.data(), str.length());
if (!pyRet)
{
ERROR_MSG(fmt::format("Pickler::unpickle: failed to unpickle[{}] len={}.\n",
str.c_str(), str.length()));
}
SCRIPT_ERROR_CHECK();
return pyRet;
}
//-------------------------------------------------------------------------------------
void Pickler::registerUnpickleFunc(PyObject* pyFunc, const char* funcName)
{
if(PyObject_SetAttrString(pyPickleFuncTableModule_, funcName, pyFunc) == -1)
{
SCRIPT_ERROR_CHECK();
return;
}
}
//-------------------------------------------------------------------------------------
}
}