Skip to content

Commit c154f39

Browse files
committed
[lldb/python] Use PythonObject in LLDBSwigPython functions
Return our PythonObject wrappers instead of raw PyObjects (obfuscated as void *). This ensures that ownership (reference counts) of python objects is automatically tracked. Differential Revision: https://reviews.llvm.org/D117462
1 parent cc0d208 commit c154f39

File tree

8 files changed

+154
-181
lines changed

8 files changed

+154
-181
lines changed

lldb/bindings/python/python-wrapper.swig

Lines changed: 53 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ bool lldb_private::LLDBSwigPythonCallTypeScript(
152152
return true;
153153
}
154154

155-
void *lldb_private::LLDBSwigPythonCreateSyntheticProvider(
155+
PythonObject lldb_private::LLDBSwigPythonCreateSyntheticProvider(
156156
const char *python_class_name, const char *session_dictionary_name,
157157
const lldb::ValueObjectSP &valobj_sp) {
158158
if (python_class_name == NULL || python_class_name[0] == '\0' ||
159159
!session_dictionary_name)
160-
Py_RETURN_NONE;
160+
return PythonObject();
161161

162162
PyErr_Cleaner py_err_cleaner(true);
163163

@@ -167,29 +167,29 @@ void *lldb_private::LLDBSwigPythonCreateSyntheticProvider(
167167
python_class_name, dict);
168168

169169
if (!pfunc.IsAllocated())
170-
Py_RETURN_NONE;
170+
return PythonObject();
171171

172172
auto sb_value = std::make_unique<lldb::SBValue>(valobj_sp);
173173
sb_value->SetPreferSyntheticValue(false);
174174

175175
PythonObject val_arg = ToSWIGWrapper(std::move(sb_value));
176176
if (!val_arg.IsAllocated())
177-
Py_RETURN_NONE;
177+
return PythonObject();
178178

179179
PythonObject result = pfunc(val_arg, dict);
180180

181181
if (result.IsAllocated())
182-
return result.release();
182+
return result;
183183

184-
Py_RETURN_NONE;
184+
return PythonObject();
185185
}
186186

187-
void *lldb_private::LLDBSwigPythonCreateCommandObject(
187+
PythonObject lldb_private::LLDBSwigPythonCreateCommandObject(
188188
const char *python_class_name, const char *session_dictionary_name,
189189
lldb::DebuggerSP debugger_sp) {
190190
if (python_class_name == NULL || python_class_name[0] == '\0' ||
191191
!session_dictionary_name)
192-
Py_RETURN_NONE;
192+
return PythonObject();
193193

194194
PyErr_Cleaner py_err_cleaner(true);
195195
auto dict = PythonModule::MainModule().ResolveName<PythonDictionary>(
@@ -198,24 +198,19 @@ void *lldb_private::LLDBSwigPythonCreateCommandObject(
198198
python_class_name, dict);
199199

200200
if (!pfunc.IsAllocated())
201-
return nullptr;
202-
203-
PythonObject result = pfunc(ToSWIGWrapper(std::move(debugger_sp)), dict);
201+
return PythonObject();
204202

205-
if (result.IsAllocated())
206-
return result.release();
207-
208-
Py_RETURN_NONE;
203+
return pfunc(ToSWIGWrapper(std::move(debugger_sp)), dict);
209204
}
210205

211-
void *lldb_private::LLDBSwigPythonCreateScriptedProcess(
206+
PythonObject lldb_private::LLDBSwigPythonCreateScriptedProcess(
212207
const char *python_class_name, const char *session_dictionary_name,
213208
const lldb::TargetSP &target_sp,
214209
const lldb_private::StructuredDataImpl &args_impl,
215210
std::string &error_string) {
216211
if (python_class_name == NULL || python_class_name[0] == '\0' ||
217212
!session_dictionary_name)
218-
Py_RETURN_NONE;
213+
return PythonObject();
219214

220215
PyErr_Cleaner py_err_cleaner(true);
221216

@@ -227,7 +222,7 @@ void *lldb_private::LLDBSwigPythonCreateScriptedProcess(
227222
if (!pfunc.IsAllocated()) {
228223
error_string.append("could not find script class: ");
229224
error_string.append(python_class_name);
230-
return nullptr;
225+
return PythonObject();
231226
}
232227

233228
PythonObject target_arg = ToSWIGWrapper(target_sp);
@@ -240,7 +235,7 @@ void *lldb_private::LLDBSwigPythonCreateScriptedProcess(
240235
[&](const llvm::ErrorInfoBase &E) {
241236
error_string.append(E.message());
242237
});
243-
Py_RETURN_NONE;
238+
return PythonObject();
244239
}
245240

246241
PythonObject result = {};
@@ -249,21 +244,17 @@ void *lldb_private::LLDBSwigPythonCreateScriptedProcess(
249244
} else {
250245
error_string.assign("wrong number of arguments in __init__, should be 2 "
251246
"(not including self)");
252-
Py_RETURN_NONE;
253247
}
254-
255-
if (result.IsAllocated())
256-
return result.release();
257-
Py_RETURN_NONE;
248+
return result;
258249
}
259250

260-
void *lldb_private::LLDBSwigPythonCreateScriptedThread(
251+
PythonObject lldb_private::LLDBSwigPythonCreateScriptedThread(
261252
const char *python_class_name, const char *session_dictionary_name,
262253
const lldb::ProcessSP &process_sp, const StructuredDataImpl &args_impl,
263254
std::string &error_string) {
264255
if (python_class_name == NULL || python_class_name[0] == '\0' ||
265256
!session_dictionary_name)
266-
Py_RETURN_NONE;
257+
return PythonObject();
267258

268259
PyErr_Cleaner py_err_cleaner(true);
269260

@@ -275,7 +266,7 @@ void *lldb_private::LLDBSwigPythonCreateScriptedThread(
275266
if (!pfunc.IsAllocated()) {
276267
error_string.append("could not find script class: ");
277268
error_string.append(python_class_name);
278-
return nullptr;
269+
return PythonObject();
279270
}
280271

281272
llvm::Expected<PythonCallable::ArgInfo> arg_info = pfunc.GetArgInfo();
@@ -286,30 +277,24 @@ void *lldb_private::LLDBSwigPythonCreateScriptedThread(
286277
[&](const llvm::ErrorInfoBase &E) {
287278
error_string.append(E.message());
288279
});
289-
Py_RETURN_NONE;
280+
return PythonObject();
290281
}
291282

292-
PythonObject result = {};
293-
if (arg_info.get().max_positional_args == 2) {
294-
result = pfunc(ToSWIGWrapper(process_sp), ToSWIGWrapper(args_impl));
295-
} else {
296-
error_string.assign("wrong number of arguments in __init__, should be 2 "
297-
"(not including self)");
298-
Py_RETURN_NONE;
299-
}
283+
if (arg_info.get().max_positional_args == 2)
284+
return pfunc(ToSWIGWrapper(process_sp), ToSWIGWrapper(args_impl));
300285

301-
if (result.IsAllocated())
302-
return result.release();
303-
Py_RETURN_NONE;
286+
error_string.assign("wrong number of arguments in __init__, should be 2 "
287+
"(not including self)");
288+
return PythonObject();
304289
}
305290

306-
void *lldb_private::LLDBSwigPythonCreateScriptedThreadPlan(
291+
PythonObject lldb_private::LLDBSwigPythonCreateScriptedThreadPlan(
307292
const char *python_class_name, const char *session_dictionary_name,
308293
const lldb_private::StructuredDataImpl &args_impl,
309294
std::string &error_string, const lldb::ThreadPlanSP &thread_plan_sp) {
310295
if (python_class_name == NULL || python_class_name[0] == '\0' ||
311296
!session_dictionary_name)
312-
Py_RETURN_NONE;
297+
return PythonObject();
313298

314299
PyErr_Cleaner py_err_cleaner(true);
315300

@@ -321,7 +306,7 @@ void *lldb_private::LLDBSwigPythonCreateScriptedThreadPlan(
321306
if (!pfunc.IsAllocated()) {
322307
error_string.append("could not find script class: ");
323308
error_string.append(python_class_name);
324-
return nullptr;
309+
return PythonObject();
325310
}
326311

327312
PythonObject tp_arg = ToSWIGWrapper(thread_plan_sp);
@@ -334,7 +319,7 @@ void *lldb_private::LLDBSwigPythonCreateScriptedThreadPlan(
334319
[&](const llvm::ErrorInfoBase &E) {
335320
error_string.append(E.message());
336321
});
337-
Py_RETURN_NONE;
322+
return PythonObject();
338323
}
339324

340325
PythonObject result = {};
@@ -343,23 +328,21 @@ void *lldb_private::LLDBSwigPythonCreateScriptedThreadPlan(
343328
if (args_sb->IsValid()) {
344329
error_string.assign(
345330
"args passed, but __init__ does not take an args dictionary");
346-
Py_RETURN_NONE;
331+
return PythonObject();
347332
}
348333
result = pfunc(tp_arg, dict);
349334
} else if (arg_info.get().max_positional_args >= 3) {
350335
result = pfunc(tp_arg, ToSWIGWrapper(std::move(args_sb)), dict);
351336
} else {
352337
error_string.assign("wrong number of arguments in __init__, should be 2 or "
353338
"3 (not including self)");
354-
Py_RETURN_NONE;
339+
return PythonObject();
355340
}
356341

357342
// FIXME: At this point we should check that the class we found supports all
358343
// the methods that we need.
359344

360-
if (result.IsAllocated())
361-
return result.release();
362-
Py_RETURN_NONE;
345+
return result;
363346
}
364347

365348
bool lldb_private::LLDBSWIGPythonCallThreadPlan(
@@ -400,14 +383,14 @@ bool lldb_private::LLDBSWIGPythonCallThreadPlan(
400383
return false;
401384
}
402385

403-
void *lldb_private::LLDBSwigPythonCreateScriptedBreakpointResolver(
386+
PythonObject lldb_private::LLDBSwigPythonCreateScriptedBreakpointResolver(
404387
const char *python_class_name, const char *session_dictionary_name,
405388
const StructuredDataImpl &args_impl,
406389
const lldb::BreakpointSP &breakpoint_sp) {
407390

408391
if (python_class_name == NULL || python_class_name[0] == '\0' ||
409392
!session_dictionary_name)
410-
Py_RETURN_NONE;
393+
return PythonObject();
411394

412395
PyErr_Cleaner py_err_cleaner(true);
413396

@@ -417,7 +400,7 @@ void *lldb_private::LLDBSwigPythonCreateScriptedBreakpointResolver(
417400
python_class_name, dict);
418401

419402
if (!pfunc.IsAllocated())
420-
return nullptr;
403+
return PythonObject();
421404

422405
PythonObject result =
423406
pfunc(ToSWIGWrapper(breakpoint_sp), ToSWIGWrapper(args_impl), dict);
@@ -428,11 +411,9 @@ void *lldb_private::LLDBSwigPythonCreateScriptedBreakpointResolver(
428411
// Check that __callback__ is defined:
429412
auto callback_func = result.ResolveName<PythonCallable>("__callback__");
430413
if (callback_func.IsAllocated())
431-
return result.release();
432-
else
433-
result.release();
414+
return result;
434415
}
435-
Py_RETURN_NONE;
416+
return PythonObject();
436417
}
437418

438419
unsigned int lldb_private::LLDBSwigPythonCallBreakpointResolver(
@@ -474,17 +455,17 @@ unsigned int lldb_private::LLDBSwigPythonCallBreakpointResolver(
474455
return ret_val;
475456
}
476457

477-
void *lldb_private::LLDBSwigPythonCreateScriptedStopHook(
458+
PythonObject lldb_private::LLDBSwigPythonCreateScriptedStopHook(
478459
lldb::TargetSP target_sp, const char *python_class_name,
479460
const char *session_dictionary_name, const StructuredDataImpl &args_impl,
480461
Status &error) {
481462
if (python_class_name == NULL || python_class_name[0] == '\0') {
482463
error.SetErrorString("Empty class name.");
483-
Py_RETURN_NONE;
464+
return PythonObject();
484465
}
485466
if (!session_dictionary_name) {
486467
error.SetErrorString("No session dictionary");
487-
Py_RETURN_NONE;
468+
return PythonObject();
488469
}
489470

490471
PyErr_Cleaner py_err_cleaner(true);
@@ -497,7 +478,7 @@ void *lldb_private::LLDBSwigPythonCreateScriptedStopHook(
497478
if (!pfunc.IsAllocated()) {
498479
error.SetErrorStringWithFormat("Could not find class: %s.",
499480
python_class_name);
500-
return nullptr;
481+
return PythonObject();
501482
}
502483

503484
PythonObject result =
@@ -514,23 +495,22 @@ void *lldb_private::LLDBSwigPythonCreateScriptedStopHook(
514495
"Wrong number of args for "
515496
"handle_stop callback, should be 2 (excluding self), got: %zu",
516497
num_args);
517-
Py_RETURN_NONE;
498+
return PythonObject();
518499
} else
519-
return result.release();
500+
return result;
520501
} else {
521502
error.SetErrorString("Couldn't get num arguments for handle_stop "
522503
"callback.");
523-
Py_RETURN_NONE;
504+
return PythonObject();
524505
}
525-
return result.release();
506+
return result;
526507
} else {
527508
error.SetErrorStringWithFormat("Class \"%s\" is missing the required "
528509
"handle_stop callback.",
529510
python_class_name);
530-
result.release();
531511
}
532512
}
533-
Py_RETURN_NONE;
513+
return PythonObject();
534514
}
535515

536516
bool lldb_private::LLDBSwigPythonStopHookCallHandleStop(
@@ -842,12 +822,12 @@ bool lldb_private::LLDBSwigPythonCallCommandObject(
842822
return true;
843823
}
844824

845-
void *lldb_private::LLDBSWIGPythonCreateOSPlugin(
825+
PythonObject lldb_private::LLDBSWIGPythonCreateOSPlugin(
846826
const char *python_class_name, const char *session_dictionary_name,
847827
const lldb::ProcessSP &process_sp) {
848828
if (python_class_name == NULL || python_class_name[0] == '\0' ||
849829
!session_dictionary_name)
850-
Py_RETURN_NONE;
830+
return PythonObject();
851831

852832
PyErr_Cleaner py_err_cleaner(true);
853833

@@ -857,21 +837,16 @@ void *lldb_private::LLDBSWIGPythonCreateOSPlugin(
857837
python_class_name, dict);
858838

859839
if (!pfunc.IsAllocated())
860-
Py_RETURN_NONE;
861-
862-
auto result = pfunc(ToSWIGWrapper(process_sp));
863-
864-
if (result.IsAllocated())
865-
return result.release();
840+
return PythonObject();
866841

867-
Py_RETURN_NONE;
842+
return pfunc(ToSWIGWrapper(process_sp));
868843
}
869844

870-
void *lldb_private::LLDBSWIGPython_CreateFrameRecognizer(
845+
PythonObject lldb_private::LLDBSWIGPython_CreateFrameRecognizer(
871846
const char *python_class_name, const char *session_dictionary_name) {
872847
if (python_class_name == NULL || python_class_name[0] == '\0' ||
873848
!session_dictionary_name)
874-
Py_RETURN_NONE;
849+
return PythonObject();
875850

876851
PyErr_Cleaner py_err_cleaner(true);
877852

@@ -881,14 +856,9 @@ void *lldb_private::LLDBSWIGPython_CreateFrameRecognizer(
881856
python_class_name, dict);
882857

883858
if (!pfunc.IsAllocated())
884-
Py_RETURN_NONE;
885-
886-
auto result = pfunc();
887-
888-
if (result.IsAllocated())
889-
return result.release();
859+
return PythonObject();
890860

891-
Py_RETURN_NONE;
861+
return pfunc();
892862
}
893863

894864
PyObject *lldb_private::LLDBSwigPython_GetRecognizedArguments(

lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ StructuredData::ObjectSP PythonObject::CreateStructuredObject() const {
280280
case PyObjectType::None:
281281
return StructuredData::ObjectSP();
282282
default:
283-
return StructuredData::ObjectSP(new StructuredPythonObject(m_py_obj));
283+
return StructuredData::ObjectSP(new StructuredPythonObject(
284+
PythonObject(PyRefType::Borrowed, m_py_obj)));
284285
}
285286
}
286287

0 commit comments

Comments
 (0)