Except both AttributeError and TypeError when trying to enter context manager
Since Python 3.11, a TypeError is now raised instead of an AttributeError when an object doesn't support the context manager protocol. We can safely catch both cases here.