Jump to content

pyscripter

Members
  • Content Count

    761
  • Joined

  • Last visited

  • Days Won

    40

Everything posted by pyscripter

  1. pyscripter

    Strange memory leak with checkbox

    Just to clarify. a) No memory leakage happens if your timer routine is: def __on_timer(self, sender): pass b) The increase in memory usage increases indefinitely when self.CheckBox1.Enabled = True is called inside the timer routine. c) Could this be tracemalloc overhead? (the memory traces take memory space). For example does the memory gets released with tracemalloc.clear_traces() (tracemalloc — Trace memory allocations — Python 3.10.5 documentation) d) Do you see Windows memory allocated to the process increasing without tracemalloc
  2. pyscripter

    Returning lists from Python to Delphi

    I am not sure what is the problem you are trying to solve. Your "Second attempt" should work. In your "First attempt" myV := PythonModule1.GetVarAsVariant('myboxes'); fails because PythonModule1 has no variable 'myboxes'. myboxes will be part of the main module namespace after your code gets executed. GetVarAsVariant can be improved to avoid the access violation. function TPythonModule.GetVarAsVariant( const varName : AnsiString ) : Variant; var obj : PPyObject; begin CheckEngine; with Engine do begin obj := GetVar( varName ); if Assigned(obj) then try Result := PyObjectAsVariant( obj ); finally Py_XDecRef(obj); end; end; end;
  3. pyscripter

    Returning lists from Python to Delphi

    for var V in VarPyIterate(MainModule.myboxes) do V will be a python variant pointing to whatever is inside myboxes. Listbox1.items.add(V); The above converts the python variant to string.
  4. pyscripter

    Returning lists from Python to Delphi

    You can use either VarPy or the low-level python API to manipulate python tuples (e.g PyTuple_GetItem, PyTuple_SetItem etc.) with VarPyth the unit tests give you a good idea of things you can do: c := VarPythonCreate([1, 2, 3, 4], stTuple); // test a tuple Assert.IsTrue( VarIsPythonTuple(c) ); Assert.IsTrue( VarIsPythonSequence(c) ); Assert.IsTrue( c.GetItem(1) = 2 ); Assert.IsTrue( c.Length = 4 ); c := NewPythonTuple(3); c.SetItem(0, 1); c.SetItem(1, 2); c.SetItem(2, 3); Assert.IsTrue( VarIsPythonTuple(c) ); Assert.IsTrue( VarIsPythonSequence(c) ); Assert.IsTrue( c.GetItem(1) = 2 ); Assert.IsTrue( c.Length = 3 ); from python4delphi/VarPythTest.pas at master · pyscripter/python4delphi (github.com)
  5. pyscripter

    Returning lists from Python to Delphi

    The function return value is a tuple containing your lists.
  6. Args is a tuple not a dictionary. Search in the P4D source code for PyArg_ParseTuple to see how to make use of Args. There are hundreds of examples!
  7. pyscripter

    DUnitX and StackTraces

    I am new to DUnitX and I am trying to get stacktraces working, so that when a test case fails, I can see the source code line containing the Assertion that failed. I have done the following: Added DUnitX.StackTrace.Jcl, to the project uses clause. Edited C:\Program Files (x86)\Embarcadero\Studio\20.0\source\DunitX\DUnitX.Stacktrace.inc to enable the JCL define Compiled generating a full MAP file which was converted to jdbg and inserted into the executable. The MAP file is also present. I still do not get stack traces for failures. What am I missing? Another small issue is the XML report shows asserts="0" despite the fact that there are many Assert.IsTrue in the Test cases. Is this feature not working?
  8. np_array.SetItem(0) should work. From VarPyth source code: // here we handle a special case: COM (or Delphi?) doesn't allow you to have index properties // on a variant except from a true array variant! So if myVar is a Python variant that holds // a Python list, I can't write something like: myVar[0]! But if the list is a member of a // Python instance, it will work fine! Like: myInst.myList[0] // So, to handle this problem we detect some special names: GetItem, SetItem and Length // that will do the same as: // myList[0] <-> myList.GetItem(0) // myDict['Hello'] := 1 <-> myDict.SetItem('Hello', 1) // len(myList) <-> myList.Length() // we get some bonus with the slices and in operators also: // myList = [0, 1, 2, 3]; myList.GetSlice(1, 2) --> [1, 2] // myList.Contains(2) <-> 2 in myList
  9. pyscripter

    Data exchange between Delphi and SciKit Learn

    Have you seen python4delphi/Tutorials/Webinar II at master · pyscripter/python4delphi (github.com)? The VarPyth demo shows how to work with numpy arrays (two way link).
  10. pyscripter

    Cancel script execution

    Study demo 33 on how to interrupt a running python thread. (TSortThread.Stop).
  11. pyscripter

    Problems with webinar examples

    SynEdit has been updated since these demos have been created. I am not sure what version of SynEdit you are using, but the solution to such problems is to open the unit with the form in the IDE ignoring all errors and then save the unit again.
  12. pyscripter

    SynEdit just got a major uplift

    Getit has not been updated. To use the latest version use the Github repo instead.
  13. pyscripter

    TTreeNode leak when VCL styles are active

    @balabuevThis appears to be fixed in Delphi 11.1, and I cannot reproduce it anymore. But I cannot see any change that could have fixed that. Any ideas,
  14. pyscripter

    SynEdit just got a major uplift

    A new Word-like spell checking has been added to SynEdit.
  15. I always thought class vars are supposed to be initialized. See also this Stackoverflow question. However in Alexandria 11.1 I got bitten by this: type TMyComponent = class(TComponent) public class var GlobalInstance: TMyComponent ; constructor Create(AOwner: TComponent); override; end constructor TMyComponent .Create(AOwner: TComponent); begin inherited Create(AOwner); if Assigned(GlobalInstance) then raise Exception.Create('TMyComponent is a singleton. You can only have one!'); GlobalInstance := Self; end; When I added such a component to a data module in the 11.1 IDE an exception was raised. It was working fine with 11.0. Was I making a wrong assumption or is this an Alexandria 11.1 bug? Update: My stupid fault. Actually the error has occurred when I closed one project and opened another one with the same component. I should set the GlobalInstance to nil in the desctructor, which I did not. Even then the above approach is problematic if for instance you have have two projects in the same project group using this component. I think this would create a problem. Any better way of having a singleton component without causing issues in the IDE?
  16. pyscripter

    SynEdit preferred version?

    The master branch of https://github.com/TurboPack/SynEdit contains the latest version of SynEdit. Apparently, the compiler directive should be {$IF CompilerVersion <= 33} and this has now been fixed. SynEdit strives to be compatible with Delphi Berlin and later. If you find incompatibilities please report them to the issue tracker so that they can be fixed. There were many new properties introduced and some removed, so forms with SynEdit need to be loaded and saved again after installing the new version. Just ignore the errors you get opening the files.
  17. pyscripter

    SynEdit just got a major uplift

    A new feature has been added: Scintilla like indicators.
  18. In d2d1svg.h (Direct2D) there is the following declaration of the ID2D1SvgElement interface: /// <summary> /// Sets an attribute of this element using a POD type. Returns an error if the /// attribute name is not valid on this element. Returns an error if the attribute /// cannot be expressed as the specified type. /// </summary> STDMETHOD(SetAttributeValue)( _In_ PCWSTR name, D2D1_SVG_ATTRIBUTE_POD_TYPE type, _In_reads_bytes_(valueSizeInBytes) CONST void *value, UINT32 valueSizeInBytes ) PURE; D2D1_SVG_ATTRIBUTE_POD_TYPE = DWord; I am translating this as function SetAttributeValue(name: LPWSTR; _type: D2D1_SVG_ATTRIBUTE_POD_TYPE; value: Pointer; valueSizeInBytes: UINT32): HResult; overload; stdcall; But it crashes when it is used. The assembly prolog of SetAttributeValue is 564711F0 6690 nop 564711F2 55 push ebp 564711F3 8BEC mov ebp,esp 564711F5 83EC14 sub esp,$14 564711F8 8B4508 mov eax,[ebp+$08] 564711FB 33C9 xor ecx,ecx 564711FD 53 push ebx 564711FE 56 push esi 564711FF 57 push edi 56471200 8D5808 lea ebx,[eax+$08] 56471203 85DB test ebx,ebx 56471205 6A08 push $08 56471207 0F44C1 cmovz eax,ecx 5647120A 8945F8 mov [ebp-$08],eax 5647120D 5A pop edx Any idea what I am doing wrong? Thanks!
  19. I was trying to improve the performance of SynEdit in handling files with super long lines (e.g. 50K characters). In doing so I came across an unexpected optimization that had a spectacular impact on performance. See this innocent looking function probably written two decades ago (fCasedLine is a PChar): function TSynCustomHighlighter.GetToken: string; var Len: Integer; begin Len := Run - fTokenPos; SetLength(Result, Len); if Len > 0 then StrLCopy(@Result[1], fCasedLine + fTokenPos, Len); end; By measuring I found that this function (called zillions of times) was a bottleneck and replaced it with: function TSynCustomHighlighter.GetToken: string; var Len: Integer; begin Len := Run - fTokenPos; SetString(Result, fCasedLine + fTokenPos, Len); end; The impact was massive. Both SetString and StrLCopy in x64 are implemented in pascal (not assembly) using Move. So initially I was puzzled. However here is the reason. Look at StrLCopy: function StrLCopy(Dest: PWideChar; const Source: PWideChar; MaxLen: Cardinal): PWideChar; var Len: Cardinal; begin Result := Dest; Len := StrLen(Source); if Len > MaxLen then Len := MaxLen; Move(Source^, Dest^, Len * SizeOf(WideChar)); Dest[Len] := #0; end; Can you see the problem? It is the call to StrLen(Source)! In my case the Source was very very long strings, the length of which was unnecessarily calculated the hard way. The lesson here is that you need to identify the bottlenecks by measuring. You may be in for surprises, sometimes pleasant ones,
  20. pyscripter

    String optimization

    TStopwatch and occasionally VTune.
  21. I think var SList := Shared<TMyStringList>.Make(TMyStringList.Create(dupIgnore, True, False)); should work.
  22. pyscripter

    python delphivcl development

    I asked you to submit an issue at Embarcadero/DelphiVCL4Python: Delphi's VCL library as a Python module for building Windows GUI (github.com), but you did not bother... The modules are built from this directory: python4delphi/Modules at master · Embarcadero/python4delphi (github.com)
  23. pyscripter

    Python4Delphi Tutorials/Information

    Look at the tutorials python4delphi/Tutorials at master · pyscripter/python4delphi (github.com) and demos python4delphi/Demos at master · pyscripter/python4delphi (github.com). You will also find a lot of stuff in python | Embarcadero RAD Studio, Delphi, & C++Builder Blogs
×