refactor: Show deprecation warning on root context manager

This context manager can delete the entire instance of ModIO when using
`with rpi:` after creating an instance of ModIO by
`rpi = revpimodio.RevPiModIO(...)`.
This commit is contained in:
2024-11-07 11:24:22 +01:00
parent 579617d223
commit f3f81f3633

View File

@@ -208,6 +208,19 @@ class RevPiModIO(object):
self._myfh.close()
def __enter__(self):
# todo: Remove this context manager in future
warnings.warn(
"This context manager is deprecated and will be removed!\n\n"
"You should use the context manager of the IO object `with revpi.io:` "
"or with a single device `with revpi.device.my_device:`.\n\n"
"This deprecated context manager can be reproduced as follows:\n"
"```"
"revpi = revpimodio2.RevPiModIO()"
"with revpi.io:"
" ..."
"```",
DeprecationWarning,
)
if self._context_manager:
raise RuntimeError("can not use multiple context managers of same instance")
if self._looprunning: