sip: Conditionally wrapping a virtual method?

Phil Thompson phil at riverbankcomputing.com
Mon Apr 6 10:24:29 BST 2026


On 06/04/2026 01:15, Scott Talbert wrote:
> On Thu, 2 Apr 2026, Phil Thompson wrote:
> 
>>> I'm trying to wrap a C++ class in wxPython that has a virtual method
>>> that is conditionally defined.  Very simplified, it looks like this:
>>> 
>>> class wxWindow
>>> {
>>>     public:
>>>         ...other methods...
>>> #if wxUSE_ACCESSIBILITY
>>>         virtual wxAccessible* CreateAccessible() { return nullptr; }
>>> #endif
>>> }
>>> 
>>> It seems like the sip way of doing this would be to use a feature 
>>> flag
>>> and guard this method with %If in the .sip file.  However, wxPython's
>>> build process pre-generates all the .cpp code as part of the sdist
>>> build process, so the generated .cpp code is used on all platforms,
>>> where wxUSE_ACCESSIBILITY is both defined and not.
>>> 
>>> Is there any way to solve this with sip using a cpp compile time 
>>> flag?
>>> If I include the method unconditionally the .sip file, it fails to
>>> compile on platforms where wxUSE_ACCESSIBILITY is not defined because
>>> the method is flagged with SIP_OVERRIDE and it doesn't override
>>> anything on those platforms.
>> 
>> The design of sip assumes the .sip files are distributed and the .cpp 
>> files are just build artifacts. The .sip files are part of an 
>> installation and allow 3rd party packages to extend them by %Importing 
>> them.
>> 
>> A feature results in a corresponding SIP_FEATURE_* macro to be defined 
>> so you could try overriding it on the compiler command line.
>> 
>> You could also try overriding the definition of SIP_OVERRIDE on the 
>> command line.
> 
> Yes, it's understandable that the expectation is that the .cpp files
> are not part of the sdist and are generated at build time.  However,
> pre-generating them shaves off a few minutes from our already long
> build time.  Presumably that's why Robin set things up this way
> originally.
> 
> I think I'd prefer to avoid overriding SIP_OVERRIDE, too, as that has
> been useful in finding bugs in the past.
> 
> How about this - would you be willing to emit #ifdef SIP_FEATURE_XYZ /
> #endif guards around code for enabled features?  Then, features could
> be disabled at compile time, which it seems like it would solve our
> problem and be relatively clean.

This is what sip did many, many years ago. To do it properly you have to 
support platforms and timelines as well and the resulting code 
complexity is horrendous (both the code generator and the generated 
code). It may be that the complexity is easier to manage now that sip is 
written in Python rather than C, but the changes needed to implement it 
would be huge.

I see the current situation as a strong positive. How does a 3rd party 
produce bindings for their own wxWidgets-based library that integrates 
properly with wxPython?

If the root cause of your current approach is that build times are too 
long then I think it's better to look at ways to reduce those times to 
make them acceptable. v6.15.0 included optimisations that reportedly 
made significant improvements and I'm sure there is more work that could 
be done.

Phil


More information about the PyQt mailing list