Jump to content


  • Content Count

  • Joined

  • Last visited

  • Days Won


Posts posted by pyscripter

  1. 13 hours ago, Mark- said:

    Thanks for the response.

    Are you saying the examples demonstrate an IDE, with debugging, breakpoints, stepping through Python code, variable examination, etc.?


    Not quite.   You can use PyScripter code as a basis for providing embedded IDE functionality, however that would be non-trivial.

  2. 1 hour ago, wuwuxin said:

    To be exact,  Py_Begin_Allow_Threads should be called inside PythonEngine.OnAfterInit event,  NOT OnAfterLoad event as advised by @pyscripter 

    I said "after loading" loosely speaking and not "in the OnAfterLoad event".  Indeed you can only call Py_Begin_Allow_Threads after the engine is initialized.


    I have not tested, but you can set AutoFinalize to False and in the OnBeforeUnload do



    Also probably, skipping the above when the application closes down may not be necessary.

    • Like 1


    3 hours ago, wuwuxin said:

    PyGILState_Ensure is not blocking at all

    Of course it is.   If another thread holds the lock, PyGILState_Ensure blocks until the GIL becomes available, i.e. the holding thread calls PyGILState_Release.  If more than one threads are waiting for GIL then I think it is assigned of FCFS basis.  The automatic switching that you described only applies to threads created with the threading code.  All other threads need to get the GIL before executing python code and then release it.


    14 hours ago, wuwuxin said:

    If I use your utility function IPyEngineAndGIL, do I still need to call TPythonThread.Py_Begin_Allow_Threads  (calls PyEval_SaveThread) in the main thread, after loading the Python DLL?

    Yes you do need to call Py_Begin_Allow_Threads in the main thread.   When the python DLL loads, the main thread owns the GIL and it needs to release it before other threads can execute python code.



    • Like 1

  4. The OleSetClipboardData documentation claims that the Clipboard implements delayed rendering, meaning that it renders a given format of the IDataObject implementation, only when the format is actually requested.


    My DataObject implements CF_UNICODETEXT and CF_HTML formats.  As soon as I call OleSetClipboardData both formats are rendered.   Any idea why is that?  How can one implement delayed (lazy) rendering of clipboard formats?

  5. 1 hour ago, David Heffernan said:

    How would you handle non ANSI input?

    There is a corresponding event OnReceiveUniData.  Which is used is based on IO.UnicodeIO, which by default is true.


    • Like 1

  6. function  TPythonGUIInputOutput.ReceiveData : AnsiString;
      S : string;
      if Assigned( FOnReceiveData ) then
        Result := inherited ReceiveData
        InputQuery( 'Query from Python', 'Enter text', S);
        Result := AnsiString(S);

    You can handle the OnReceiveData event and respond in whatever way you like.

  7. 41 minutes ago, Uwe Raabe said:

    Do you by any chance happen to have Project Magician installed with Auto LibSuffix option active?

    YES INDEED.  That explains the mystery.   It almost drove me crazy.

  8. 7 minutes ago, Uwe Raabe said:

    In which build/platform configuration are you removing that value?

    32bit VCL Windows.

    I have the issue with both runtime and design time packages.


    Try to open for example the D10_4 packages of github.com/EtheaDev/SVGIconImageList

  9. Starting from Syndey update 2, I am experiencing the folowing:


    • Open in the IDE packages with no LIBSUFFIX or a LIBSUFFIX different that auto.
    • LIBSUFFIX 270 is added automatically and the package is named accordingly
    • Open project options and remove the LIBSUFFIX has no effect.   Next time you open the project options is back there.
    • Removing the LIBSUFFIX directive from the package source also has no effect on the package name.

    It seems that the IDE is enforcing the LIBSUFFIX 270.  I have not found a workaround.  You can still compile the packages from the command-line though.  Am I missing something?

  10. 9 hours ago, Edwin Yip said:

    Just in case you didn't know, WASM is a subset of JavaScript that's optimized for speed and cross-platform.

    Not quite.  WebAssembly (WASM in short) is a binary executable format supported by all major browsers.  See WebAssembly.


    WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable compilation target for programming languages, enabling deployment on the web for client and server applications.


    Various compilers of different programming languages produce WASM code.   Fpc is already moving ahead with support for Webassembly target. See WebAssembly/Roadmap - Lazarus wiki (freepascal.org).  Overall WebAssembly is an alternative to compilation to JS.  .Net has already a WASM target they call Blazor.

    • Like 2
    • Thanks 1

  11. The DLLPath was always meant to be the full path to the DLL.  If it worked it was by accident rather than by design.


    Why don't you just use:

    PythonEngine.DllPath  := TPath.Combine(ExtractFilePath(Application.ExeName), 'Python')  


    in the OnBeforeLoad event for instance?

  12. 26 minutes ago, David Heffernan said:

    I don't think this is true. TEdit is the Win32 EDIT control. What are you doing with strings that need to know what you are asking about? What's your usage scenario? 

    Mostly agree. The problem with TEdit is a Windows and not a Delphi one.  Notepad and VSCode have similar issues.


    If you are not rendering text it probably does not matter.  I got interested in this because SynEdit does not handle complex unicode characters well.  Other scenarios for using libraries such as ICU include proper sorting of multi-lingual text, proper change of capitalization, string normalization etc.  (I mean better in corner cases than what Windows provides and more compatible with Unicode standards), 



  13. 1 hour ago, vfbb said:

    Windows - DWriteTextAnalyze
    Android - GraphemeCharsLength := JString.codePointAt(Index);
    iOS - CFStringGetRangeOfComposedCharactersAtInd

    It appears that ICU is the way to go.  ICU is now bunded in Windows, .NET 5 is based on ICU, also android Unicode and internationalization support  |  Android Developers  and iOS ICU usage in Swift - Development / Standard Library - Swift Forums

    • Like 1
    • Thanks 1

  14. 37 minutes ago, Stefan Glienke said:

    But before it did not run in an endless loop

    I did say I did not test :classic_biggrin:.  One missing line now added to the code.  (FStart := FEnd);


    Works as expected in:

        var TestString := 'å'+#$0061#$0301#$0302#$0303#$0304;
        for var S in TextElements(TestString) do

    and yes it does not work as it should with complex emojis.



    Interestingly even VScode does not handle 🤷🏽‍♀️ correctly.  Paste the symbol and then try to delete it with Backspace.  It takes pressing backspace multiple time to actually delete this emoji.

  15. CharNext does handle surrogate pairs, diacritics and other multi-codepoint sequences reasonably well.  Cleary it does not handle emojis well.  Nor does any other Windows function that I know of. But I was mostly trying to show that doing the text enumeration is the easy part.



    CharNext works with default "user" expectations of characters when dealing with diacritics. For example: A string that contains U+0061 U+030a "LATIN SMALL LETTER A" + COMBINING RING ABOVE" — which looks like "å", will advance two code points, not one. A string that contains U+0061 U+0301 U+0302 U+0303 U+0304 — which looks like "a´^~¯", will advance five code points, not one, and so on.


    The .NET 5 unicode support is based on the ICU library.  See Globalization and ICU | Microsoft Docs for details.  There is an ICU Delphi wrapper but it has not been updated for years.


    ICU has been included in the Windows Creators update.  The dll names the the header files are listed here.  It would be nice to have translations of the headers to pascal and even better some higher level wrappers.

  16. Here is an implementation of a TextElementEnumerator (not tested):

    Note that in Windows CharNext is the best but still not perfect way to get text elements.  See What('s) a character! | The Old New Thing (microsoft.com)


    You should then be able to write code such as:


    for var Element in TextElements(MyString) do:


      TTextElementEnumerator = record
        FStart: PChar;
        FCurrent: string;
        constructor Create(const AValue: string);
        function MoveNext: Boolean; inline;
        function GetCurrent: string; inline;
        property Current: string read GetCurrent;
      TTextElementEnumeratorHelper = record
        FString: string;
        constructor Create(const AValue: string);
        function  GetEnumerator: TTextElementEnumerator;
    function TextElements(const AValue: string): TTextElementEnumeratorHelper;
    {$REGION Text Element Enumberator}
    { TTextElementEnumerator }
    constructor TTextElementEnumerator.Create(const AValue: string);
       FStart := PWideChar(AValue);
    function TTextElementEnumerator.GetCurrent: string;
      Result := FCurrent;
    function TTextElementEnumerator.MoveNext: Boolean;
      FEnd : PWideChar;
      if FStart^ = #0 then Exit(False);
      FEnd := Windows.CharNext(FStart);
      SetString(FCurrent, FStart, FEnd - FStart);
      FStart := FEnd;
      Result := True;
    { TTextElementEnumeratorHelper }
    constructor TTextElementEnumeratorHelper.Create(const AValue: string);
      FString := AValue;
    function TTextElementEnumeratorHelper.GetEnumerator: TTextElementEnumerator;
    function TextElements(const AValue: string): TTextElementEnumeratorHelper;

  17. Have you seen the last comment in AttributeError: partially initialized module 'cv2' has no attribute 'VideoCapture' · Issue #303 · pyscripter/python4delphi (github.com)

    I am not sure whether specifying the dependency with a manifest file as in P4DPython26 · pyscripter/python4delphi Wiki (github.com) might help.


    A minimal script reproducing the error would be a lot more helpful instead of your long python script.