-
Content Count
913 -
Joined
-
Last visited
-
Days Won
55
Everything posted by pyscripter
-
It is almost a show stopper for laptop screens. I wonder why it has not attracted more votes and the attention of Embarcadero.
-
Not sure. It looks worse in a high DPI monitor.
-
The latter.
-
Wrapping a delphi object containing an attribute with an enum return value defined in Python
pyscripter replied to iqrf's topic in Python4Delphi
You don't need to modify WrapDelphi. Just use the latest version. function TValueToPyObject(const Value: TValue; DelphiWrapper: TPyDelphiWrapper; out ErrMsg: string): PPyObject; begin if Value.IsEmpty then Result := GetPythonEngine.ReturnNone else case Value.Kind of tkClass: Result := DelphiWrapper.Wrap(Value.AsObject); tkClassRef: Result := DelphiWrapper.WrapClass(Value.AsClass); tkInterface: Result := DelphiWrapper.WrapInterface(Value); tkRecord{$IFDEF MANAGED_RECORD},tkMRecord{$ENDIF}: Result := DelphiWrapper.WrapRecord(Value); tkArray, tkDynArray: Result := DynArrayToPython(Value, DelphiWrapper, ErrMsg); tkPointer: if Value.IsType<PPyObject> then Result := Value.AsType<PPyObject> else begin Result := nil; ErrMsg := rs_ErrValueToPython; end; else Result := SimpleValueToPython(Value, ErrMsg); end; end; -
LockBox 3 via GetIt broken since April 2024 Update
pyscripter replied to Sherlock's topic in General Help
Indeed, but Github repo owners can safeguard against that by adding a .gitattributes file. -
LockBox 3 via GetIt broken since April 2024 Update
pyscripter replied to Sherlock's topic in General Help
This is a common problem with Delphi github repos: https://github.com/JAM-Software/Virtual-TreeView/issues/1151#issuecomment-1332032004 Virtual-TreeView/.gitattributes at master · JAM-Software/Virtual-TreeView (github.com) -
In an older post I showed how to patch a virtual/non virtual, public/private method. A recently came across an Vcl bug that requires patching a non-virtual constructor. Does anyone know how to do that?
-
How to patch a constructor?
pyscripter replied to pyscripter's topic in RTL and Delphi Object Pascal
@Stefan Glienke Thanks. Would you happen know whether you can patch message methods? The only thing I could find is an old article Hallvard's Blog: Hack#15: Overriding message and dynamic methods at run-time (hallvards.blogspot.com). -
There have been a lot of questions in this forum about running python code in threads using Python4Delphi. I have created a comprehensive guide in this Wiki topic.
-
PyBuffer name of variables to read back to delphi?!
pyscripter replied to o815's topic in Python4Delphi
Your taps Variant has not assigned a value. You are missing: taps := MainModule.taps; -
convert native object instance to wrapped instance
pyscripter replied to AlexanderMi's topic in Python4Delphi
See TPyDelphiWrapper.Wrap(AObj: TObject; AOwnership: TObjectOwnership): PPyObject; Also you do need need your TPyEventFromScript. This is the old and hard way. You can use instead (e.g. in your FormCreate): PyDelphiWrapper1.RegisterDelphiWrapper(TPyClassWrapper<TEventFromScript>).Initialize; Since you are using interfaces there is also: function TPyDelphiWrapper.WrapInterface(const IValue: TValue): PPyObject; -
@maomao2028 Works fine here without memory leaks. See attached project. Note the use of DelayWrites := True in the PythonGUIInputOutput and the calls to TPythonThread.Py_End_Allow_Threads and TPythonThread.Py_Begin_Allow_Threads. Demo01.zip
-
Forgot to say that unfortunately you cannot just set the __doc__ property of Wrapped classes and methods. It has to be done at the time of wrapping. A trivial implementation of IDocServer would be: uses TypInfo; type TMyDocServer = class(TInterfacedObject, IDocServer) private function ReadTypeDocStr(ATypeInfo: PTypeInfo; out ADocStr: string): Boolean; function ReadMemberDocStr(AMember: TRttiMember; out ADocStr: string): Boolean; procedure Initialize; procedure Finalize; function Initialized: Boolean; end;{ TMyDocServer } procedure TMyDocServer.Finalize; begin end; procedure TMyDocServer.Initialize; begin end; function TMyDocServer.Initialized: Boolean; begin Result := True; end; function TMyDocServer.ReadMemberDocStr(AMember: TRttiMember; out ADocStr: string): Boolean; begin Result := False; if AMember.Name = 'getVersion' then begin Result := True; ADocStr := 'getVersion'; end; end; function TMyDocServer.ReadTypeDocStr(ATypeInfo: PTypeInfo; out ADocStr: string): Boolean; begin Result := False; if ATypeInfo = TypeInfo(ThostAPI ) then begin Result := True; ADocStr := 'ThostAPI doc'; end; end; and then PyDocServer := TMyDocServer.Create; DelphiWrapper.RegisterDelphiWrapper(TPyClassWrapper<THostAPI>).Initialize;
-
WrapDelphi defines the following: IDocServer = interface ['{4AF0D319-47E9-4F0A-9C71-97B8CBB559FF}'] function ReadTypeDocStr(ATypeInfo: PTypeInfo; out ADocStr: string): Boolean; function ReadMemberDocStr(AMember: TRttiMember; out ADocStr: string): Boolean; procedure Initialize; procedure Finalize; function Initialized: Boolean; end; var PyDocServer: IDocServer = nil; You need to implement the interface and assign the implemented interface to PyDocServer. PythonDocs.pas provides an implementation based on xml files, which is used by delphivcl and delphifmx. But if, what you are after is to provide docstrings to a few methods, it would be easier to create your own implementation.
-
CompareString function for UTF8 strings or buffers?
pyscripter replied to MarkShark's topic in RTL and Delphi Object Pascal
System.AnsiStrings.AnsiCompareStr under POSIX converts the ansistrings to UnicodeStrings and then compares. FPC has a function UTF8CompareStr, which sounds promising, but it also converts the ansi strings to UTF-16 and then compares them. Unfortunately it appears that there is no good way to directly compare utf8 strings without converting them. I hope I am wrong. -
CompareString function for UTF8 strings or buffers?
pyscripter replied to MarkShark's topic in RTL and Delphi Object Pascal
Unfortunately (see CompareStringA function (winnls.h) - Win32 apps | Microsoft Learn😞 It also appears that System.AnsiStrings.AnsiCompareStr ignores the code page of the ansi strings. -
[help] how to convert delphi TBytes to python Bytes
pyscripter replied to maomao2028's topic in Python4Delphi
Look at PyBytes_AsString, or PyBytes_AsStringAndSize functions. For example if obj is a bytes PPyObject. the following converts it to an AnsiString. AnsiString(PyBytes_AsString(obj)) Also PyObjectAsVariant(obj) converts it to a variant. In the opposite direction to create a bytes object in Delphi use PyBytes_FromStringAndSize -
Returning numpy array constructed in Delphi back to python call
pyscripter replied to hsauro's topic in Python4Delphi
Have you updated to the latest sources? -
This may be worth a look: neslib/Chet: C Header Translator for Delphi (github.com). From @Erik@Grijjy
-
Returning numpy array constructed in Delphi back to python call
pyscripter replied to hsauro's topic in Python4Delphi
WrapDelphi was designed to expose Delphi classes, records and interfaces to python. So it did not handle PPyObject parameters or return values. You could handle that by using the low-level approach (adding an Event to TPythonModule). However, I have now added to WrapDelphi support for exposing functions with parameters and/or results of type PPyObject. See WrapDelphiTest.pas in the latest version of the pyscripter/python4delphi: Free components that wrap up Python into Delphi and Lazarus (FPC) (github.com) repo. Example: Exposed Method: function TTestRttiAccess.PlaceInNewList(PyObj: PPyObject): PPyObject; begin with GetPythonEngine do begin Result := PyList_New(1); Py_XIncRef(PyObj); PyList_SetItem(Result, 0, PyObj); end; end; Usage in python from delphi import rtti_var list = rtti_var.PlaceInNewList('abc') The corresponding test in WrapDelphiTest: procedure TTestWrapDelphi.TestPPyObjects; var List: Variant; begin List := rtti_var.PlaceInNewList('abc'); Assert.IsTrue(VarIsPythonList(List)); Assert.AreEqual<string>(List.GetItem(0), 'abc'); end; -
Does anybody know whether the workaround mentioned by @Uwe Raabe in Parnassus Bookmarks issue - again. - Delphi IDE and APIs - Delphi-PRAXiS [en] (delphipraxis.net) is still needed in Delphi 12.1?
-
Under Tools, Options, Language, Delphi, Library, I see a new platform in the drop-down list "Selected platform" called "Windows 64 bits (Modern)". This is in addition to "Windows 64 bits". There was no mention of it in today's seminar. Anyone knows what it is and how to use it?
-
PyEnum_Check and VarIsEnum have to do with enumerators and not the enum.Enum class. There are many ways to get the value. Using VarPyth: var a := VarPythonCreate(pColor); var val := a.value
-
Note that if you get the PyObject corresponding to the function by using for instance var PyMainModule: PPyObject; PyFunc: PPyObject; PyMainModule := GetPythonEngine.GetMainModule; PyFunc := GetPythonEngine.PyObject_GetAttrString (PyMainModule, PAnsiChar(AnsiString(method_name))); // When you finish with PyFunc you need to decrease the refcount You can then call the function in a number of different ways including the relatively high level: function EvalFunction(pyfunc:PPyObject; const args: array of const): Variant;
-
How about reading the documentation of PyObject_CallMethod or just googling for example usage?