Skip to content

Commit 1673081

Browse files
authored
Merge pull request Embarcadero#87 from Embarcadero/sync
Sync with upstream
2 parents a9ace8e + b738549 commit 1673081

17 files changed

+160
-157
lines changed

Demos/Demo06/Unit1.dfm

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ object Form1: TForm1
1010
Font.Height = -11
1111
Font.Name = 'MS Sans Serif'
1212
Font.Style = []
13-
OldCreateOrder = True
14-
PixelsPerInch = 96
1513
TextHeight = 13
1614
object Splitter1: TSplitter
1715
Left = 0
@@ -20,7 +18,6 @@ object Form1: TForm1
2018
Height = 3
2119
Cursor = crVSplit
2220
Align = alTop
23-
ExplicitWidth = 536
2421
end
2522
object Memo1: TMemo
2623
Left = 0

Demos/Demo06/Unit1.pas

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ procedure PyPoint_dealloc(obj : PPyObject); cdecl;
151151
// object.value
152152
// object.method(args)
153153
function PyPoint_getattr(obj : PPyObject; key : PAnsiChar) : PPyObject; cdecl;
154+
var
155+
Py_Key: PPyObject;
154156
begin
155157
with GetPythonEngine, PPyPoint(obj)^ do
156158
begin
@@ -163,9 +165,14 @@ function PyPoint_getattr(obj : PPyObject; key : PAnsiChar) : PPyObject; cdecl;
163165
else
164166
begin
165167
// Else check for a method
166-
Result := PyObject_GenericGetAttr(obj, PyUnicodeFromString(key));
167-
if not Assigned(Result) then
168-
PyErr_SetString (PyExc_AttributeError^, PAnsiChar(Utf8Encode(Format('Unknown attribute "%s"',[key]))));
168+
Py_Key := PyUnicodeFromString(key);
169+
try
170+
Result := PyObject_GenericGetAttr(obj, Py_key);
171+
if not Assigned(Result) then
172+
PyErr_SetString (PyExc_AttributeError^, PAnsiChar(Utf8Encode(Format('Unknown attribute "%s"',[key]))));
173+
finally
174+
Py_DECREF(Py_Key);
175+
end;
169176
end;
170177
end;
171178
end;

Demos/Demo07/Unit1.dfm

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ object Form1: TForm1
1313
Font.Name = 'MS Sans Serif'
1414
Font.Pitch = fpVariable
1515
Font.Style = []
16-
OldCreateOrder = True
17-
PixelsPerInch = 96
1816
TextHeight = 13
1917
object Splitter1: TSplitter
2018
Left = 0
@@ -23,7 +21,6 @@ object Form1: TForm1
2321
Height = 3
2422
Cursor = crVSplit
2523
Align = alTop
26-
ExplicitWidth = 536
2724
end
2825
object Memo1: TMemo
2926
Left = 0

Demos/Demo07/Unit1.pas

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ procedure PyPoint_dealloc(obj : PPyObject); cdecl;
191191
// object.value
192192
// object.method(args)
193193
function PyPoint_getattr(obj : PPyObject; key : PAnsiChar) : PPyObject; cdecl;
194+
var
195+
Py_Key: PPyObject;
194196
begin
195197
with GetPythonEngine, PPyPoint(obj)^ do
196198
begin
@@ -203,9 +205,14 @@ function PyPoint_getattr(obj : PPyObject; key : PAnsiChar) : PPyObject; cdecl;
203205
else
204206
begin
205207
// Else check for a method
206-
Result := PyObject_GenericGetAttr(obj, PyUnicodeFromString(key));
207-
if not Assigned(Result) then
208-
PyErr_SetString (PyExc_AttributeError^, PAnsiChar(AnsiString(Format('Unknown attribute "%s"',[key]))));
208+
Py_Key := PyUnicodeFromString(key);
209+
try
210+
Result := PyObject_GenericGetAttr(obj, Py_Key);
211+
if not Assigned(Result) then
212+
PyErr_SetString (PyExc_AttributeError^, PAnsiChar(Utf8Encode(Format('Unknown attribute "%s"',[key]))));
213+
finally
214+
Py_DECREF(Py_Key);
215+
end;
209216
end;
210217
end;
211218
end;
@@ -226,7 +233,7 @@ function PyPoint_setattrfunc(obj : PPyObject; key : PAnsiChar; value : PPyObjec
226233
Result := 0;
227234
end
228235
else
229-
PyErr_SetString (PyExc_AttributeError^, PAnsiChar(AnsiString(Format('Attribute "%s" needs an integer',[key]))));
236+
PyErr_SetString (PyExc_AttributeError^, PAnsiChar(Utf8Encode(Format('Attribute "%s" needs an integer',[key]))));
230237
// Check for attribute y
231238
end else if key = 'y' then begin
232239
if PyLong_Check(value) then
@@ -235,9 +242,9 @@ function PyPoint_setattrfunc(obj : PPyObject; key : PAnsiChar; value : PPyObjec
235242
Result := 0;
236243
end
237244
else
238-
PyErr_SetString (PyExc_AttributeError^, PAnsiChar(AnsiString(Format('Attribute "%s" needs an integer',[key]))));
245+
PyErr_SetString (PyExc_AttributeError^, PAnsiChar(Utf8Encode(Format('Attribute "%s" needs an integer',[key]))));
239246
end else
240-
PyErr_SetString (PyExc_AttributeError^, PAnsiChar(AnsiString(Format('Unknown attribute "%s"',[key]))));
247+
PyErr_SetString (PyExc_AttributeError^, PAnsiChar(Utf8Encode(Format('Unknown attribute "%s"',[key]))));
241248
end;
242249
end;
243250

Demos/Demo36/ParallelPython.dpr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ begin
109109
WriteLn('Elapsed ms: ' + SW.ElapsedMilliseconds.ToString);
110110
WriteLn;
111111
finally
112+
Sleep(1000); // allow some time for the threads to terminate
112113
DestroyEngine;
113114
end;
114115
except

Source/PythonAction.pas

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface
1717

1818
uses
1919
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
20-
ActnList, PythonEngine;
20+
Actions, ActnList, PythonEngine;
2121

2222
type
2323
TPythonAction = class(TAction)
@@ -114,21 +114,21 @@ function TPythonAction.HandlesTarget(Target: TObject): Boolean;
114114

115115
procedure TPythonAction.InitializeAction;
116116
var
117-
docString: string;
117+
docString: string;
118118
begin
119119
if not (csDesigning in ComponentState) and Assigned(PythonModule) then
120120
begin
121121
fClearname := 'Clear' + Name;
122122
docString := 'Claer all events of "' + Owner.Name + '.' + Name + '" action';
123-
PythonModule.AddDelphiMethod(PChar(fClearname), PythonClear, PChar(docString));
123+
PythonModule.AddDelphiMethod(PAnsiChar(fClearname), PythonClear, PAnsiChar(docString));
124124

125125
fRegistername := 'Register' + Name;
126126
docString := 'Register an event againt the "' + Owner.Name + '.' + Name + '" action';
127-
PythonModule.AddDelphiMethod(PChar(fRegistername), PythonRegister, PChar(docString));
127+
PythonModule.AddDelphiMethod(PAnsiChar(fRegistername), PythonRegister, PAnsiChar(docString));
128128

129129
fUnregistername := 'Unregister' + Name;
130130
docString := 'Unregister an event againt the "' + Owner.Name + '.' + Name + '" action';
131-
PythonModule.AddDelphiMethod(PChar(fUnregistername), PythonUnregister, PChar(docString));
131+
PythonModule.AddDelphiMethod(PAnsiChar(fUnregistername), PythonUnregister, PAnsiChar(docString));
132132
end;
133133
end;
134134

@@ -152,7 +152,7 @@ function TPythonAction.PythonRegister(pself, args: PPyObject): PPyObject;
152152
( not PyFunction_Check(func)) then
153153
begin
154154
s := fRegistername + '(function)';
155-
PyErr_SetString(PyExc_TypeError^,PChar(s));
155+
PyErr_SetString(PyExc_TypeError^, PAnsiChar(Utf8Encode(s)));
156156
end
157157
else
158158
with RegisteredMethods do
@@ -174,7 +174,7 @@ function TPythonAction.PythonUnregister(pself, args: PPyObject): PPyObject;
174174
(RegisteredMethods.IndexOf(func) = -1) then
175175
begin
176176
s := fUnregistername + '(function)';
177-
PyErr_SetString(PyExc_TypeError^,PChar(s));
177+
PyErr_SetString(PyExc_TypeError^, PAnsiChar(Utf8Encode(s)));
178178
end
179179
else
180180
with RegisteredMethods do

Source/PythonEngine.pas

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,10 +2118,8 @@ TPythonEngine = class(TPythonInterface)
21182118
function Run_CommandAsString(const command: AnsiString; mode: Integer; const FileName: string = '<string>'): string;
21192119
function Run_CommandAsObject(const command: AnsiString; mode: Integer; const FileName: string = '<string>'): PPyObject;
21202120
function Run_CommandAsObjectWithDict(const command: AnsiString; mode: Integer; locals, globals: PPyObject; const FileName: string = '<string>'): PPyObject;
2121-
function EncodeString (const str: UnicodeString): AnsiString; {$IFDEF FPC}overload;{$ENDIF}
2122-
{$IFDEF FPC}
2123-
function EncodeString (const str: AnsiString): AnsiString; overload;
2124-
{$ENDIF}
2121+
function EncodeString(const str: UnicodeString): AnsiString; overload;
2122+
function EncodeString(const str: AnsiString): AnsiString; overload;
21252123
function EncodeWindowsFilePath(const str: string): AnsiString;
21262124
procedure ExecString(const command: AnsiString; const FileName: string = '<string>'); overload;
21272125
procedure ExecStrings(strings: TStrings; const FileName: string = '<string>'); overload;
@@ -2498,8 +2496,8 @@ TError = class(TCollectionItem)
24982496
destructor Destroy; override;
24992497
procedure Assign(Source: TPersistent); override;
25002498
procedure BuildError( const ModuleName : AnsiString );
2501-
procedure RaiseError( const msg : AnsiString );
2502-
procedure RaiseErrorObj( const msg : AnsiString; obj : PPyObject );
2499+
procedure RaiseError(const msg : AnsiString);
2500+
procedure RaiseErrorObj(const msg : AnsiString; obj : PPyObject);
25032501
function Owner : TErrors;
25042502
property Error : PPyObject read FError write FError;
25052503
published
@@ -3098,9 +3096,8 @@ function CleanString(const s : UnicodeString; AppendLF : Boolean = True) : Unico
30983096
implementation
30993097

31003098
uses
3101-
{$IFDEF FPC}
31023099
StrUtils,
3103-
{$ELSE}
3100+
{$IFNDEF FPC}
31043101
AnsiStrings,
31053102
{$ENDIF}
31063103
{$IFDEF MSWINDOWS}
@@ -5627,17 +5624,15 @@ function TPythonEngine.FindClient( const aName : string ) : TEngineClient;
56275624
end;
56285625
end;
56295626

5630-
function TPythonEngine.EncodeString(const str: UnicodeString): AnsiString; {$IFDEF FPC}overload;{$ENDIF}
5627+
function TPythonEngine.EncodeString(const str: UnicodeString): AnsiString;
56315628
begin
5632-
Result := UTF8Encode(str)
5629+
Result := UTF8Encode(str);
56335630
end;
56345631

5635-
{$IFDEF FPC}
5636-
function TPythonEngine.EncodeString (const str: AnsiString): AnsiString; overload;
5632+
function TPythonEngine.EncodeString(const str: AnsiString): AnsiString;
56375633
begin
5638-
Result := str;
5634+
Result := UTF8Encode(str);
56395635
end;
5640-
{$ENDIF}
56415636

56425637
function TPythonEngine.EncodeWindowsFilePath(const str: string): AnsiString;
56435638
{PEP 529}
@@ -7276,14 +7271,14 @@ procedure TError.BuildError( const ModuleName : AnsiString );
72767271
raise Exception.CreateFmt( 'Could not create error "%s"', [Name] );
72777272
end;
72787273

7279-
procedure TError.RaiseError( const msg : AnsiString );
7274+
procedure TError.RaiseError(const msg : AnsiString);
72807275
begin
72817276
Owner.Owner.CheckEngine;
72827277
with Owner.Owner.Engine do
7283-
PyErr_SetString( Error, PAnsiChar(msg) );
7278+
PyErr_SetString(Error, PAnsiChar(EncodeString(msg)));
72847279
end;
72857280

7286-
procedure TError.RaiseErrorObj( const msg : AnsiString; obj : PPyObject );
7281+
procedure TError.RaiseErrorObj(const msg : AnsiString; obj : PPyObject);
72877282
var
72887283
args, res, str : PPyObject;
72897284
i : Integer;
@@ -7327,10 +7322,11 @@ procedure TError.RaiseErrorObj( const msg : AnsiString; obj : PPyObject );
73277322
end
73287323
else
73297324
raise Exception.Create('TError.RaiseErrorObj: I didn''t get an instance' );
7330-
PyErr_SetObject( Error, res );
7325+
PyErr_SetObject(Error, res);
7326+
Py_XDECREF(res);
73317327
end
73327328
else
7333-
PyErr_SetObject( Error, obj );
7329+
PyErr_SetObject(Error, obj);
73347330
end;
73357331

73367332
function TError.Owner : TErrors;
@@ -7790,8 +7786,8 @@ function TPyObject.SetAttr(key : PAnsiChar; value : PPyObject) : Integer;
77907786
with GetPythonEngine do
77917787
begin
77927788
Result := -1;
7793-
PyErr_SetString (PyExc_AttributeError^,
7794-
PAnsiChar(AnsiString(Format('Unknown attribute "%s"',[key]))));
7789+
PyErr_SetString(PyExc_AttributeError^,
7790+
PAnsiChar(EncodeString(Format('Unknown attribute "%s"',[key]))));
77957791
end;
77967792
end;
77977793

@@ -7828,7 +7824,7 @@ function TPyObject.GetBuffer(view: PPy_buffer; flags: Integer): Integer;
78287824
begin
78297825
view^.obj := nil;
78307826
with GetPythonEngine do
7831-
PyErr_SetObject(PyExc_BufferError^, PyUnicodeFromString(''));
7827+
PyErr_SetString(PyExc_BufferError^, '');
78327828
Result := -1;
78337829
end;
78347830

0 commit comments

Comments
 (0)