Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrongly throwing an exception from the wrapping object #464

Closed
Vobsoft opened this issue Mar 19, 2024 · 2 comments
Closed

Wrongly throwing an exception from the wrapping object #464

Vobsoft opened this issue Mar 19, 2024 · 2 comments

Comments

@Vobsoft
Copy link

Vobsoft commented Mar 19, 2024

Hello,
I have an example like this:

TColumn = class(TPersistent)
private
FHeaderName: String;
  procedure SetHeaderName(const Value: String);
  function GetHeaderName: String;
public
  constructor Create(Index: Integer);
  procedure Auto_size;
  property header_name: String read GetHeaderName write SetHeaderName;
end;

TColumnWrapper = class(TPyClassWrapper<TColumn>)
  constructor CreateWith(APythonType: TPythonType; args, kwds: PPyObject); override;
  function repr: PPyObject; override;
end;

If I throw an exception in the auto_size function, using the
procedure TColumn.Auto_Size; begin raise Exception.Create('Test error'); end
An error is raised and the script terminates correctly.
TypeError: "auto_size" called with invalid arguments. Error: Test error
If the error is raised in SetHeaderName, it will not be displayed, the script will terminate incorrectly and a memory leak will occur.
procedure TColumn.SetHeaderName(const Value: String); begin raise Exception.Create('Test error'); end

This modification solves the problem.

function SetRttiProperty(const ParentAddr: Pointer; Prop: TRttiProperty;

function SetRttiProperty(const ParentAddr: Pointer;  Prop: TRttiProperty;
  Value: PPyObject;  PyDelphiWrapper: TPyDelphiWrapper;
  out ErrMsg: string): Boolean;
var
  AttrValue: TValue;
begin
  Result := False;

try

    if Ord(Prop.Visibility) < Ord(mvPublic) then
      ErrMsg := rs_NoAccess
    else if not Prop.IsWritable then
      ErrMsg := rs_NotWritable
    else if Prop.PropertyType = nil then
      ErrMsg := rs_ErrNoTypeInfo
    else if Prop.PropertyType.TypeKind = tkMethod then
    begin
      if (Prop is TRttiInstanceProperty) and  (Prop.Visibility = mvPublished) then
        Result := PyDelphiWrapper.EventHandlers.Link(TObject(ParentAddr),
          (Prop as TRttiInstanceProperty).PropInfo, Value, ErrMsg)
      else
        ErrMsg := rs_NotPublished;
    end
    else if PyObjectToTValue(Value, Prop.PropertyType, AttrValue, ErrMsg) then
    begin
      Prop.SetValue(ParentAddr, AttrValue);
      Result := True;
    end;
except
    on E: Exception do begin
      ErrMsg := E.Message;
    end;
  end;

end;

AttributeError: Error in setting property header_name Error: Test error

@pyscripter
Copy link
Owner

The fix is a bit different that yours. Could you please confirm it now works as expected?

@Vobsoft
Copy link
Author

Vobsoft commented Mar 20, 2024

The repair is OK, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants