> > Bearing in mind compatibility with existing simpletrace analysis > scripts, how about the following default method implementations? > > def __enter__(self): > self.begin() > > def __exit__(self, exc_type, exc_val, exc_tb): > if exc_type is None: > self.end() > return False > > Now simpletrace.py can switch to using the context manager and new > scripts can implement __enter__()/__exit__(), while old scripts continue > to work.
I was considering the same, but I was worried about double initialization if someone used both the context manager as well as calling .begin(). Should we add a guard, that prohibits this? Otherwise, we could also keep begin()/end() in Analyzer, and then make Analyzer2 a context manager? > > Please don't change the function signature since this is a public method > and we should avoid breaking existing callers when possible. > > Instead of: > > with analyzer_class() as analyzer: > > we can use: > > with analyzer: > ... I didn't think of that. Let's do this, if we keep the context manager.