]> granicus.if.org Git - python/commitdiff
Merge fix for issue #11501
authorAntoine Pitrou <solipsis@pitrou.net>
Tue, 15 Mar 2011 20:05:36 +0000 (21:05 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Tue, 15 Mar 2011 20:05:36 +0000 (21:05 +0100)
1  2 
Lib/test/support.py
Misc/NEWS

index 7a43f9760fdaa9ca13f1e2938e821b99f331d381,bef7161e9a1a229c5d272c88bfa45a9f0b0e6624..8d8e18735ebbae0db966e7b9ad1dc1d2a42f16ad
@@@ -1364,106 -1097,35 +1364,139 @@@ def strip_python_stderr(stderr)
      stderr = re.sub(br"\[\d+ refs\]\r?\n?$", b"", stderr).strip()
      return stderr
  
 +def args_from_interpreter_flags():
 +    """Return a list of command-line arguments reproducing the current
 +    settings in sys.flags."""
 +    flag_opt_map = {
 +        'bytes_warning': 'b',
 +        'dont_write_bytecode': 'B',
 +        'ignore_environment': 'E',
 +        'no_user_site': 's',
 +        'no_site': 'S',
 +        'optimize': 'O',
 +        'verbose': 'v',
 +    }
 +    args = []
 +    for flag, opt in flag_opt_map.items():
 +        v = getattr(sys.flags, flag)
 +        if v > 0:
 +            args.append('-' + opt * v)
 +    return args
 +
 +#============================================================
 +# Support for assertions about logging.
 +#============================================================
 +
 +class TestHandler(logging.handlers.BufferingHandler):
 +    def __init__(self, matcher):
 +        # BufferingHandler takes a "capacity" argument
 +        # so as to know when to flush. As we're overriding
 +        # shouldFlush anyway, we can set a capacity of zero.
 +        # You can call flush() manually to clear out the
 +        # buffer.
 +        logging.handlers.BufferingHandler.__init__(self, 0)
 +        self.matcher = matcher
 +
 +    def shouldFlush(self):
 +        return False
 +
 +    def emit(self, record):
 +        self.format(record)
 +        self.buffer.append(record.__dict__)
 +
 +    def matches(self, **kwargs):
 +        """
 +        Look for a saved dict whose keys/values match the supplied arguments.
 +        """
 +        result = False
 +        for d in self.buffer:
 +            if self.matcher.matches(d, **kwargs):
 +                result = True
 +                break
 +        return result
 +
 +class Matcher(object):
 +
 +    _partial_matches = ('msg', 'message')
 +
 +    def matches(self, d, **kwargs):
 +        """
 +        Try to match a single dict with the supplied arguments.
 +
 +        Keys whose values are strings and which are in self._partial_matches
 +        will be checked for partial (i.e. substring) matches. You can extend
 +        this scheme to (for example) do regular expression matching, etc.
 +        """
 +        result = True
 +        for k in kwargs:
 +            v = kwargs[k]
 +            dv = d.get(k)
 +            if not self.match_value(k, dv, v):
 +                result = False
 +                break
 +        return result
 +
 +    def match_value(self, k, dv, v):
 +        """
 +        Try to match a single stored value (dv) with a supplied value (v).
 +        """
 +        if type(v) != type(dv):
 +            result = False
 +        elif type(dv) is not str or k not in self._partial_matches:
 +            result = (v == dv)
 +        else:
 +            result = dv.find(v) >= 0
 +        return result
 +
 +
 +_can_symlink = None
 +def can_symlink():
 +    global _can_symlink
 +    if _can_symlink is not None:
 +        return _can_symlink
 +    try:
 +        os.symlink(TESTFN, TESTFN + "can_symlink")
 +        can = True
 +    except (OSError, NotImplementedError):
 +        can = False
 +    _can_symlink = can
 +    return can
 +
 +def skip_unless_symlink(test):
 +    """Skip decorator for tests that require functional symlink"""
 +    ok = can_symlink()
 +    msg = "Requires functional symlink implementation"
 +    return test if ok else unittest.skip(msg)(test)
++
+ def patch(test_instance, object_to_patch, attr_name, new_value):
+     """Override 'object_to_patch'.'attr_name' with 'new_value'.
+     Also, add a cleanup procedure to 'test_instance' to restore
+     'object_to_patch' value for 'attr_name'.
+     The 'attr_name' should be a valid attribute for 'object_to_patch'.
+     """
+     # check that 'attr_name' is a real attribute for 'object_to_patch'
+     # will raise AttributeError if it does not exist
+     getattr(object_to_patch, attr_name)
+     # keep a copy of the old value
+     attr_is_local = False
+     try:
+         old_value = object_to_patch.__dict__[attr_name]
+     except (AttributeError, KeyError):
+         old_value = getattr(object_to_patch, attr_name, None)
+     else:
+         attr_is_local = True
+     # restore the value when the test is done
+     def cleanup():
+         if attr_is_local:
+             setattr(object_to_patch, attr_name, old_value)
+         else:
+             delattr(object_to_patch, attr_name)
+     test_instance.addCleanup(cleanup)
+     # actually override the attribute
+     setattr(object_to_patch, attr_name, new_value)
diff --cc Misc/NEWS
index adc719bf2f5e1753ffcfb43e30240e4e498798cf,c4627fac66ace8d960b87fa05c26cbc8b9662983..6501b15e9bc6cc7d8e8197f8d8ab01bfb64b9b4c
+++ b/Misc/NEWS
@@@ -34,12 -40,10 +34,16 @@@ Core and Builtin
  Library
  -------
  
- - Issue #11500: Fixed a bug in the os x proxy bypass code for fully qualified 
+ - Issue #11501: disutils.archive_utils.make_zipfile no longer fails if zlib is
+   not installed. Instead, the zipfile.ZIP_STORED compression is used to create
+   the ZipFile. Patch by Natalia B. Bidart.
 +- Issue #11554: Fixed support for Japanese codecs; previously the body output
 +  encoding was not done if euc-jp or shift-jis was specified as the charset.
 +
++- Issue #11500: Fixed a bug in the os x proxy bypass code for fully qualified
 +  IP addresses in the proxy exception list. 
 +
  - Issue #11491: dbm.error is no longer raised when dbm.open is called with
    the "n" as the flag argument and the file exists. The behavior matches
    the documentation and general logic.