-
-
Notifications
You must be signed in to change notification settings - Fork 320
Closed
Labels
Description
Hi,
I found that to make the script debuggable by debugpy / VS Code a bit different script execution must be done. Below is a code that works for me. The idea is to use Py_CompileString, PyEval_EvalCode to ensure a proper line info. It shopuld work for other debuggers as well.
function TPythonEngine.ExecCommandPathAsObjectWithDict(const command : AnsiString; const path : String;
mode : Integer; locals, globals : PPyObject) : PPyObject;
var
m : PPyObject;
_locals, _globals : PPyObject;
Resul : PPyObject ;
obj : PPyObject ;
begin
Traceback.Clear;
CheckError(False);
m := GetMainModule;
if m = nil then
raise EPythonError.Create('Run_CommandAsObject: can''t create __main__');
if Assigned(locals) then
_locals := locals
else
if Assigned(FLocalVars) then
_locals := LocalVars
else
_locals := PyModule_GetDict(m);
if Assigned(globals) then
_globals := globals
else
if Assigned(FGlobalVars) then
_globals := GlobalVars
else
_globals := _locals;
try
var o := GetPythonEngine.PyUnicodeFromString( path ) ;
var compiled := Py_CompileString( PAnsiChar(CleanString(command)), PyUnicode_AsUTF8(o), file_input ) ;
Resul := PyEval_EvalCode(compiled, _globals, _locals );
if Resul = nil then
CheckError(False);
except
if PyErr_Occurred <> nil then
CheckError(False)
else
raise;
end;
end;
procedure TPythonEngine.ExecPath(const path : String);
var
lst : TStringList ;
begin
lst := TStringList.Create ;
try
lst.LoadFromFile( path ) ;
ExecStringPath( EncodeString(lst.Text), path )
finally
lst.Free ;
end;
end;
procedure TPythonEngine.ExecStringPath(const command : AnsiString; const path : String);
begin
Py_XDecRef( ExecCommandPathAsObjectWithDict( command, path, file_input, nil, nil ) ) ;
end;
pult