flacjacket / pywlroots

Python binding to the wlroots library using cffi
University of Illinois/NCSA Open Source License
51 stars 12 forks source link

Proposal: Add helper function ptr_or_null to wlroots.__init__.py #160

Closed heuer closed 5 months ago

heuer commented 5 months ago

A lot of methods need to check if the input is None and use either the _ptr attribute of the input or call the C function with ffi.NULL:

def method(self, obj: SomePtrType | None) -> None:
    if obj is None:
        lib.something(self._ptr, ffi.NULL)
    else:
        lib.something(self._ptr, obj._ptr)

Having a helper function which either returns the _ptr attribute or ffi.NULL the code could be reduced to:

def method(self, obj: SomePtrType | None) -> None:
    obj_ptr = ptr_or_null(obj)
    lib.something(self._ptr, obj_ptr)

IMO it is a lot easier to read and to type.

Could be reduced to a one-liner, though:

def method(self, obj: SomePtrType | None) -> None:
    lib.something(self._ptr, ptr_or_null(obj))