Skip to content

Commit be587a7

Browse files
committed
Trying the PEP484 convention to generate arg descriptions in the docstr compatible with Sphinx
1 parent a7f2567 commit be587a7

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

Source/WrapDelphi.pas

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,7 @@ TExposedMethodImplementation = class(TAbstractExposedMemberImplementation)
939939

940940
function VirtualMethodImplementation(): pointer;
941941

942+
class function MethodDocStr(const ARttiMethod: TRttiMethod): string;
942943
class function Methods_Wrapper(const ASelf, AArgs, AKeyWords: PPyObject): PPyObject; static; cdecl;
943944

944945
property MethodHandler: TExposedMethodHandler read FMethodHandler write FMethodHandler;
@@ -1138,6 +1139,65 @@ function TExposedMethodImplementation.GetDocString(): string;
11381139
Name, FRttiType.Name, NativeInt(Self)]);
11391140
end;
11401141

1142+
class function TExposedMethodImplementation.MethodDocStr
1143+
(const ARttiMethod: TRttiMethod): string;
1144+
const
1145+
METHOD_DOC_STR_PATTERN = '%s.%s(%s)';
1146+
var
1147+
LArgsStr: string;
1148+
LRttiParameter: TRttiParameter;
1149+
begin
1150+
if (Length(ARttiMethod.GetParameters) = 0) then
1151+
Exit(String.Empty);
1152+
1153+
LArgsStr := String.Empty;
1154+
for LRttiParameter in ARttiMethod.GetParameters do begin
1155+
if not LArgsStr.IsEmpty then
1156+
LArgsStr := LArgsStr + ', ';
1157+
1158+
if not Assigned(LRttiParameter.ParamType) then
1159+
LArgsStr := LArgsStr + LRttiParameter.Name
1160+
else
1161+
LArgsStr := LArgsStr + LRttiParameter.Name + '=' + LRttiParameter.ParamType.Name;
1162+
end;
1163+
1164+
Result := String.Format(METHOD_DOC_STR_PATTERN, [
1165+
ARttiMethod.Parent.Name, ARttiMethod.Name, LArgsStr]);
1166+
1167+
if Assigned(ARttiMethod.ReturnType) then
1168+
Result := Result + ': ' + ARttiMethod.ReturnType.Name;
1169+
1170+
//Args:
1171+
// param1: The first parameter.
1172+
// param2: The second parameter.
1173+
if Length(ARttiMethod.GetParameters()) > 0 then begin
1174+
Result := Result + #10 + #10 + 'Args:' + #10;
1175+
for LRttiParameter in ARttiMethod.GetParameters do begin
1176+
if Assigned(LRttiParameter.ParamType) then
1177+
Result := Result + String.Format(' %s (%s)', [LRttiParameter.Name, LRttiParameter.ParamType.Name])
1178+
else if TParamFlag.pfVar in LRttiParameter.Flags then
1179+
Result := Result + String.Format(' %s (%s)', [LRttiParameter.Name, 'var'])
1180+
else if TParamFlag.pfConst in LRttiParameter.Flags then
1181+
Result := Result + String.Format(' %s (%s)', [LRttiParameter.Name, 'const'])
1182+
else if TParamFlag.pfOut in LRttiParameter.Flags then
1183+
Result := Result + String.Format(' %s (%s)', [LRttiParameter.Name, 'out']);
1184+
1185+
Result := Result + #10;
1186+
end;
1187+
end;
1188+
1189+
if Assigned(ARttiMethod.ReturnType) then begin
1190+
//Returns:
1191+
// The return value. True for success, False otherwise.
1192+
1193+
Result := Result + #10 + 'Returns:' + #10;
1194+
Result := Result + String.Format(' Return type: %s', [
1195+
ARttiMethod.ReturnType.Name]) + #10;
1196+
end;
1197+
1198+
Result := Result + #10;
1199+
end;
1200+
11411201
function TExposedMethodImplementation.VirtualMethodImplementation(): pointer;
11421202
var
11431203
LRttiCtx: TRttiContext;
@@ -3400,6 +3460,11 @@ class procedure TPyDelphiObject.ExposeMethods(const AClass: TClass;
34003460
LClass := LClass.ClassParent;
34013461
end;
34023462

3463+
//Build the DocStr including method args
3464+
LExposedMethod.DocString :=
3465+
AnsiString(TExposedMethodImplementation.MethodDocStr(LRttiMethod))
3466+
+ LExposedMethod.DocString;
3467+
34033468
//Adds the Python method
34043469
if LRttiMethod.IsStatic then
34053470
APythonType.AddStaticMethodWithKeywords(

0 commit comments

Comments
 (0)