Skip to content

Supporting with-statements #128

@erikbgithub

Description

@erikbgithub

As the library already has a class, an open() method and a close() method, it would make sense to add the small steps needed to make it callable in a with statement. However, as it's a c-Extension I currently don't feel confident that I could write the code alone.

In Python I would add this:

class SpiDev(...):
    def __init__(self, param1, ...):
        ...
        self.param1 = param1
    ...
    def __enter__(self):
        self.open(self.param1)

    def __exit__(self):
        self.close()

    def __del__(self):
        self.close()

When the with statement is entered, the connection is opened. When it is left, the connection gets closed. Even in case of an exception. And just in case, the connection gets closed on object deletion as well.

More explanation regarding the value of supporting with.

Is there someone who has experience in transferring that idea to the C codebase here?

PS: Locally, I might simply add a wrapper class that gets the new methods added.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions