コード例 #1
0
 def test_dynamic_var(self):
   var = utils.DynamicVar()
   self.assertIsNone(var.get())
   with var.bind(123):
     self.assertEqual(123, var.get())
     with var.bind(456):
       self.assertEqual(456, var.get())
     self.assertEqual(123, var.get())
   self.assertIsNone(var.get())
コード例 #2
0
import sys

from pytype import abstract
from pytype import utils
from pytype.pytd import pytd
from pytype.pytd import utils as pytd_utils

# "Error level" enum for distinguishing between warnings and errors:
SEVERITY_WARNING = 1
SEVERITY_ERROR = 2

# The set of known error names.
_ERROR_NAMES = set()

# The current error name, managed by the error_name decorator.
_CURRENT_ERROR_NAME = utils.DynamicVar()


def _error_name(name):
    """Decorate a function so that it binds the current error name."""
    _ERROR_NAMES.add(name)

    def wrap(func):
        def invoke(*args, **kwargs):
            with _CURRENT_ERROR_NAME.bind(name):
                return func(*args, **kwargs)

        return invoke

    return wrap