-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathUnit1.pas
214 lines (182 loc) · 5.58 KB
/
Unit1.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
unit Unit1;
interface
uses
Classes, SysUtils,
Windows, Messages, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ExtCtrls,
PythonEngine, Vcl.PythonGUIInputOutput;
type
TForm1 = class(TForm)
PythonEngine1: TPythonEngine;
Memo1: TMemo;
Panel1: TPanel;
Button1: TButton;
Splitter1: TSplitter;
Button2: TButton;
Button3: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
PythonGUIInputOutput1: TPythonGUIInputOutput;
Memo2: TMemo;
PythonModule1: TPythonModule;
PythonType1: TPythonType;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure PythonModule1Events0Execute(Sender: TObject; PSelf,
Args: PPyObject; var Result: PPyObject);
procedure PythonType1Initialization(Sender: TObject);
procedure PythonType1Events0Execute(Sender: TObject; PSelf,
Args: PPyObject; var Result: PPyObject);
private
public
end;
// This is a Delphi class implementing a new Python type
// it must derive from TPyObject or one of its descendants.
// Then it must override some methods, like the constructors,
// the RegisterMethods and the type services' virtual methods.
TPyPoint = class(TPyObject)
x, y : Integer;
// Constructors & Destructors
constructor Create( APythonType : TPythonType ); override;
constructor CreateWith(PythonType: TPythonType; args, kwds: PPyObject); override;
// Type services
////////////////
// Basic services
function GetAttr(key : PAnsiChar) : PPyObject; override;
function SetAttr(key : PAnsiChar; value : PPyObject) : Integer; override;
function Repr : PPyObject; override;
// Methods of TPyPoint
procedure OffsetBy( dx, dy : Integer );
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
// We override the constructors
constructor TPyPoint.Create( APythonType : TPythonType );
begin
inherited;
x := 0;
y := 0;
end;
// Don't call the Create constructor of TPyPoint, because
// we call the inherited constructor CreateWith that calls
// the Create constructor first, and because the constructors
// are virtual, TPyPoint.Create will be automatically be called.
constructor TPyPoint.CreateWith(PythonType: TPythonType; args, kwds: PPyObject);
begin
Create(PythonType);
with GetPythonEngine do
begin
if PyArg_ParseTuple( args, 'ii:CreatePoint',@x, @y ) = 0 then
exit;
end;
end;
// Then we override the needed services
function TPyPoint.GetAttr(key : PAnsiChar) : PPyObject;
begin
with GetPythonEngine do
begin
if key = 'x' then
Result := VariantAsPyObject( x )
// Or Result := PyLong_FromLong( x )
else if key = 'y' then
Result := PyLong_FromLong( y )
// or Result := PyLong_FromLong( y )
else
Result := inherited GetAttr(key);
end;
end;
function TPyPoint.SetAttr(key : PAnsiChar; value : PPyObject) : Integer;
begin
Result := 0;
with GetPythonEngine do
begin
if key = 'x' then
begin
if PyArg_Parse( value, 'i:Point.SetAttr', @x ) = 0 then
Result := -1;
end
else if key = 'y' then
begin
if PyArg_Parse( value, 'i:Point.SetAttr', @y ) = 0 then
Result := -1;
end
else
Result := inherited SetAttr(key, value);
end;
end;
function TPyPoint.Repr : PPyObject;
begin
with GetPythonEngine do
Result := VariantAsPyObject(Format('(%d, %d)',[x, y]));
// or Result := PyUnicodeFromString(Format('(%d, %d)',[x, y]));
end;
// Methods of TPyPoint
// They do the real actions on the object
// It's better to split the functions that interface
// Delphi to Python and the functions that do the
// real implementation.
procedure TPyPoint.OffsetBy( dx, dy : Integer );
begin
Inc( x, dx );
Inc( y, dy );
end;
/////////////////////////////////////////////////
procedure TForm1.Button1Click(Sender: TObject);
begin
PythonEngine1.ExecStrings( Memo1.Lines );
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
with OpenDialog1 do
begin
if Execute then
Memo1.Lines.LoadFromFile( FileName );
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
with SaveDialog1 do
begin
if Execute then
Memo1.Lines.SaveToFile( FileName );
end;
end;
procedure TForm1.PythonModule1Events0Execute(Sender: TObject; PSelf,
Args: PPyObject; var Result: PPyObject);
begin
with GetPythonEngine do
begin
Result := PyUnicodeFromString('Hello world !');
end;
end;
procedure TForm1.PythonType1Initialization(Sender: TObject);
begin
PythonType1.PyObjectClass := TPyPoint;
end;
procedure TForm1.PythonType1Events0Execute(Sender: TObject; PSelf,
Args: PPyObject; var Result: PPyObject);
var
dx, dy : Integer;
Instance : TPyPoint;
begin
with GetPythonEngine do
begin
// Convert the PSelf Python object to a Delphi instance pointer.
Instance := TPyPoint(PythonToDelphi(PSelf));
// first we extract the arguments
if PyArg_ParseTuple( args, 'ii:Point.Offset',@dx, @dy ) <> 0 then
begin
// if it's ok, then we call the method that does the job
// with the correct arguments
Instance.OffsetBy( dx, dy );
// Finally, we return nothing
Result := ReturnNone;
end
else // the arguments were not right
Result := nil;
end;
end;
end.