Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-1280

Typesystem C++ Injections for __getattr__ (for MonkeyPatch)

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P4: Low
    • None
    • 5.14.2
    • Shiboken
    • None
    • All
    • c782714ad3e47b8a74342c95d64b69b6dd4cc270 (pyside/pyside-setup/5.15)

    Description

      I need to override a generated classes __getattr__ in the MonkeyPatch for attribute lookup at Python runtime:

      def _my_generated_class__getattr__(self: MyGeneratedClass, name: str):
          # me no works
          print(f'MyGeneratedClass.__getattr__ called for attribute {name}')
      
      def _my_generated_class__dir__(self: MyGeneratedClass):
          # me works!
          print(f'MyGeneratedClass.__dir__ called')
      
      # in the monkey patch function
      MyGeneratedClass.__getattr__ = _my_generated_class__getattr__
      MyGeneratedClass.__dir__ = _my_generated_class__dir__ # me works!
      

      From what I could observe, Shiboken does write out a "__setattr__" CPython Slot-Function, which in it's most simple case just wraps "PyObject_GenericSetattr". However the "__getattr__" pendant is not being generated, which translates to "nullptr" in "class_wrapper.cpp" code.

      Further it is also not possible to work around this by injecting custom code via Typesystem mechanics:

      <!-- a function string to "methods" and "SignatureStrings" array - yet "slot" still points to a "nullptr", which probably is an issue here -->
      <add-function signature="__getattr__(PyObject*)" return="PyObject*">
          <!-- THIS WILL BE SKIPPED COMPLETELY IN SHIBOKEN-GENERATOR <= 5.14.2 -->
          <inject-code file="my-injections.cpp" snippet="my-getattr-snippet" position="end" />
      </add-function>
      

      FYI:
      Currently I am using a "custom" (personally I'd call this rather a dirty hack) Shiboken patch that makes the code-injection work. However the approach became highly incompatible between just 5.14.1 and 5.14.2.

      Attachments

        Issue Links

          No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

              kleint Friedemann Kleint
              antis81 Nils Fenner
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes