Skip to content

Commit 84a37b9

Browse files
committed
Python reference counting
1 parent a179d6d commit 84a37b9

2 files changed

Lines changed: 10 additions & 80 deletions

File tree

‎src/pythonapi/Interpreter.cc‎

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,15 @@ limitations under the License.
2020
#include "dsAssert.hh"
2121
#include "GetGlobalParameter.hh"
2222

23-
//#include <iostream>
2423

25-
Interpreter::Interpreter() //: interp_(nullptr)
24+
Interpreter::Interpreter()
2625
{
2726
}
2827

2928
Interpreter::~Interpreter()
3029
{
3130
}
3231

33-
#if 0
34-
/* for now run in main intepreter*/
35-
bool Interpreter::RunCommand(const std::string &command, const std::vector<std::string> &arguments)
36-
{
37-
// PyObject *interp_ = reinterpret_cast<PyObject *>(GetMainInterpreter());
38-
39-
std::vector<ObjectHolder> objs(arguments.size() + 1);
40-
objs[0] = ObjectHolder(command);
41-
for (size_t i = 0; i < arguments.size(); ++i)
42-
{
43-
objs[i + 1] = ObjectHolder(arguments[i]);
44-
}
45-
return RunCommand(objs);
46-
}
47-
#endif
48-
4932
namespace
5033
{
5134
PyObject *GetGlobalDictionary()
@@ -125,13 +108,13 @@ ObjectHolder CreateTuple(std::vector<ObjectHolder> &objects, size_t beg, size_t
125108
for (size_t i = 0; i < len; ++i)
126109
{
127110
PyObject *p = reinterpret_cast<PyObject *>(objects[beg + i].GetObject());
111+
// PyTuple_SetItem steals a reference
128112
Py_INCREF(p);
129113
PyTuple_SetItem(args, i, p);
130114
}
131115
return ret;
132116
}
133117

134-
///// Check for duplicates!
135118
ObjectHolder CreateDictionary(const std::vector<std::pair<std::string, ObjectHolder> > &objects)
136119
{
137120
ObjectHolder ret;
@@ -141,7 +124,8 @@ ObjectHolder CreateDictionary(const std::vector<std::pair<std::string, ObjectHol
141124
{
142125
const std::string &key = objects[i].first;
143126
const PyObject *obj = reinterpret_cast<const PyObject *>((objects[i].second).GetObject());
144-
Py_INCREF(const_cast<PyObject *>(obj));
127+
//PyDict_SetItemString does not steal a reference
128+
//Py_INCREF(const_cast<PyObject *>(obj));
145129
PyDict_SetItemString(args, key.c_str(), const_cast<PyObject *>(obj));
146130
}
147131
return ret;
@@ -175,7 +159,6 @@ bool Interpreter::RunCommand(ObjectHolder &procedure, std::vector<ObjectHolder>
175159
return ret;
176160
}
177161

178-
//// This really can't fail
179162
ObjectHolder args = CreateTuple(objects, 0, objects.size());
180163

181164
PyErr_Clear();
@@ -194,41 +177,6 @@ bool Interpreter::RunCommand(ObjectHolder &procedure, std::vector<ObjectHolder>
194177
return ret;
195178
}
196179

197-
#if 0
198-
bool Interpreter::RunCommand(std::vector<ObjectHolder> &objects)
199-
{
200-
bool ret = false;
201-
error_string_.clear();
202-
203-
std::string commandname = objects[0].GetString();
204-
205-
ObjectHolder command_object = GetCommandFromInterpreter(commandname, error_string_);
206-
if (!error_string_.empty())
207-
{
208-
ret = false;
209-
return ret;
210-
}
211-
212-
//// This really can't fail
213-
ObjectHolder args = CreateTuple(objects, 1, objects.size() - 1);
214-
215-
PyErr_Clear();
216-
PyObject *robj = PyObject_Call(reinterpret_cast<PyObject *>(command_object.GetObject()), reinterpret_cast<PyObject *>(args.GetObject()), nullptr);
217-
result_ = ObjectHolder(robj);
218-
if (robj)
219-
{
220-
ret = true;
221-
}
222-
else
223-
{
224-
ret = false;
225-
ProcessError(commandname, error_string_);
226-
}
227-
228-
return ret;
229-
}
230-
#endif
231-
232180
bool Interpreter::RunInternalCommand(const std::string &commandname, const std::vector<std::pair<std::string, ObjectHolder> > &arguments)
233181
{
234182
std::string newname;
@@ -238,7 +186,6 @@ bool Interpreter::RunInternalCommand(const std::string &commandname, const std::
238186
return RunCommand(newname, arguments);
239187
}
240188

241-
//TODO: pass command directly as function reference from python
242189
bool Interpreter::RunCommand(const std::string &commandname, const std::vector<std::pair<std::string, ObjectHolder> > &arguments)
243190
{
244191
bool ret = false;
@@ -273,24 +220,6 @@ bool Interpreter::RunCommand(const std::string &commandname, const std::vector<s
273220
}
274221

275222

276-
#if 0
277-
bool Interpreter::RunCommand(const std::string &commandString)
278-
{
279-
error_string_.clear();
280-
PyErr_Clear();
281-
PyObject *globals = GetGlobalDictionary();
282-
PyObject *res = PyRun_String(commandString.c_str(), Py_file_input, globals, globals);
283-
result_ = ObjectHolder(res);
284-
285-
if (!res)
286-
{
287-
ProcessError(commandString, error_string_);
288-
}
289-
290-
return (res != nullptr);
291-
}
292-
#endif
293-
294223
std::string Interpreter::GetVariable(const std::string &name)
295224
{
296225
std::string ret;

‎src/pythonapi/ObjectHolder.cc‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void ObjectHolder::clear()
7575

7676
ObjectHolder::ObjectHolder(void *t) : object_(t)
7777
{
78-
//// Assume the reference already had a reference count increase
78+
//// Assume the reference already had a reference count increase
7979
#if 0
8080
if (object_)
8181
{
@@ -297,9 +297,9 @@ bool ObjectHolder::GetListOfObjects(ObjectHolderList_t &objs) const
297297
objs.resize(len);
298298
for (size_t i = 0; i < len; ++i)
299299
{
300-
//// this is a borrowed reference
300+
//// this is a new reference
301301
PyObject *lobj = PySequence_GetItem(obj, i);
302-
Py_INCREF(lobj);
302+
//Py_INCREF(lobj);
303303
objs[i] = ObjectHolder(lobj);
304304
}
305305
}
@@ -521,10 +521,11 @@ ObjectHolder::ObjectHolder(ObjectHolderMap_t &map)
521521
{
522522
const std::string &key = it->first;
523523
PyObject *val = reinterpret_cast<PyObject *>((it->second).GetObject());
524-
Py_INCREF(val);
524+
//// does not steal the reference
525+
//Py_INCREF(val);
525526
PyDict_SetItemString(map_object, key.c_str(), val);
526527
}
527-
Py_INCREF(map_object);
528+
//Py_INCREF(map_object);
528529
object_ = map_object;
529530
}
530531

0 commit comments

Comments
 (0)