forked from pyscripter/pyscripter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cPyBaseDebugger.pas
1317 lines (1197 loc) · 42.5 KB
/
cPyBaseDebugger.pas
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-----------------------------------------------------------------------------
Unit Name: cPyBaseDebugger
Author: Kiriakos Vlahos
Date: 23-Apr-2006
Purpose:
History: Base debugger classes
-----------------------------------------------------------------------------}
unit cPyBaseDebugger;
interface
uses
Windows, SysUtils, Classes, uEditAppIntfs, PythonEngine, Forms,
Contnrs, cTools, cPythonSourceScanner;
type
TPythonEngineType = (peInternal, peRemote, peRemoteTk, peRemoteWx);
TDebuggerState = (dsInactive, dsRunning, dsPaused, dsRunningNoDebug, dsPostMortem);
TDebuggerCommand = (dcNone, dcRun, dcStepInto, dcStepOver, dcStepOut,
dcRunToCursor, dcPause, dcAbort);
TDebuggerLineInfo = (dlCurrentLine,
dlBreakpointLine,
dlDisabledBreakpointLine,
dlExecutableLine,
dlErrorLine);
TDebuggerLineInfos = set of TDebuggerLineInfo;
TInterpreterCapability = (icReInitialize);
TInterpreterCapabilities = set of TInterpreterCapability;
TNamespaceItemAttribute = (nsaNew, nsaChanged);
TNamespaceItemAttributes = set of TNamespaceItemAttribute;
TBreakpointChangeEvent = procedure(Sender: TObject; Editor : IEditor; ALine: integer) of object;
TDebuggerStateChangeEvent = procedure(Sender: TObject;
OldState, NewState: TDebuggerState) of object;
TDebuggerYieldEvent = procedure(Sender: TObject; DoIdle : Boolean) of object;
TRunConfiguration = class(TPersistent)
private
fScriptName: string;
fEngineType: TPythonEngineType;
fWorkingDir: string;
fParameters: string;
fReinitializeBeforeRun: Boolean;
fOutputFileName: string;
fWriteOutputToFile: Boolean;
fAppendToFile: Boolean;
fExternalRun: TExternalRun;
fDescription: string;
procedure SetExternalRun(const Value: TExternalRun);
public
constructor Create;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
published
property ScriptName : string read fScriptName write fScriptName;
property Description : string read fDescription write fDescription;
property EngineType : TPythonEngineType read fEngineType write fEngineType;
property ReinitializeBeforeRun : Boolean read fReinitializeBeforeRun
write fReinitializeBeforeRun;
property Parameters : string read fParameters write fParameters;
property WorkingDir : string read fWorkingDir write fWorkingDir;
property WriteOutputToFile : Boolean read fWriteOutputToFile
write fWriteOutputToFile;
property OutputFileName : string read fOutputFileName write fOutputFileName;
property AppendToFile : Boolean read fAppendToFile write fAppendToFile;
property ExternalRun : TExternalRun read fExternalRun write SetExternalRun;
end;
TEditorPos = class(TPersistent)
public
Editor : IEditor;
Line : integer;
Char : integer;
IsSyntax : Boolean;
ErrorMsg : string;
procedure Clear;
procedure Assign(Source: TPersistent); override;
end;
TBaseFrameInfo = class(TObject)
// Base (abstract) class for Call Stack frame information
protected
function GetFunctionName : string; virtual; abstract;
function GetFileName : string; virtual; abstract;
function GetLine : integer; virtual; abstract;
public
property FunctionName : string read GetFunctionName;
property FileName : string read GetFileName;
property Line : integer read GetLine;
end;
TBaseNameSpaceItem = class(TObject)
// Base (abstract) class for Namespace item information
protected
fPyObject : Variant;
fExpandCommonTypes : Boolean;
fExpandSequences : Boolean;
GotChildNodes : Boolean;
GotBufferedValue : Boolean;
BufferedValue : string;
function GetOrCalculateValue : string;
function GetName : string; virtual; abstract;
function GetObjectType : string; virtual; abstract;
function GetValue : string; virtual; abstract;
function GetDocString : string; virtual; abstract;
function GetChildCount : integer; virtual; abstract;
function GetChildNode(Index: integer): TBaseNameSpaceItem; virtual; abstract;
public
Attributes : TNamespaceItemAttributes;
function IsClass : Boolean; virtual; abstract;
function IsDict : Boolean; virtual; abstract;
function IsModule : Boolean; virtual; abstract;
function IsFunction : Boolean; virtual; abstract;
function IsMethod : Boolean; virtual; abstract;
function Has__dict__ : Boolean; virtual; abstract;
function IndexOfChild(AName : string): integer; virtual; abstract;
procedure GetChildNodes; virtual; abstract;
procedure CompareToOldItem(OldItem : TBaseNameSpaceItem); virtual;
property Name : string read GetName;
property ObjectType : string read GetObjectType;
property Value : string read GetOrCalculateValue;
property DocString : string read GetDocString;
property ChildCount : integer read GetChildCount;
property ChildNode[Index : integer] : TBaseNameSpaceItem
read GetChildNode;
property PyObject : Variant read fPyObject;
property ExpandCommonTypes : Boolean read fExpandCommonTypes write fExpandCommonTypes;
property ExpandSequences : Boolean read fExpandSequences write fExpandSequences;
end;
TModuleProxy = class;
TPyBaseInterpreter = class(TObject)
// Base (abstract) class for implementing Python Interpreters
private
function GetMainModule: TModuleProxy;
protected
fInterpreterCapabilities : TInterpreterCapabilities;
fEngineType : TPythonEngineType;
fMainModule : TModuleProxy;
procedure CreateMainModule; virtual; abstract;
public
destructor Destroy; override;
procedure Initialize; virtual;
// Python Path
function SysPathAdd(const Path : string) : boolean; virtual; abstract;
function SysPathRemove(const Path : string) : boolean; virtual; abstract;
function AddPathToPythonPath(const Path : string; AutoRemove : Boolean = True) : IInterface;
procedure SysPathToStrings(Strings : TStrings); virtual; abstract;
procedure StringsToSysPath(Strings : TStrings); virtual; abstract;
// NameSpace
function GetGlobals : TBaseNameSpaceItem; virtual; abstract;
procedure GetModulesOnPath(Path : Variant; SL : TStrings); virtual; abstract;
function NameSpaceFromExpression(const Expr : string) : TBaseNameSpaceItem; virtual; abstract;
function CallTipFromExpression(const Expr : string;
var DisplayString, DocString : string) : Boolean; virtual; abstract;
// Service routines
procedure HandlePyException(E : EPythonError; SkipFrames : integer = 1); virtual;
procedure SetCommandLine(ARunConfig : TRunConfiguration); virtual; abstract;
procedure RestoreCommandLine; virtual; abstract;
procedure ReInitialize; virtual;
// Main interface
function ImportModule(Editor : IEditor; AddToNameSpace : Boolean = False) : Variant; virtual; abstract;
procedure RunNoDebug(ARunConfig : TRunConfiguration); virtual; abstract;
function RunSource(Const Source, FileName : Variant; symbol : string = 'single') : boolean; virtual; abstract;
procedure RunScript(FileName : string); virtual;
function EvalCode(const Expr : string) : Variant; virtual; abstract;
function GetObjectType(Ob : Variant) : string; virtual; abstract;
function UnitTestResult : Variant; virtual; abstract;
function NameSpaceItemFromPyObject(aName : string; aPyObject : Variant): TBaseNameSpaceItem; virtual; abstract;
property EngineType : TPythonEngineType read fEngineType;
property InterpreterCapabilities : TInterpreterCapabilities read fInterpreterCapabilities;
property MainModule : TModuleProxy read GetMainModule;
end;
TPyBaseDebugger = class(TObject)
{ Base (abstract) class for implementing Python Debuggers }
protected
procedure SetCommandLine(ARunConfig : TRunConfiguration); virtual; abstract;
procedure RestoreCommandLine; virtual; abstract;
procedure SetDebuggerBreakpoints; virtual; abstract;
public
// Python Path
function SysPathAdd(const Path : string) : boolean; virtual; abstract;
function SysPathRemove(const Path : string) : boolean; virtual; abstract;
function AddPathToPythonPath(const Path : string; AutoRemove : Boolean = True) : IInterface;
// Debugging
procedure Run(ARunConfig : TRunConfiguration; InitStepIn : Boolean = False;
RunToCursorLine : integer = -1); virtual; abstract;
procedure RunToCursor(Editor : IEditor; ALine: integer); virtual; abstract;
procedure StepInto; virtual; abstract;
procedure StepOver; virtual; abstract;
procedure StepOut; virtual; abstract;
procedure Resume; virtual; abstract;
procedure Pause; virtual; abstract;
procedure Abort; virtual; abstract;
// Evaluate expression in the current frame
procedure Evaluate(const Expr : string; out ObjType, Value : string); overload; virtual; abstract;
function Evaluate(const Expr : string) : TBaseNamespaceItem; overload; virtual; abstract;
// Like the InteractiveInterpreter runsource but for the debugger frame
function RunSource(Const Source, FileName : Variant; symbol : string = 'single') : boolean; virtual; abstract;
// Fills in CallStackList with TBaseFrameInfo objects
procedure GetCallStack(CallStackList : TObjectList); virtual; abstract;
// functions to get TBaseNamespaceItems corresponding to a frame's gloabals and locals
function GetFrameGlobals(Frame : TBaseFrameInfo) : TBaseNameSpaceItem; virtual; abstract;
function GetFrameLocals(Frame : TBaseFrameInfo) : TBaseNameSpaceItem; virtual; abstract;
function NameSpaceFromExpression(const Expr : string) : TBaseNameSpaceItem; virtual; abstract;
procedure MakeFrameActive(Frame : TBaseFrameInfo); virtual; abstract;
// post mortem stuff
function HaveTraceback : boolean; virtual; abstract;
procedure EnterPostMortem; virtual; abstract;
procedure ExitPostMortem; virtual; abstract;
end;
TPythonControl = class(Tobject)
{
Interface between PyScripter and the Interpreter/Debugger.
Holds information Breakpoints, ErrorPos, CurrentPos
}
private
fBreakPointsChanged : Boolean;
fDebuggerState: TDebuggerState;
fErrorPos : TEditorPos;
fCurrentPos : TEditorPos;
fOnBreakpointChange: TBreakpointChangeEvent;
fOnCurrentPosChange: TNotifyEvent;
fOnErrorPosChange: TNotifyEvent;
fOnStateChange: TDebuggerStateChangeEvent;
fOnYield: TDebuggerYieldEvent;
fActiveInterpreter : TPyBaseInterpreter;
fActiveDebugger : TPyBaseDebugger ;
fRunConfig : TRunConfiguration;
FPythonVersionIndex: integer;
procedure DoOnBreakpointChanged(Editor : IEditor; ALine: integer);
procedure SetActiveDebugger(const Value: TPyBaseDebugger);
procedure SetActiveInterpreter(const Value: TPyBaseInterpreter);
function GetPythonEngineType: TPythonEngineType;
procedure SetPythonEngineType(const Value: TPythonEngineType);
procedure SetRunConfig(ARunConfig: TRunConfiguration);
procedure PrepareRun;
public
// ActiveInterpreter and ActiveDebugger are created
// and destroyed in frmPythonII
constructor Create;
destructor Destroy; override;
// Breakpoint related
procedure ToggleBreakpoint(Editor : IEditor; ALine: integer;
CtrlPressed : Boolean = False);
procedure SetBreakPoint(FileName : string; ALine : integer;
Disabled : Boolean; Condition : string);
procedure ClearAllBreakpoints;
// Editor related
function GetLineInfos(Editor : IEditor; ALine: integer): TDebuggerLineInfos;
function IsBreakpointLine(Editor: IEditor; ALine: integer;
var Disabled : boolean): boolean;
function IsExecutableLine(Editor: IEditor; ALine: integer): boolean;
// Event processing
procedure DoCurrentPosChanged;
procedure DoErrorPosChanged;
procedure DoStateChange(NewState : TDebuggerState);
procedure DoYield(DoIdle : Boolean);
// Other
function IsRunning: boolean;
// Running Python Scripts
procedure Run(ARunConfig : TRunConfiguration);
procedure Debug(ARunConfig : TRunConfiguration; InitStepIn : Boolean = False;
RunToCursorLine : integer = -1);
procedure ExternalRun(ARunConfig : TRunConfiguration);
// properties and events
// PythonVersionIndex is the Index of Python version in the PYTHON_KNOWN_VERSIONS array
property PythonVersionIndex : integer read FPythonVersionIndex write FPythonVersionIndex;
property PythonEngineType : TPythonEngineType read GetPythonEngineType
write SetPythonEngineType;
property ActiveInterpreter : TPyBaseInterpreter read fActiveInterpreter
write SetActiveInterpreter;
property ActiveDebugger : TPyBaseDebugger read fActiveDebugger
write SetActiveDebugger;
property BreakPointsChanged : Boolean read fBreakPointsChanged
write fBreakPointsChanged;
property DebuggerState : TDebuggerState read fDebuggerState;
property ErrorPos: TEditorPos read fErrorPos;
property CurrentPos: TEditorPos read fCurrentPos;
property RunConfig : TRunConfiguration read fRunConfig;
property OnBreakpointChange: TBreakpointChangeEvent read fOnBreakpointChange
write fOnBreakpointChange;
property OnCurrentPosChange: TNotifyEvent read fOnCurrentPosChange
write fOnCurrentPosChange;
property OnErrorPosChange: TNotifyEvent read fOnErrorPosChange
write fOnErrorPosChange;
property OnStateChange: TDebuggerStateChangeEvent read fOnStateChange
write fOnStateChange;
property OnYield: TDebuggerYieldEvent read fOnYield write fOnYield;
end;
TModuleProxy = class(TParsedModule)
private
fPyModule : Variant;
fIsExpanded : boolean;
fPyInterpreter: TPyBaseInterpreter;
protected
function GetAllExportsVar: string; override;
function GetDocString: string; override;
function GetCodeHint : string; override;
public
constructor CreateFromModule(AModule : Variant; aPyInterpreter : TPyBaseInterpreter);
procedure Expand;
procedure GetNameSpace(SList : TStringList); override;
property PyModule : Variant read fPyModule;
property IsExpanded : boolean read fIsExpanded;
property Interpreter: TPyBaseInterpreter read fPyInterpreter;
end;
TClassProxy = class(TParsedClass)
private
fPyClass : Variant;
fIsExpanded : boolean;
protected
function GetDocString: string; override;
public
constructor CreateFromClass(AName : string; AClass : Variant);
function GetConstructor : TParsedFunction; override;
procedure Expand;
procedure GetNameSpace(SList : TStringList); override;
property PyClass : Variant read fPyClass;
property IsExpanded : boolean read fIsExpanded;
end;
TFunctionProxy = class(TParsedFunction)
private
fPyFunction : Variant;
fIsExpanded : boolean;
protected
function GetDocString: string; override;
public
constructor CreateFromFunction(AName : string; AFunction : Variant);
procedure Expand;
function ArgumentsString : string; override;
procedure GetNameSpace(SList : TStringList); override;
property PyFunction : Variant read fPyFunction;
property IsExpanded : boolean read fIsExpanded;
end;
TVariableProxy = class(TCodeElement)
private
fPyObject : Variant;
fIsExpanded : boolean;
protected
function GetDocString: string; override;
function GetCodeHint : string; override;
public
constructor CreateFromPyObject(const AName : string; AnObject : Variant);
procedure Expand;
procedure GetNameSpace(SList : TStringList); override;
property PyObject : Variant read fPyObject;
property IsExpanded : boolean read fIsExpanded;
end;
Const
CommonTypes: array[1..29] of TIdentMapEntry = (
(Value: 0; Name: 'NoneType'),
(Value: 1; Name: 'NotImplementedType'),
(Value: 2; Name: 'bool'),
(Value: 3; Name: 'buffer'),
(Value: 4; Name: 'builtin_function_or_method'),
(Value: 5; Name: 'code' ),
(Value: 6; Name: 'complex'),
(Value: 7; Name: 'dict'),
(Value: 8; Name: 'dictproxy'),
(Value: 9; Name: 'ellipsis'),
(Value: 10; Name: 'file'),
(Value: 11; Name: 'float'),
(Value: 12; Name: 'frame'),
(Value: 13; Name: 'function'),
(Value: 14; Name: 'generator'),
(Value: 15; Name: 'getset_descriptor'),
(Value: 16; Name: 'instancemethod'),
(Value: 17; Name: 'int'),
(Value: 18; Name: 'list'),
(Value: 19; Name: 'long'),
(Value: 20; Name: 'member_descriptor'),
(Value: 21; Name: 'method-wrapper'),
(Value: 22; Name: 'object'),
(Value: 23; Name: 'slice'),
(Value: 24; Name: 'str'),
(Value: 25; Name: 'traceback'),
(Value: 26; Name: 'tuple'),
(Value: 27; Name: 'unicode'),
(Value: 28; Name: 'xrange')
);
var
PyControl : TPythonControl = nil;
const
EngineInitFile = 'python_init.py';
PyScripterInitFile = 'pyscripter_init.py';
implementation
uses dmCommands, frmPythonII, frmMessages, frmPyIDEMain,
uCommonFunctions, VarPyth,
cParameters, StringResources, cPyDebugger,
frmCommandOutput, gnugettext, cProjectClasses, Dialogs;
{ TEditorPos }
procedure TEditorPos.Assign(Source: TPersistent);
begin
if Source is TEditorPos then begin
Self.Editor := TEditorPos(Source).Editor;
Self.Line := TEditorPos(Source).Line;
Self.Char := TEditorPos(Source).Char;
Self.IsSyntax := TEditorPos(Source).IsSyntax;
Self.ErrorMsg := TEditorPos(Source).ErrorMsg;
end else
inherited;
end;
procedure TEditorPos.Clear;
begin
Editor := nil;
Line := -1;
Char := -1;
IsSyntax := False;
ErrorMsg := '';
end;
{ TPythonPathAdder }
type
TSysPathFunction = function(const Path : string) : boolean of object;
TPythonPathAdder = class(TInterfacedObject, IInterface)
private
fPath : string;
fPathAdded : boolean;
PackageRootAdder : IInterface;
fAutoRemove : Boolean;
fSysPathRemove : TSysPathFunction;
public
constructor Create(SysPathAdd, SysPathRemove : TSysPathFunction;
const Path : string; AutoRemove : Boolean = True);
destructor Destroy; override;
end;
constructor TPythonPathAdder.Create(SysPathAdd, SysPathRemove : TSysPathFunction;
const Path: string; AutoRemove : Boolean = True);
var
S : string;
begin
inherited Create;
fPath := ExcludeTrailingPathDelimiter(Path);
fAutoRemove := AutoRemove;
fSysPathRemove := SysPathRemove;
if (fPath <> '') and DirectoryExists(fPath) then begin
// Add parent directory of the root of the package first
if DirIsPythonPackage(fPath) then begin
S := ExtractFileDir(GetPackageRootDir(fPath));
if S <> fPath then
PackageRootAdder :=
TPythonPathAdder.Create(SysPathAdd, SysPathRemove, S, AutoRemove);
end;
fPathAdded := SysPathAdd(fPath);
end;
end;
destructor TPythonPathAdder.Destroy;
begin
PackageRootAdder := nil; // will remove package root
if fPathAdded and FAutoRemove then
fSysPathRemove(fPath);
inherited;
end;
{ TPyBaseInterpreter }
function TPyBaseInterpreter.AddPathToPythonPath(const Path: string;
AutoRemove: Boolean): IInterface;
begin
Result := TPythonPathAdder.Create(SysPathAdd, SysPathRemove, Path, AutoRemove);
end;
destructor TPyBaseInterpreter.Destroy;
begin
FreeAndNil(fMainModule);
inherited;
end;
function TPyBaseInterpreter.GetMainModule: TModuleProxy;
begin
if not Assigned(fMainModule) then
CreateMainModule;
Result := fMainModule;
end;
procedure TPyBaseInterpreter.HandlePyException(E: EPythonError; SkipFrames : integer = 1);
Var
TI : TTracebackItem;
FileName : string;
Editor : IEditor;
begin
MessagesWindow.ShowPythonTraceback(SkipFrames);
MessagesWindow.AddMessage(E.Message);
with GetPythonEngine.Traceback do begin
if ItemCount > 0 then begin
TI := Items[ItemCount -1];
FileName := TI.FileName;
if (FileName[1] ='<') and (FileName[Length(FileName)] = '>') then
FileName := Copy(FileName, 2, Length(FileName)-2);
Editor := GI_EditorFactory.GetEditorByNameOrTitle(FileName);
// Check whether the error occurred in the active editor
if (Assigned(Editor) and (Editor = PyIDEMainForm.GetActiveEditor)) or
CommandsDataModule.PyIDEOptions.JumpToErrorOnException then
begin
if PyIDEMainForm.ShowFilePosition(TI.FileName, TI.LineNo, 1) and
Assigned(GI_ActiveEditor)
then begin
PyControl.ErrorPos.Editor := GI_ActiveEditor;
PyControl.ErrorPos.Line := TI.LineNo;
PyControl.DoErrorPosChanged;
end;
end;
end;
end;
end;
procedure TPyBaseInterpreter.Initialize;
// Execute python_init.py
Var
FileName : String;
begin
FileName := CommandsDataModule.UserDataPath + EngineInitFile;
try
RunScript(FileName);
except
on E: Exception do
Dialogs.MessageDlg(Format(_(SErrorInitScript),
[EngineInitFile, E.Message]), mtError, [mbOK], 0);
end;
end;
procedure TPyBaseInterpreter.ReInitialize;
begin
raise Exception.Create(_(SNotImplented));
end;
procedure TPyBaseInterpreter.RunScript(FileName: string);
Var
Source : string;
AnsiSource : AnsiString;
begin
// Execute pyscripterEngineSetup.py
if FileExists(FileName) then begin
if GetPythonEngine.IsPython3000 then begin
Source := CleanEOLs(FileToStr(FileName))+#10;
RunSource(Source, FileName, 'exec');
end else begin
AnsiSource := CleanEOLs(FileToEncodedStr(FileName))+#10;
RunSource(AnsiSource, FileName, 'exec');
end;
end;
end;
{ TBaseNameSpaceItem }
procedure TBaseNameSpaceItem.CompareToOldItem(OldItem: TBaseNameSpaceItem);
var
i, Index : integer;
Child : TBaseNameSpaceItem;
begin
if OldItem.GotBufferedValue then begin
if OldItem.BufferedValue <> Value then
Attributes := [nsaChanged];
end;
if OldItem.GotChildNodes then begin
GetChildNodes;
for i := 0 to ChildCount - 1 do begin
Child := ChildNode[i];
Index := OldItem.IndexOfChild(Child.Name);
if Index >= 0 then
Child.CompareToOldItem(OldItem.ChildNode[Index])
else
Child.Attributes := [nsaNew];
end;
end;
end;
function TBaseNameSpaceItem.GetOrCalculateValue: string;
begin
if GotBufferedValue then
Result := BufferedValue
else begin
BufferedValue := GetValue;
GotBufferedValue := True;
Result := BufferedValue;
end;
end;
{ TPythonControl }
constructor TPythonControl.Create;
begin
fDebuggerState := dsInactive;
fCurrentPos := TEditorPos.Create;
fCurrentPos.Clear;
fErrorPos := TEditorPos.Create;
fErrorPos.Clear;
fRunConfig := TRunConfiguration.Create;
end;
procedure TPythonControl.Debug(ARunConfig: TRunConfiguration; InitStepIn : Boolean = False;
RunToCursorLine : integer = -1);
begin
SetRunConfig(ARunConfig);
if not Assigned(ActiveDebugger) then Exit;
PrepareRun;
if fRunConfig.WriteOutputToFile then
PythonIIForm.StartOutputMirror(Parameters.ReplaceInText(fRunConfig.OutputFileName),
fRunConfig.AppendToFile);
try
ActiveDebugger.Run(fRunConfig, InitStepIn, RunToCursorLine);
finally
if fRunConfig.WriteOutputToFile then
PythonIIForm.StopFileMirror;
end;
end;
destructor TPythonControl.Destroy;
begin
fCurrentPos.Free;
fErrorPos.Free;
fRunConfig.Free;
inherited;
end;
function TPythonControl.GetLineInfos(Editor : IEditor; ALine: integer): TDebuggerLineInfos;
Var
Disabled : boolean;
begin
Result := [];
if ALine > 0 then begin
if (Editor = PyControl.CurrentPos.Editor) and (ALine = PyControl.CurrentPos.Line) then
Include(Result, dlCurrentLine);
if (Editor = PyControl.ErrorPos.Editor) and (ALine = PyControl.ErrorPos.Line) then
Include(Result, dlErrorLine);
if IsExecutableLine(Editor, ALine) then
Include(Result, dlExecutableLine);
Disabled := False;
if IsBreakpointLine(Editor, ALine, Disabled) then
if Disabled then
Include(Result, dlDisabledBreakpointLine)
else
Include(Result, dlBreakpointLine);
end;
end;
function TPythonControl.GetPythonEngineType: TPythonEngineType;
begin
if Assigned(ActiveInterpreter) then
Result := ActiveInterpreter.EngineType
else
Result := peInternal;
end;
function TPythonControl.IsBreakpointLine(Editor: IEditor; ALine: integer;
var Disabled : boolean): boolean;
Var
i: integer;
begin
Result := FALSE;
if ALine > 0 then begin
i := Editor.Breakpoints.Count - 1;
while i >= 0 do begin
if TBreakPoint(Editor.Breakpoints[i]).LineNo = ALine then begin
Disabled := TBreakPoint(Editor.Breakpoints[i]).Disabled;
Result := TRUE;
break;
end;
Dec(i);
end;
end;
end;
function TPythonControl.IsExecutableLine(Editor: IEditor; ALine: integer): boolean;
begin
Assert(Assigned(Editor));
with Editor.SynEdit do begin
Result := CommandsDataModule.IsExecutableLine(Lines[ALine-1]);
end;
end;
procedure TPythonControl.ToggleBreakpoint(Editor : IEditor; ALine: integer;
CtrlPressed : Boolean = False);
var
Index : integer;
i: integer;
BreakPoint : TBreakPoint;
begin
if ALine > 0 then begin
Index := Editor.Breakpoints.Count; // append at the end
for i := 0 to Editor.Breakpoints.Count - 1 do begin
if TBreakPoint(Editor.Breakpoints[i]).LineNo = ALine then begin
if CtrlPressed then
// Toggle disabled
TBreakPoint(Editor.Breakpoints[i]).Disabled :=
not TBreakPoint(Editor.Breakpoints[i]).Disabled
else
Editor.Breakpoints.Delete(i);
Index := -1;
break;
end else if TBreakPoint(Editor.Breakpoints[i]).LineNo > ALine then begin
Index := i;
break;
end;
end;
if Index >= 0 then begin
BreakPoint := TBreakPoint.Create;
BreakPoint.LineNo := ALine;
if CtrlPressed then
BreakPoint.Disabled := True;
Editor.Breakpoints.Insert(Index, BreakPoint);
end;
DoOnBreakpointChanged(Editor, ALine);
end;
end;
procedure TPythonControl.SetActiveDebugger(const Value: TPyBaseDebugger);
begin
if fActiveDebugger <> Value then begin
if Assigned(fActiveDebugger) then
FreeAndNil(fActiveDebugger);
fActiveDebugger := Value;
end;
end;
procedure TPythonControl.SetActiveInterpreter(const Value: TPyBaseInterpreter);
begin
if fActiveInterpreter <> Value then begin
if Assigned(fActiveInterpreter) and
(fActiveInterpreter <> InternalInterpreter)
then
FreeAndNil(fActiveInterpreter);
fActiveInterpreter := Value;
end;
end;
procedure TPythonControl.SetBreakPoint(FileName: string; ALine: integer;
Disabled : Boolean; Condition: string);
var
Editor : IEditor;
i: integer;
BreakPoint : TBreakPoint;
begin
Editor := GI_EditorFactory.GetEditorByNameOrTitle(FileName);
BreakPoint := nil;
if Assigned(Editor) and (ALine > 0) then begin
for i := 0 to Editor.Breakpoints.Count - 1 do begin
if TBreakPoint(Editor.Breakpoints[i]).LineNo = ALine then begin
BreakPoint := TBreakPoint(Editor.Breakpoints[i]);
break;
end else if TBreakPoint(Editor.Breakpoints[i]).LineNo > ALine then begin
BreakPoint := TBreakPoint.Create;
Editor.Breakpoints.Insert(i, BreakPoint);
break;
end;
end;
if not Assigned(BreakPoint) then begin
BreakPoint := TBreakPoint.Create;
Editor.Breakpoints.Add(BreakPoint);
end;
BreakPoint.LineNo := ALine;
BreakPoint.Disabled := Disabled;
BreakPoint.Condition := Condition;
DoOnBreakpointChanged(Editor, ALine);
end;
end;
procedure TPythonControl.SetPythonEngineType(const Value: TPythonEngineType);
begin
if Value <> PythonEngineType then
PythonIIForm.SetPythonEngineType(Value);
end;
procedure TPythonControl.ClearAllBreakpoints;
Var
i : integer;
begin
for i := 0 to GI_EditorFactory.Count -1 do
if GI_EditorFactory.Editor[i].Breakpoints.Count > 0 then begin
GI_EditorFactory.Editor[i].Breakpoints.Clear;
DoOnBreakpointChanged(GI_EditorFactory.Editor[i], -1);
end;
end;
procedure TPythonControl.DoCurrentPosChanged;
begin
if Assigned(fOnCurrentPosChange) then
fOnCurrentPosChange(Self);
end;
procedure TPythonControl.DoErrorPosChanged;
begin
if Assigned(fOnErrorPosChange) then
fOnErrorPosChange(Self);
end;
procedure TPythonControl.DoOnBreakpointChanged(Editor : IEditor; ALine: integer);
begin
fBreakPointsChanged := True;
if Assigned(fOnBreakpointChange) then
fOnBreakpointChange(Self, Editor, ALine);
end;
procedure TPythonControl.DoStateChange(NewState : TDebuggerState);
Var
OldDebuggerState: TDebuggerState;
begin
OldDebuggerState := fDebuggerState;
if NewState in [dsInactive, dsRunning, dsRunningNoDebug] then
CurrentPos.Clear
else begin
ErrorPos.Clear;
DoErrorPosChanged;
end;
fDebuggerState := NewState;
if Assigned(fOnStateChange) then
fOnStateChange(Self, OldDebuggerState, NewState);
PyControl.DoCurrentPosChanged;
end;
procedure TPythonControl.DoYield(DoIdle : Boolean);
begin
if Assigned(fOnYield) then
fOnYield(Self, DoIdle);
end;
procedure TPythonControl.ExternalRun(ARunConfig: TRunConfiguration);
begin
SetRunConfig(ARunConfig);
OutputWindow.ExecuteTool(fRunConfig.ExternalRun);
end;
procedure TPythonControl.PrepareRun;
begin
if CommandsDataModule.PyIDEOptions.SaveFilesBeforeRun then begin
PyIDEMainForm.SaveFileModules;
// Application.ProcessMessages;
// Application.DoApplicationIdle;
// Application.ProcessMessages;
PyIDEMainForm.Refresh; // To update save flags
end;
if CommandsDataModule.PyIDEOptions.SaveEnvironmentBeforeRun then
PyIDEMainForm.SaveEnvironment;
if CommandsDataModule.PyIDEOptions.ClearOutputBeforeRun then
PythonIIForm.actClearContentsExecute(nil);
if fRunConfig.EngineType <> PythonEngineType then
PythonEngineType := fRunConfig.EngineType
else if (icReInitialize in ActiveInterpreter.InterpreterCapabilities) and
fRunConfig.ReinitializeBeforeRun
then
ActiveInterpreter.ReInitialize;
end;
procedure TPythonControl.SetRunConfig(ARunConfig: TRunConfiguration);
begin
if ARunConfig <> fRunConfig then
begin
fRunConfig.Assign(ARunConfig);
// Expand Parameters in filename
fRunConfig.fScriptName := ''; // to avoid circular substitution
fRunConfig.fScriptName := Parameters.ReplaceInText(ARunConfig.fScriptName);
PyIDEMainForm.SetRunLastScriptHints(fRunConfig.fScriptName);
end;
end;
function TPythonControl.IsRunning: boolean;
begin
Result := fDebuggerState in [dsRunning, dsRunningNoDebug];
end;
procedure TPythonControl.Run(ARunConfig: TRunConfiguration);
begin
SetRunConfig(ARunConfig);
if not Assigned(ActiveInterpreter) then Exit;
PrepareRun;
if fRunConfig.WriteOutputToFile then
PythonIIForm.StartOutputMirror(Parameters.ReplaceInText(fRunConfig.OutputFileName),
fRunConfig.AppendToFile);
try
ActiveInterpreter.RunNoDebug(fRunConfig);
finally
if fRunConfig.WriteOutputToFile then
PythonIIForm.StopFileMirror;
end;
end;
{ TPyBaseDebugger }
function TPyBaseDebugger.AddPathToPythonPath(const Path: string;
AutoRemove: Boolean): IInterface;
begin
Result := TPythonPathAdder.Create(SysPathAdd, SysPathRemove, Path, AutoRemove);
end;
{ TRunConfiguration }
procedure TRunConfiguration.Assign(Source: TPersistent);
begin
if Source is TRunConfiguration then with TRunConfiguration(Source) do begin
Self.fScriptName := ScriptName;
Self.fDescription := Description;
Self.fEngineType := EngineType;
Self.fWorkingDir := WorkingDir;
Self.fParameters := fParameters;
Self.fReinitializeBeforeRun := ReinitializeBeforeRun;
Self.fWriteOutputToFile := WriteOutputToFile;
Self.fOutputFileName := OutputFileName;
Self.fAppendToFile := AppendToFile;
Self.fExternalRun.Assign(fExternalRun);
end else
inherited;
end;
constructor TRunConfiguration.Create;
begin
inherited;
fEngineType := peRemote;
fReinitializeBeforeRun := True;
fOutputFileName := '$[ActiveScript-NoExt].log';
fWorkingDir := '$[ActiveScript-Dir]';
fExternalRun := TExternalRun.Create;
fExternalRun.Assign(ExternalPython);
fExternalRun.Caption := 'External Run';
fExternalRun.Description := 'Run script using an external Python Interpreter';
fExternalRun.Parameters := '$[ActiveScript-Short]';
fExternalRun.WorkingDirectory := '$[ActiveScript-Dir]';
end;
destructor TRunConfiguration.Destroy;
begin
fExternalRun.Free;
inherited;
end;
procedure TRunConfiguration.SetExternalRun(const Value: TExternalRun);
begin
fExternalRun.Assign(Value);
end;
{ TModuleProxy }
procedure TModuleProxy.Expand;
Var
i : integer;
S : string;
VariableProxy : TVariableProxy;
NS, ChildNS : TBaseNameSpaceItem;
begin
if Name = '__main__' then begin
if Assigned(fChildren) then fChildren.Clear;
fGlobals.Clear;
end else if fIsExpanded then
Exit;
NS := Interpreter.NameSpaceItemFromPyObject(Name, fPyModule);
try
for I := 0 to NS.ChildCount - 1 do begin
ChildNS := NS.ChildNode[i];
if ChildNS.IsFunction or ChildNS.IsMethod then
AddChild(TFunctionProxy.CreateFromFunction(ChildNS.Name, ChildNS.PyObject))
else if ChildNS.IsClass then
AddChild(TClassProxy.CreateFromClass(ChildNS.Name, ChildNS.PyObject))
else begin
VariableProxy := TVariableProxy.CreateFromPyObject(ChildNS.Name, ChildNS.PyObject);
VariableProxy.Parent := self;
Globals.Add(VariableProxy);
end;
end;
finally
NS.Free;
end;
fIsExpanded := True;
end;
constructor TModuleProxy.CreateFromModule(AModule: Variant; aPyInterpreter : TPyBaseInterpreter);
begin
inherited Create;