Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Recent enhancements #445

Nov 2, 2023 · 1 comments · 2 replies
Discussion options

Recent enhancements to P4D

There have been a large number of enhancements implemented in the Python for Delphi library recently. This discussion provides an overview of these developments.

Python API encapsulation enhancements

Wrapping of Delphi classes and objects

The WrapDelphi unit allows you to easily wrap your Pascal classes, objects, records and interfaces and expose them to python. It is the technology that makes it possible to expose the whole of Vcl and Fmx to python as extension modules. Over the last couple of months, it has been enhanced and expanded in a number of important ways. Here follows a summary of the new features.

  • A new generic class wrapper TPyClassWrapper<T: class> makes it very easy to wrap Delphi classes, without having to create TRegisteredUnit descendants and implementing RegisterWrappers. See below for an example of how to use it.

  • Delphi classes are now exposed as first-class python types. This means that you can use the python functions isinstance. issubclass, dir with them and you can call their class methods. Here is an example:

        class TTest
          class procedure DoSomething;
          class procedure DoSomethingElse; static;
        end;
    
       // In FormCreate
       PyDelphiWrapper.RegisterDelphiWrapper(TPyClassWrapper<TTest>).Initialize;

    Then in your python code:

      from delph_module import Test
      Test.DoSomething()
      Test.DoSomethingElse()
  • Class references are also fully supported as function arguments and function results.

  • Class methods and properties are automatically exposed at the class (python type) level. You no longer need to manually expose classes using RegisterMethods, RegisterGetSets etc. Credit to @lmbelo for coming up with the idea and an initial implementation.

  • Published class events are handled automatically. There is no need to create and register TEventHandler subclasses. This includes events that have var parameters. See the unit tests.

  • Functions and procedures with var and out parameters are now fully supported. Here is an example of how to use them:

         class TTest
           class procedure Func1(var I: Integer): Integer;
           class procedure Proc(var S: string);
         end;

    Then in your python code:

      from delph_module import Test
      res, i = Test.Func(i)
      s = Test.Proc(s)
  • Automatic wrapping of indexed properties including the handling of default properties. See the related unit test.

Studying the unit tests for WrapDelphi will help you to better understand how to use the above enhancements, as well as the other features of WrapDelphi.

You must be logged in to vote

Replies: 1 comment · 2 replies

Comment options

🤞

You must be logged in to vote
2 replies
@maxkleiner
Comment options

Just tested with the 64bit maXbox and Python 3.12 got a
debug: AssertionError: SRE module mismatch 0 err:20
but I think its the missing path environment cause we do have different installs:
python312_Screenshot 2023-11-16 191102

@pyscripter
Comment options

pyscripter Nov 17, 2023
Maintainer Author

This is unrelated to P4D and has to do with your python installations. Just google for "SRE module mismatch".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.