[Python-checkins] r75400 - in python/trunk: Lib/test/regrtest.py Misc/NEWS

Nick Coghlan ncoghlan at gmail.com
Thu Oct 15 13:50:32 CEST 2009


r.david.murray wrote:
> +    # To add things to save and restore, add a name XXX to the resources list
> +    # and add corresponding get_XXX/restore_XXX functions.  get_XXX should
> +    # return the value to be saved and compared against a second call to the
> +    # get function when test execution completes.  restore_XXX should accept
> +    # the saved value and restore the resource using it.  It will be called if
> +    # and only if a change in the value is detected.  XXX will have any '_'
> +    # replaced with '.' characters and will then be used in the error messages
> +    # as the name of the resource that changed.

To allow this decorator to be used with resources that have an actual
"_" in their name, it may be better to do the lossy transformation the
other way around (i.e. use "." in the resource names, but then replace
it when looking up the corresponding method names).

An iterator that centralises the method lookups would also be nice.

> +
> +    resources = ('sys_argv', 'cwd', 'sys_stdin', 'sys_stdout', 'sys_stderr',
> +                 'os_environ', 'sys_path')

resources = ('sys.argv', 'cwd', 'sys.stdin', 'sys.stdout', 'sys.stderr',
                'os.environ', 'sys.path')

def resource_info(self):
  for name in self.resources:
    safe_name = name.replace(".", "_")
    get_name = 'get_'+safe_name
    restore_name = 'restore_'+safe_name
    yield name, getattr(self, get_name), getattr(self, restore_name)

> +    def __enter__(self):
> +        self.saved_values = dict((name, getattr(self, 'get_'+name)())
> +            for name in self.resources)
> +        return self

   def __enter__(self):
       self.saved_values = dict((name, get())
           for name, get, restore in self.resource_info())
       return self

> +    def __exit__(self, exc_type, exc_val, exc_tb):
> +        for name in self.resources:
> +            if not getattr(self, 'get_'+name)() == self.saved_values[name]:
> +                self.changed = True
> +                getattr(self, 'restore_'+name)(self.saved_values[name])
> +                if not self.quiet:
> +                    print >>sys.stderr, ("Warning -- {} was modified "
> +                        "by {}").format(name.replace('_', '.'), self.testname)
> +        return False

   def __exit__(self, exc_type, exc_val, exc_tb):
       for name, get, restore in self.resources:
           if get() != self.saved_values[name]:
               self.changed = True
               restore(self.saved_values[name])
               if not self.quiet:
                   print >>sys.stderr, ("Warning -- {} was modified "
                       "by {}").format(name, self.testname)
       return False

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------


More information about the Python-checkins mailing list