Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
heuer opened this issue Mar 17, 2024 · 0 comments · Fixed by #161
Closed

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

heuer opened this issue Mar 17, 2024 · 0 comments · Fixed by #161

Comments

@heuer
Copy link
Contributor

heuer commented Mar 17, 2024

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))
heuer added a commit to heuer/pywlroots that referenced this issue Mar 17, 2024
This helper function checks if the input is None and returns ffi.NULL,
otherwise the _ptr attribute.

Closes flacjacket#160.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant