|
1 |
| -(**************************************************************************) |
2 |
| -(* *) |
3 |
| -(* Module: Unit 'PythonEngine' Copyright (c) 1997 *) |
4 |
| -(* *) |
5 |
| -(* Dr. Dietmar Budelsky *) |
6 |
| -(* dbudelsky@web.de *) |
7 |
| -(* Germany *) |
8 |
| -(* *) |
9 |
| -(* Morgan Martinet *) |
10 |
| -(* 4723 rue Brebeuf *) |
11 |
| -(* H2J 3L2 MONTREAL (QC) *) |
12 |
| -(* CANADA *) |
13 |
| -(* e-mail: p4d@mmm-experts.com *) |
| 1 | +(**************************************************************************) |
| 2 | +(* This unit is part of the Python for Delphi (P4D) library *) |
| 3 | +(* Project home: https://github.com/pyscripter/python4delphi *) |
14 | 4 | (* *)
|
15 |
| -(* PyScripter *) |
16 |
| -(* e-mail: pyscripter@gmail.com *) |
| 5 | +(* Project Maintainer: PyScripter (pyscripter@gmail.com) *) |
| 6 | +(* Original Authors: Dr. Dietmar Budelsky (dbudelsky@web.de) *) |
| 7 | +(* Morgan Martinet (https://github.com/mmm-experts) *) |
| 8 | +(* Core developer: Lucas Belo (lucas.belo@live.com) *) |
| 9 | +(* Contributors: See contributors.md at project home *) |
17 | 10 | (* *)
|
18 |
| -(* Project page: https://github.com/pyscripter/python4delphi *) |
19 |
| -(**************************************************************************) |
20 |
| -(* Functionality: Delphi Components that provide an interface to the *) |
21 |
| -(* Python language (see python.txt for more infos on *) |
22 |
| -(* Python itself). *) |
23 |
| -(* *) |
24 |
| -(**************************************************************************) |
25 |
| -(* Contributors: *) |
26 |
| -(* Grzegorz Makarewicz (mak@mikroplan.com.pl) *) |
27 |
| -(* Andrew Robinson (andy@hps1.demon.co.uk) *) |
28 |
| -(* Mark Watts(mark_watts@hotmail.com) *) |
29 |
| -(* Olivier Deckmyn (olivier.deckmyn@mail.dotcom.fr) *) |
30 |
| -(* Sigve Tjora (public@tjora.no) *) |
31 |
| -(* Mark Derricutt (mark@talios.com) *) |
32 |
| -(* Igor E. Poteryaev (jah@mail.ru) *) |
33 |
| -(* Yuri Filimonov (fil65@mail.ru) *) |
34 |
| -(* Stefan Hoffmeister (Stefan.Hoffmeister@Econos.de) *) |
35 |
| -(* Michiel du Toit (micdutoit@hsbfn.com) - Lazarus Port *) |
36 |
| -(* Chris Nicolai (nicolaitanes@gmail.com) *) |
37 |
| -(* Andrey Gruzdev (andrey.gruzdev@gmail.com) *) |
38 |
| -(**************************************************************************) |
39 |
| -(* This source code is distributed with no WARRANTY, for no reason or use.*) |
40 |
| -(* Everyone is allowed to use and change this code free for his own tasks *) |
41 |
| -(* and projects, as long as this header and its copyright text is intact. *) |
42 |
| -(* For changed versions of this code, which are public distributed the *) |
43 |
| -(* following additional conditions have to be fullfilled: *) |
44 |
| -(* 1) The header has to contain a comment on the change and the author of *) |
45 |
| -(* it. *) |
46 |
| -(* 2) A copy of the changed source has to be sent to the above E-Mail *) |
47 |
| -(* address or my then valid address, if this is possible to the *) |
48 |
| -(* author. *) |
49 |
| -(* The second condition has the target to maintain an up to date central *) |
50 |
| -(* version of the component. If this condition is not acceptable for *) |
51 |
| -(* confidential or legal reasons, everyone is free to derive a component *) |
52 |
| -(* or to generate a diff file to my or other original sources. *) |
53 |
| -(* Dr. Dietmar Budelsky, 1997-11-17 *) |
| 11 | +(* LICENCE and Copyright: MIT (see project home) *) |
54 | 12 | (**************************************************************************)
|
55 | 13 |
|
56 | 14 | {$I Definition.Inc}
|
@@ -1958,8 +1916,9 @@ TPythonEngine = class(TPythonInterface)
|
1958 | 1916 | function ArrayToPyDict( const items : array of const) : PPyObject;
|
1959 | 1917 | function StringsToPyList( strings : TStrings ) : PPyObject;
|
1960 | 1918 | function StringsToPyTuple( strings : TStrings ) : PPyObject;
|
1961 |
| - procedure PyListToStrings(list: PPyObject; Strings: TStrings; ClearStrings: Boolean = True); |
| 1919 | + procedure PyListToStrings(list: PPyObject; Strings: TStrings; ClearStrings: Boolean = True); |
1962 | 1920 | procedure PyTupleToStrings( tuple: PPyObject; strings : TStrings );
|
| 1921 | + function GetSequenceItem( sequence : PPyObject; idx : Integer ) : Variant; |
1963 | 1922 | function ReturnNone : PPyObject;
|
1964 | 1923 | function ReturnTrue : PPyObject;
|
1965 | 1924 | function ReturnFalse : PPyObject;
|
@@ -4691,7 +4650,7 @@ procedure TPythonEngine.SetProgramArgs;
|
4691 | 4650 | for I := 0 to argc do begin
|
4692 | 4651 | {
|
4693 | 4652 | ... the first entry should refer to the script file to be executed rather
|
4694 |
| - than the executable hosting the Python interpreter. If there isnt a |
| 4653 | + than the executable hosting the Python interpreter. If there isn’t a |
4695 | 4654 | script that will be run, the first entry in argv can be an empty string.
|
4696 | 4655 | }
|
4697 | 4656 | if I = 0 then
|
@@ -4795,6 +4754,19 @@ function TPythonEngine.GetPythonPath: UnicodeString;
|
4795 | 4754 | {$ENDIF}
|
4796 | 4755 | end;
|
4797 | 4756 |
|
| 4757 | +function TPythonEngine.GetSequenceItem(sequence: PPyObject; |
| 4758 | + idx: Integer): Variant; |
| 4759 | + var |
| 4760 | + val : PPyObject; |
| 4761 | + begin |
| 4762 | + val := PySequence_GetItem( sequence, idx ); |
| 4763 | + try |
| 4764 | + Result := PyObjectAsVariant( val ); |
| 4765 | + finally |
| 4766 | + Py_XDecRef( val ); |
| 4767 | + end; |
| 4768 | +end; |
| 4769 | + |
4798 | 4770 | function TPythonEngine.GetProgramName: UnicodeString;
|
4799 | 4771 | begin
|
4800 | 4772 | {$IFDEF POSIX}
|
@@ -5752,18 +5724,6 @@ function TPythonEngine.PyObjectAsVariant( obj : PPyObject ) : Variant;
|
5752 | 5724 | end;
|
5753 | 5725 | end;
|
5754 | 5726 |
|
5755 |
| - function GetSequenceItem( sequence : PPyObject; idx : Integer ) : Variant; |
5756 |
| - var |
5757 |
| - val : PPyObject; |
5758 |
| - begin |
5759 |
| - val := PySequence_GetItem( sequence, idx ); |
5760 |
| - try |
5761 |
| - Result := PyObjectAsVariant( val ); |
5762 |
| - finally |
5763 |
| - Py_XDecRef( val ); |
5764 |
| - end; |
5765 |
| - end; |
5766 |
| - |
5767 | 5727 | var
|
5768 | 5728 | i, seq_length : Integer;
|
5769 | 5729 | begin
|
@@ -6091,7 +6051,8 @@ function TPythonEngine.PyUnicodeAsString(obj : PPyObject): UnicodeString;
|
6091 | 6051 | Exit;
|
6092 | 6052 |
|
6093 | 6053 | // The second argument is the size of the destination (Result) including #0
|
6094 |
| - NewSize := Utf8ToUnicode(PChar(Result), Cardinal(Size + 1), Buffer, Cardinal(Size)); |
| 6054 | + NewSize := Utf8ToUnicode(PWideChar(Result), Cardinal(Size + 1), Buffer, Cardinal(Size)); |
| 6055 | + |
6095 | 6056 | // NewSize includes #0
|
6096 | 6057 | SetLength(Result, NewSize - 1);
|
6097 | 6058 | end
|
@@ -6395,6 +6356,10 @@ constructor TEngineClient.Create( AOwner : TComponent );
|
6395 | 6356 |
|
6396 | 6357 | destructor TEngineClient.Destroy;
|
6397 | 6358 | begin
|
| 6359 | + // if the client is destroyed before the Python Engine then |
| 6360 | + // we need to finalize it. Otherwise it will already be finalized |
| 6361 | + if FInitialized then |
| 6362 | + Finalize; |
6398 | 6363 | Engine := nil; // This detaches the client from the Engine.
|
6399 | 6364 | if Assigned( FOnDestroy ) then
|
6400 | 6365 | FOnDestroy( Self );
|
@@ -8684,7 +8649,8 @@ procedure TPythonType.Initialize;
|
8684 | 8649 |
|
8685 | 8650 | procedure TPythonType.Finalize;
|
8686 | 8651 | begin
|
8687 |
| - Engine.Py_CLEAR(FCreateFunc); |
| 8652 | + if Assigned(Engine) then |
| 8653 | + Engine.Py_CLEAR(FCreateFunc); |
8688 | 8654 | FCreateFunc := nil;
|
8689 | 8655 | inherited;
|
8690 | 8656 | end;
|
|
0 commit comments