Skip to content

Commit c113a4f

Browse files
committed
Check for DelphiObject = nil immediately in TPyDelphiObject.SetAttrO and TPyDelphiObject.GetAttrO
1 parent 0599a88 commit c113a4f

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed

Source/WrapDelphi.pas

+38-24
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,8 @@ implementation
10261026
rs_ErrAttrGet = 'Error in getting property "%s".'#$A'Error: %s';
10271027
rs_UnknownAttribute = 'Unknown attribute';
10281028
rs_ErrIterSupport = 'Wrapper %s does not support iterators';
1029-
rs_ErrAttrSetr = 'Error in setting property %s'#$A'Error: %s';
1029+
rs_ErrAttrSet = 'Error in setting property %s'#$A'Error: %s';
1030+
rs_ErrObjectDestroyed = 'Trying to access a destroyed pascal object';
10301031
rs_IncompatibleClasses = 'Incompatible classes';
10311032
rs_IncompatibleRecords = 'Incompatible record types';
10321033
rs_IncompatibleInterfaces = 'Incompatible interfaces';
@@ -1550,7 +1551,7 @@ function TExposedGetSet.SetterWrapper(AObj, AValue: PPyObject; AContext: Pointer
15501551
if Result <> 0 then
15511552
with GetPythonEngine do
15521553
PyErr_SetObject (PyExc_AttributeError^,
1553-
PyUnicodeFromString(Format(rs_ErrAttrSetr, [FRttiMember.Name, ErrMsg])));
1554+
PyUnicodeFromString(Format(rs_ErrAttrSet, [FRttiMember.Name, ErrMsg])));
15541555
end;
15551556

15561557
{ TExposedField }
@@ -1646,7 +1647,7 @@ function TExposedEvent.SetterWrapper(AObj, AValue: PPyObject; AContext: Pointer)
16461647
if Result <> 0 then
16471648
with GetPythonEngine do
16481649
PyErr_SetObject (PyExc_AttributeError^,
1649-
PyUnicodeFromString(Format(rs_ErrAttrSetr, [FRttiMember.Name, ErrMsg])));
1650+
PyUnicodeFromString(Format(rs_ErrAttrSet, [FRttiMember.Name, ErrMsg])));
16501651
end;
16511652

16521653
{ TExposedIndexedProperty }
@@ -3434,7 +3435,7 @@ function TPyRttiObject.SetAttrO(key, value: PPyObject): Integer;
34343435
if Result <> 0 then
34353436
with GetPythonEngine do
34363437
PyErr_SetObject(PyExc_AttributeError^, PyUnicodeFromString(
3437-
Format(rs_ErrAttrSetr, [KeyName, ErrMsg])));
3438+
Format(rs_ErrAttrSet, [KeyName, ErrMsg])));
34383439
end;
34393440

34403441
function TPyRttiObject.SetProps(args, keywords: PPyObject): PPyObject;
@@ -3543,6 +3544,7 @@ function TPyDelphiObject.GetAttrO(key: PPyObject): PPyObject;
35433544
var
35443545
KeyName: string;
35453546
ErrMsg : string;
3547+
PyEngine: TPythonEngine;
35463548
{$IFNDEF EXTENDED_RTTI}
35473549
{$IFNDEF FPC}
35483550
Info: PMethodInfoHeader;
@@ -3554,16 +3556,24 @@ function TPyDelphiObject.GetAttrO(key: PPyObject): PPyObject;
35543556
RttiType: TRttiStructuredType;
35553557
{$ENDIF}
35563558
begin
3557-
Result := inherited GetAttrO(key);
3558-
if GetPythonEngine.PyErr_Occurred = nil then Exit; // We found what we wanted
3559+
Result := nil;
3560+
PyEngine := GetPythonEngine;
35593561

3560-
// should not happen
3561-
if not (Assigned(DelphiObject) and
3562-
CheckStrAttribute(Key, 'GetAttrO key parameter', KeyName))
3563-
then
3562+
// If DelphiObject is nil Exit immediately with an error
3563+
if not Assigned(DelphiObject) then
3564+
begin
3565+
PyEngine.PyErr_SetObject(PyEngine.PyExc_AttributeError^,
3566+
PyEngine.PyUnicodeFromString(rs_ErrObjectDestroyed));
35643567
Exit;
3568+
end;
3569+
3570+
if not CheckStrAttribute(Key, 'GetAttrO key parameter', KeyName) then
3571+
Exit; // should not happen
3572+
3573+
Result := inherited GetAttrO(key);
3574+
if PyEngine.PyErr_Occurred = nil then Exit; // We found what we wanted
35653575

3566-
GetPythonEngine.PyErr_Clear;
3576+
PyEngine.PyErr_Clear;
35673577
{$IFDEF EXTENDED_RTTI}
35683578
// Use RTTI
35693579
if Assigned(DelphiObject) then begin
@@ -3620,17 +3630,17 @@ function TPyDelphiObject.GetAttrO(key: PPyObject): PPyObject;
36203630
{$ELSE FPC}
36213631
if GetTypeData(PropInfo^.PropType^)^.BaseType^ = TypeInfo(Boolean) then
36223632
{$ENDIF FPC}
3623-
Result := GetPythonEngine.VariantAsPyObject(Boolean(GetOrdProp(Self.DelphiObject, PropInfo)))
3633+
Result := PyEngine.VariantAsPyObject(Boolean(GetOrdProp(Self.DelphiObject, PropInfo)))
36243634
else
36253635
{$IFDEF FPC}
3626-
Result := GetPythonEngine.PyUnicodeFromString(GetEnumName(PropInfo^.PropType,
3636+
Result := PyEngine.PyUnicodeFromString(GetEnumName(PropInfo^.PropType,
36273637
{$ELSE FPC}
3628-
Result := GetPythonEngine.PyUnicodeFromString(GetEnumName(PropInfo^.PropType^,
3638+
Result := PyEngine.PyUnicodeFromString(GetEnumName(PropInfo^.PropType^,
36293639
{$ENDIF FPC}
36303640
GetOrdProp(Self.DelphiObject, PropInfo)));
36313641
end
36323642
end else
3633-
Result := GetPythonEngine.VariantAsPyObject(GetPropValue(DelphiObject, PropInfo));
3643+
Result := PyEngine.VariantAsPyObject(GetPropValue(DelphiObject, PropInfo));
36343644
end;
36353645
except
36363646
on E: Exception do begin
@@ -3640,9 +3650,8 @@ function TPyDelphiObject.GetAttrO(key: PPyObject): PPyObject;
36403650
end;
36413651
{$ENDIF}
36423652
if not Assigned(Result) then
3643-
with GetPythonEngine do
3644-
PyErr_SetObject (PyExc_AttributeError^,
3645-
PyUnicodeFromString(Format(rs_ErrAttrGet,[KeyName, ErrMsg])));
3653+
PyEngine.PyErr_SetObject (PyEngine.PyExc_AttributeError^,
3654+
PyEngine.PyUnicodeFromString(Format(rs_ErrAttrGet,[KeyName, ErrMsg])));
36463655
end;
36473656

36483657
function TPyDelphiObject.GetContainerAccess: TContainerAccess;
@@ -3944,11 +3953,16 @@ function TPyDelphiObject.SetAttrO(key, value: PPyObject): Integer;
39443953
Result := -1;
39453954
PyEngine := GetPythonEngine;
39463955

3947-
// should not happen
3948-
if not (Assigned(DelphiObject) and
3949-
CheckStrAttribute(Key, 'SetAttrO key parameter', KeyName))
3950-
then
3956+
// If DelphiObject is nil Exit immediately with an error
3957+
if not Assigned(DelphiObject) then
3958+
begin
3959+
PyEngine.PyErr_SetObject(PyEngine.PyExc_AttributeError^,
3960+
PyEngine.PyUnicodeFromString(rs_ErrObjectDestroyed));
39513961
Exit;
3962+
end;
3963+
3964+
if not CheckStrAttribute(Key, 'SetAttrO key parameter', KeyName) then
3965+
Exit; // should not happen
39523966

39533967
// Only call the inherited method at this stage if the attribute exists
39543968
PyObj := PyEngine.PyObject_GenericGetAttr(GetSelf, key);
@@ -3989,8 +4003,8 @@ function TPyDelphiObject.SetAttrO(key, value: PPyObject): Integer;
39894003
Result := inherited SetAttrO(key, value);
39904004
if Result <> 0 then
39914005
with PyEngine do
3992-
PyErr_SetObject(PyEngine.PyExc_AttributeError^, PyUnicodeFromString(
3993-
Format(rs_ErrAttrSetr, [KeyName, ErrMsg])));
4006+
PyErr_SetObject(PyExc_AttributeError^, PyUnicodeFromString(
4007+
Format(rs_ErrAttrSet, [KeyName, ErrMsg])));
39944008
end;
39954009

39964010
procedure TPyDelphiObject.SetDelphiObject(const Value: TObject);

0 commit comments

Comments
 (0)