From ff25cc1c3d06aae545ccc6216ad1663a03dee59f Mon Sep 17 00:00:00 2001 From: Pierre-Marie de Rodat Date: Mon, 27 Jun 2016 13:16:44 +0200 Subject: [PATCH] diagnostics: add a Context class for reusable context managers Change-Id: I43cea197b2b4dd0c21f3465dd70470bd0c1782a2 TN: P614-007 --- langkit/diagnostics.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/langkit/diagnostics.py b/langkit/diagnostics.py index 50f3dc21c..a6beb777c 100644 --- a/langkit/diagnostics.py +++ b/langkit/diagnostics.py @@ -80,6 +80,22 @@ def context(message, location): context_stack.pop() +class Context(object): + """ + Like "context", but can be used more than once. + """ + + def __init__(self, message, location): + self.message = message + self.location = location + + def __enter__(self): + context_stack.append((self.message, self.location)) + + def __exit__(self, value, typ, traceback): + context_stack.pop() + + class DiagnosticError(Exception): pass