Skip to content
Advertisement

How does pySerial implement the “with” statement without __enter__ and __exit__?

pySerial can be used with Python’s with statement like this:

JavaScript

I tried to see exactly what it is pySerial does to make this work, but I couldn’t find an __enter__ or __exit__ function anywhere! I ran grep -e '(__enter__)|(__exit__)' *.py from the pySerial directory, and grep didn’t find either function. How does pySerial do it?

Edit: I also tried the following to see what was called next:

JavaScript

This brought me directly to line 433 of serialposix.py, the close() function. Does the with statement also look for open() and close() functions?

Advertisement

Answer

Serial class doesn’t implement these methods, it inherits them from io.IOBase. The relevant part from /serial/serialposix.py:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement