Skip to content

Commit 64112f2

Browse files
committed
Breaking change: Removed CreateWith overload with 3 arguments
TPyDelphiObject.CreateWith raises a python TypeError 'Cannot create instances of class %s' CreateWith overrides in general should call Create and create an instance and assign it to DelphiObject
1 parent ba6f513 commit 64112f2

19 files changed

+122
-223
lines changed

Demos/Demo08/Unit1.pas

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ TPyPoint = class(TPyObject)
3838

3939
// Constructors & Destructors
4040
constructor Create( APythonType : TPythonType ); override;
41-
constructor CreateWith(PythonType: TPythonType; args: PPyObject); override;
41+
constructor CreateWith(PythonType: TPythonType; args, kwds: PPyObject); override;
4242

4343
// Type services
4444
////////////////
@@ -88,9 +88,9 @@ constructor TPyPoint.Create( APythonType : TPythonType );
8888
// the Create constructor first, and because the constructors
8989
// are virtual, TPyPoint.Create will be automatically be called.
9090

91-
constructor TPyPoint.CreateWith(PythonType: TPythonType; args: PPyObject);
91+
constructor TPyPoint.CreateWith(PythonType: TPythonType; args, kwds: PPyObject);
9292
begin
93-
inherited;
93+
Create(PythonType);
9494
with GetPythonEngine do
9595
begin
9696
if PyArg_ParseTuple( args, 'ii:CreatePoint',@x, @y ) = 0 then

Demos/Demo21/Unit1.pas

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ TPyPoint = class(TPyObject)
4646

4747
// Constructors & Destructors
4848
constructor Create( APythonType : TPythonType ); override;
49-
constructor CreateWith(PythonType: TPythonType; args: PPyObject); override;
49+
constructor CreateWith(PythonType: TPythonType; args, kwds: PPyObject); override;
5050

5151
// Type services
5252
////////////////
@@ -81,9 +81,9 @@ constructor TPyPoint.Create( APythonType : TPythonType );
8181
// the Create constructor first, and because the constructors
8282
// are virtual, TPyPoint.Create will be automatically be called.
8383

84-
constructor TPyPoint.CreateWith(PythonType: TPythonType; args: PPyObject);
84+
constructor TPyPoint.CreateWith(PythonType: TPythonType; args, kwds: PPyObject);
8585
begin
86-
inherited;
86+
Create(PythonType);
8787
with GetPythonEngine do
8888
begin
8989
if PyArg_ParseTuple( args, 'ii:CreatePoint',@x, @y ) = 0 then

Demos/Demo26/Unit1.pas

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ TPyPoint = class(TPyObject)
3535

3636
// Constructors & Destructors
3737
constructor Create( APythonType : TPythonType ); override;
38-
constructor CreateWith(PythonType: TPythonType; args: PPyObject); override;
38+
constructor CreateWith(PythonType: TPythonType; args, kwds: PPyObject); override;
3939

4040
// Type services
4141
////////////////
@@ -84,9 +84,9 @@ constructor TPyPoint.Create( APythonType : TPythonType );
8484
// the Create constructor first, and because the constructors
8585
// are virtual, TPyPoint.Create will be automatically be called.
8686

87-
constructor TPyPoint.CreateWith(PythonType: TPythonType; args: PPyObject);
87+
constructor TPyPoint.CreateWith(PythonType: TPythonType; args, kwds: PPyObject);
8888
begin
89-
inherited;
89+
Create(PythonType);
9090
with GetPythonEngine do
9191
begin
9292
if PyArg_ParseTuple( args, 'ii:CreatePoint',@x, @y ) = 0 then

Demos/Demo28/Unit1.pas

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ TPyStringList = class(TPyObject)
1818
public
1919
// Constructors & Destructors
2020
constructor Create( APythonType : TPythonType ); override;
21-
constructor CreateWith(PythonType: TPythonType; args: PPyObject); override;
21+
constructor CreateWith(PythonType: TPythonType; args, kwds: PPyObject); override;
2222
destructor Destroy; override;
2323

2424
// Basic services
@@ -46,7 +46,7 @@ TPyStringListIterator = class(TPyObject)
4646
procedure SetStringList(const Value: TPyStringList);
4747
public
4848
constructor Create( APythonType : TPythonType ); override;
49-
constructor CreateWith(PythonType: TPythonType; args: PPyObject); override;
49+
constructor CreateWith(PythonType: TPythonType; args, kwds: PPyObject); override;
5050
destructor Destroy; override;
5151

5252
// Basic services
@@ -145,11 +145,11 @@ constructor TPyStringList.Create(APythonType: TPythonType);
145145
fStrings := TStringList.Create;
146146
end;
147147

148-
constructor TPyStringList.CreateWith(PythonType: TPythonType; args: PPyObject);
148+
constructor TPyStringList.CreateWith(PythonType: TPythonType; args, kwds: PPyObject);
149149
var
150150
i : Integer;
151151
begin
152-
inherited;
152+
Create(PythonType);
153153
with GetPythonEngine do
154154
begin
155155
for i := 0 to PyTuple_Size(args)-1 do
@@ -241,12 +241,12 @@ constructor TPyStringListIterator.Create(APythonType: TPythonType);
241241
inherited;
242242
end;
243243

244-
constructor TPyStringListIterator.CreateWith(PythonType: TPythonType; args: PPyObject);
244+
constructor TPyStringListIterator.CreateWith(PythonType: TPythonType; args, kwds: PPyObject);
245245
var
246246
_obj : PPyObject;
247247
_stringList : TPyStringList;
248248
begin
249-
inherited;
249+
Create(PythonType);
250250
with GetPythonEngine do
251251
begin
252252
if PyArg_ParseTuple( args, 'O:TPyStringListIterator constructor',@_obj ) <> 0 then

Demos/Demo32/Unit1.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ constructor TPyPoint.CreateWith(PythonType: TPythonType; args, kwds: PPyObject);
111111
KeyArray: array of AnsiString;
112112
KeyPointerArray: array of PAnsiChar;
113113
begin
114-
inherited;
114+
Create(PythonType);
115115
KeyArray := ['x', 'y'];
116116
KeyPointerArray := [PAnsiChar(KeyArray[0]), PAnsiChar(KeyArray[1]), nil];
117117
with GetPythonEngine, DelphiObject as TPoint do

Demos/Demo34/Unit1.pas

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ TPyPoint = class(TPyObject)
4343

4444
// Constructors & Destructors
4545
constructor Create( APythonType : TPythonType ); override;
46-
constructor CreateWith(PythonType: TPythonType; args: PPyObject); override;
46+
constructor CreateWith(PythonType: TPythonType; args, kwds: PPyObject); override;
4747

4848
// Type services
4949
////////////////
@@ -158,9 +158,9 @@ constructor TPyPoint.Create( APythonType : TPythonType );
158158
// the Create constructor first, and because the constructors
159159
// are virtual, TPyPoint.Create will automatically be called.
160160

161-
constructor TPyPoint.CreateWith(PythonType: TPythonType; args: PPyObject);
161+
constructor TPyPoint.CreateWith(PythonType: TPythonType; args, kwds: PPyObject);
162162
begin
163-
inherited;
163+
Create(PythonType);
164164
with GetPythonEngine do
165165
begin
166166
if PyArg_ParseTuple( args, 'ii:CreatePoint',@x, @y ) = 0 then

Source/PythonEngine.pas

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2393,8 +2393,7 @@ TPyObject = class
23932393

23942394
// Constructors & Destructors
23952395
constructor Create(APythonType: TPythonType); virtual;
2396-
constructor CreateWith(APythonType: TPythonType; args: PPyObject); overload; virtual;
2397-
constructor CreateWith(APythonType: TPythonType; args, kwds: PPyObject); overload; virtual;
2396+
constructor CreateWith(APythonType: TPythonType; args, kwds: PPyObject); virtual;
23982397
destructor Destroy; override;
23992398

24002399
class function NewInstance: TObject; override;
@@ -2697,7 +2696,7 @@ TPyVar = class(TPyObject)
26972696

26982697
// Constructors & Destructors
26992698
constructor Create( APythonType : TPythonType ); override;
2700-
constructor CreateWith(APythonType: TPythonType; args: PPyObject); override;
2699+
constructor CreateWith(APythonType: TPythonType; args, kwds: PPyObject); override;
27012700
destructor Destroy; override;
27022701

27032702
// Type services
@@ -7462,15 +7461,10 @@ constructor TPyObject.Create(APythonType: TPythonType);
74627461
end;
74637462
end;
74647463

7465-
constructor TPyObject.CreateWith(APythonType: TPythonType; args: PPyObject);
7466-
begin
7467-
Create(APythonType);
7468-
end;
7469-
74707464
constructor TPyObject.CreateWith(APythonType: TPythonType; args,
74717465
kwds: PPyObject);
74727466
begin
7473-
CreateWith(APythonType, args);
7467+
Create(APythonType);
74747468
end;
74757469

74767470
destructor TPyObject.Destroy;
@@ -8960,9 +8954,9 @@ constructor TPyVar.Create( APythonType : TPythonType );
89608954
// the Create constructor first, and because the constructors
89618955
// are virtual, TPyVar.Create will be automatically be called.
89628956

8963-
constructor TPyVar.CreateWith(APythonType: TPythonType; args: PPyObject);
8957+
constructor TPyVar.CreateWith(APythonType: TPythonType; args, kwds: PPyObject);
89648958
begin
8965-
inherited;
8959+
Create(APythonType);
89668960
with GetPythonEngine do
89678961
begin
89688962
if PyArg_ParseTuple( args, 'O:CreateVar',@dv_object ) = 0 then

Source/WrapDelphi.pas

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,11 @@ TPyDelphiObject = class (TPyInterfacedObject, IFreeNotificationSubscriber)
569569
Owned: Boolean;
570570

571571
constructor Create( APythonType : TPythonType ); override;
572+
// CreateWith raises a python TypeError 'Cannot create instances..'
573+
// Subclasses that can be instantiated need to overwrite this method and
574+
// a) Call the virtual constructor Create
575+
// b) Create the pascal object and assign it to DelphiObject
576+
constructor CreateWith(APythonType: TPythonType; args, kwds: PPyObject); override;
572577
destructor Destroy; override;
573578

574579
function GetAttrO( key: PPyObject) : PPyObject; override;
@@ -985,6 +990,7 @@ implementation
985990
MethodCallback;
986991

987992
resourcestring
993+
rs_CannotCreate = 'Cannot create instances of class %s';
988994
rs_ErrCheckIndex = '%s "%d" out of range';
989995
rs_ErrCheckInt = '%s receives only integer values';
990996
rs_ErrCheckFloat = '%s receives only float values';
@@ -2540,6 +2546,13 @@ constructor TPyDelphiObject.Create(APythonType: TPythonType);
25402546
PyDelphiWrapper := TPyDelphiWrapper(APythonType.Owner);
25412547
end;
25422548

2549+
constructor TPyDelphiObject.CreateWith(APythonType: TPythonType; args, kwds: PPyObject);
2550+
begin
2551+
with APythonType.Engine do
2552+
PyErr_SetObject(PyExc_TypeError^, PyUnicodeFromString(
2553+
Format(rs_CannotCreate, [APythonType.TypeName])));
2554+
end;
2555+
25432556
function TPyDelphiObject.CreateContainerAccess: TContainerAccess;
25442557
var
25452558
_ContainerAccessClass : TContainerAccessClass;
@@ -3899,6 +3912,8 @@ class procedure TPyDelphiObject.ExposeIndexedProperties(AClass: TClass;
38993912
LClass: TClass;
39003913
LDocStr: string;
39013914
begin
3915+
// TODO: Identify and handle the default property
3916+
39023917
LRttiCtx := TRttiContext.Create();
39033918
try
39043919
LRttiType := LRttiCtx.GetType(AClass) as TRttiStructuredType;

Source/WrapDelphiClasses.pas

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ TPyDelphiMemoryStream = class(TPyDelphiCustomMemoryStream)
330330
function GetDelphiObject: TMemoryStream;
331331
procedure SetDelphiObject(const Value: TMemoryStream);
332332
public
333+
constructor CreateWith(APythonType: TPythonType; args, kwds: PPyObject); override;
333334
// Class methods
334335
class function DelphiObjectClass : TClass; override;
335336
// Properties
@@ -1257,7 +1258,7 @@ constructor TPyDelphiComponent.CreateWith(APythonType: TPythonType; args, kwds:
12571258
_obj : PPyObject;
12581259
_owner : TObject;
12591260
begin
1260-
inherited;
1261+
Create(APythonType);
12611262
if APythonType.Engine.PyArg_ParseTuple( args, 'O:Create',@_obj ) <> 0 then
12621263
begin
12631264
_owner := nil;
@@ -1938,7 +1939,6 @@ class procedure TPyDelphiStream.SetupType(PythonType: TPythonType);
19381939

19391940
class procedure TPyDelphiStream.RegisterMethods(PythonType: TPythonType);
19401941
begin
1941-
inherited;
19421942
PythonType.AddMethod('ReadBytes', @TPyDelphiStream.ReadBytes_Wrapper,
19431943
'TPyDelphiStream.ReadBytes()' + #10 + 'Read content as bytes.');
19441944
PythonType.AddMethod('ReadInt', @TPyDelphiStream.ReadInt_Wrapper,
@@ -2132,7 +2132,7 @@ THandleStreamClass = class of THandleStream;
21322132
LParamCount: NativeInt;
21332133
LHandle: THandle;
21342134
begin
2135-
inherited;
2135+
Create(APythonType);
21362136
//Clear unsuccessful overloaded constructor error
21372137
APythonType.Engine.PyErr_Clear();
21382138

@@ -2295,6 +2295,15 @@ procedure TPyDelphiCustomMemoryStream.SetDelphiObject(
22952295

22962296
{ TPyDelphiMemoryStream }
22972297

2298+
constructor TPyDelphiMemoryStream.CreateWith(APythonType: TPythonType; args, kwds: PPyObject);
2299+
type
2300+
TMemoryStreamClass = class of TMemoryStream;
2301+
begin
2302+
Create(APythonType);
2303+
if APythonType.Engine.PyArg_ParseTuple(args, ':Create') <> 0 then
2304+
DelphiObject := TMemoryStreamClass(DelphiObjectClass).Create;
2305+
end;
2306+
22982307
class function TPyDelphiMemoryStream.DelphiObjectClass: TClass;
22992308
begin
23002309
Result := TMemoryStream;

Source/WrapDelphiTypes.pas

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ TPyDelphiPoint = class(TPyObject)
4040
function Set_X(AValue : PPyObject; AContext : Pointer) : Integer; cdecl;
4141
function Set_Y(AValue : PPyObject; AContext : Pointer) : Integer; cdecl;
4242
public
43-
constructor CreateWith(APythonType: TPythonType; args: PPyObject); override;
43+
constructor CreateWith(APythonType: TPythonType; args, kwds: PPyObject); override;
4444

4545
function Compare( obj: PPyObject) : Integer; override;
4646
function Repr : PPyObject; override;
@@ -72,7 +72,7 @@ TPyDelphiRect = class(TPyObject)
7272
public
7373
PyDelphiWrapper : TPyDelphiWrapper;
7474

75-
constructor CreateWith(APythonType: TPythonType; args: PPyObject); override;
75+
constructor CreateWith(APythonType: TPythonType; args, kwds: PPyObject); override;
7676

7777
function Compare( obj: PPyObject) : Integer; override;
7878
function Repr : PPyObject; override;
@@ -94,7 +94,7 @@ TPyDelphiSize = class(TPyObject)
9494
function Set_CX(AValue : PPyObject; AContext : Pointer) : Integer; cdecl;
9595
function Set_CY(AValue : PPyObject; AContext : Pointer) : Integer; cdecl;
9696
public
97-
constructor CreateWith(APythonType: TPythonType; args: PPyObject); override;
97+
constructor CreateWith(APythonType: TPythonType; args, kwds: PPyObject); override;
9898

9999
function Compare( obj: PPyObject) : Integer; override;
100100
function Repr : PPyObject; override;
@@ -290,12 +290,12 @@ function TPyDelphiPoint.Compare(obj: PPyObject): Integer;
290290
Result := 1;
291291
end;
292292

293-
constructor TPyDelphiPoint.CreateWith(APythonType: TPythonType; args:
294-
PPyObject);
293+
constructor TPyDelphiPoint.CreateWith(APythonType: TPythonType; args,
294+
kwds: PPyObject);
295295
var
296296
x, y : Integer;
297297
begin
298-
inherited;
298+
Create(APythonType);
299299
if APythonType.Engine.PyArg_ParseTuple( args, 'ii:Create',@x, @y ) <> 0 then
300300
begin
301301
fValue.X := x;
@@ -395,9 +395,10 @@ function TPyDelphiRect.Compare(obj: PPyObject): Integer;
395395
Result := 1;
396396
end;
397397

398-
constructor TPyDelphiRect.CreateWith(APythonType: TPythonType; args: PPyObject);
398+
constructor TPyDelphiRect.CreateWith(APythonType: TPythonType; args,
399+
kwds: PPyObject);
399400
begin
400-
inherited;
401+
Create(APythonType);
401402
APythonType.Engine.PyArg_ParseTuple( args, 'iiii:Create',@fValue.Left, @fValue.Top, @fValue.Right, @fValue.Bottom );
402403
end;
403404

@@ -581,11 +582,11 @@ function TPyDelphiSize.Compare(obj: PPyObject): Integer;
581582
Result := 1;
582583
end;
583584

584-
constructor TPyDelphiSize.CreateWith(APythonType: TPythonType; args: PPyObject);
585+
constructor TPyDelphiSize.CreateWith(APythonType: TPythonType; args, kwds: PPyObject);
585586
var
586587
cx, cy : Integer;
587588
begin
588-
inherited;
589+
Create(APythonType);
589590
if APythonType.Engine.PyArg_ParseTuple( args, 'ii:Create',@cx, @cy ) <> 0 then
590591
begin
591592
fValue.cx := cx;

0 commit comments

Comments
 (0)