From 3ae81137c8b8dc93ebdf846e1224150012040b75 Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Fri, 11 Mar 2011 18:44:10 +0000 Subject: Reverted bug fixes for #11444 (fc4d045e3170) and #11424 (b9d76846bb1c), which should not have been made in this branch. --- Lib/logging/__init__.py | 3 -- Lib/logging/config.py | 10 ++--- Lib/test/test_logging.py | 113 ----------------------------------------------- 3 files changed, 5 insertions(+), 121 deletions(-) diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index a3b3e39..b48bee8 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -1513,15 +1513,12 @@ def shutdown(handlerList=_handlerList): #errors might occur, for example, if files are locked #we just ignore them if raiseExceptions is not set try: - h.acquire() h.flush() h.close() except: if raiseExceptions: raise #else, swallow - finally: - h.release() #Let's try and shutdown automatically on application exit... try: diff --git a/Lib/logging/config.py b/Lib/logging/config.py index 2881e3f..eb2c248 100644 --- a/Lib/logging/config.py +++ b/Lib/logging/config.py @@ -232,14 +232,14 @@ def _install_loggers(cp, handlers, disable_existing_loggers): propagate = 1 logger = logging.getLogger(qn) if qn in existing: - i = existing.index(qn) + 1 # start with the entry after qn + i = existing.index(qn) prefixed = qn + "." pflen = len(prefixed) num_existing = len(existing) - while i < num_existing: - if existing[i][:pflen] == prefixed: - child_loggers.append(existing[i]) - i += 1 + i = i + 1 # look at the entry after qn + while (i < num_existing) and (existing[i][:pflen] == prefixed): + child_loggers.append(existing[i]) + i = i + 1 existing.remove(qn) if "level" in opts: level = cp.get(sectname, "level") diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py index fbbef64..27eeb27 100644 --- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -558,38 +558,6 @@ class ConfigFileTest(BaseTest): datefmt= """ - # config1a moves the handler to the root. - config1a = """ - [loggers] - keys=root,parser - - [handlers] - keys=hand1 - - [formatters] - keys=form1 - - [logger_root] - level=WARNING - handlers=hand1 - - [logger_parser] - level=DEBUG - handlers= - propagate=1 - qualname=compiler.parser - - [handler_hand1] - class=StreamHandler - level=NOTSET - formatter=form1 - args=(sys.stdout,) - - [formatter_form1] - format=%(levelname)s ++ %(message)s - datefmt= - """ - # config2 has a subtle configuration error that should be reported config2 = config1.replace("sys.stdout", "sys.stbout") @@ -668,44 +636,6 @@ class ConfigFileTest(BaseTest): datefmt= """ - # config7 adds a compiler logger. - config7 = """ - [loggers] - keys=root,parser,compiler - - [handlers] - keys=hand1 - - [formatters] - keys=form1 - - [logger_root] - level=WARNING - handlers=hand1 - - [logger_compiler] - level=DEBUG - handlers= - propagate=1 - qualname=compiler - - [logger_parser] - level=DEBUG - handlers= - propagate=1 - qualname=compiler.parser - - [handler_hand1] - class=StreamHandler - level=NOTSET - formatter=form1 - args=(sys.stdout,) - - [formatter_form1] - format=%(levelname)s ++ %(message)s - datefmt= - """ - def apply_config(self, conf): try: fn = tempfile.mktemp(".ini") @@ -775,49 +705,6 @@ class ConfigFileTest(BaseTest): def test_config6_ok(self): self.test_config1_ok(config=self.config6) - def test_config7_ok(self): - with captured_stdout() as output: - self.apply_config(self.config1a) - logger = logging.getLogger("compiler.parser") - # See issue #11424. compiler-hyphenated sorts - # between compiler and compiler.xyz and this - # was preventing compiler.xyz from being included - # in the child loggers of compiler because of an - # overzealous loop termination condition. - hyphenated = logging.getLogger('compiler-hyphenated') - # All will output a message - logger.info(self.next_message()) - logger.error(self.next_message()) - hyphenated.critical(self.next_message()) - self.assert_log_lines([ - ('INFO', '1'), - ('ERROR', '2'), - ('CRITICAL', '3'), - ], stream=output) - # Original logger output is empty. - self.assert_log_lines([]) - with captured_stdout() as output: - self.apply_config(self.config7) - logger = logging.getLogger("compiler.parser") - self.assertFalse(logger.disabled) - # Both will output a message - logger.info(self.next_message()) - logger.error(self.next_message()) - logger = logging.getLogger("compiler.lexer") - # Both will output a message - logger.info(self.next_message()) - logger.error(self.next_message()) - # Will not appear - hyphenated.critical(self.next_message()) - self.assert_log_lines([ - ('INFO', '4'), - ('ERROR', '5'), - ('INFO', '6'), - ('ERROR', '7'), - ], stream=output) - # Original logger output is empty. - self.assert_log_lines([]) - class LogRecordStreamHandler(StreamRequestHandler): """Handler for a streaming logging request. It saves the log message in the -- cgit v0.12 From 2c8c62e64d3c8438f13774d275755ad77a516ad6 Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Sat, 12 Mar 2011 11:05:32 +0000 Subject: Issue 11131: Fix sign of zero result on plus and minus operations in ROUND_FLOOR rounding mode. --- Lib/decimal.py | 19 +++++---- Lib/test/decimaltestdata/extra.decTest | 70 ++++++++++++++++++++++++++++++++++ Misc/NEWS | 3 ++ 3 files changed, 84 insertions(+), 8 deletions(-) diff --git a/Lib/decimal.py b/Lib/decimal.py index feba3d7..102fc8e 100644 --- a/Lib/decimal.py +++ b/Lib/decimal.py @@ -1068,14 +1068,16 @@ class Decimal(object): if ans: return ans - if not self: - # -Decimal('0') is Decimal('0'), not Decimal('-0') + if context is None: + context = getcontext() + + if not self and context.rounding != ROUND_FLOOR: + # -Decimal('0') is Decimal('0'), not Decimal('-0'), except + # in ROUND_FLOOR rounding mode. ans = self.copy_abs() else: ans = self.copy_negate() - if context is None: - context = getcontext() return ans._fix(context) def __pos__(self, context=None): @@ -1088,14 +1090,15 @@ class Decimal(object): if ans: return ans - if not self: - # + (-0) = 0 + if context is None: + context = getcontext() + + if not self and context.rounding != ROUND_FLOOR: + # + (-0) = 0, except in ROUND_FLOOR rounding mode. ans = self.copy_abs() else: ans = Decimal(self) - if context is None: - context = getcontext() return ans._fix(context) def __abs__(self, round=True, context=None): diff --git a/Lib/test/decimaltestdata/extra.decTest b/Lib/test/decimaltestdata/extra.decTest index fce8435..fe8b77a 100644 --- a/Lib/test/decimaltestdata/extra.decTest +++ b/Lib/test/decimaltestdata/extra.decTest @@ -2745,3 +2745,73 @@ pwmx437 power 17 1728 1729 -> 1 pwmx438 power 18 1728 1729 -> 1 pwmx439 power 19 1728 1729 -> 456 pwmx440 power 20 1728 1729 -> 1 + +-- plus and minus zero in various rounding modes (see issue 11131) +extended: 1 +precision: 9 +maxexponent: 384 +minexponent: -383 + +rounding: half_even +plux1000 plus 0.0 -> 0.0 +plux1001 plus -0.0 -> 0.0 +minx1000 minus 0.0 -> 0.0 +minx1001 minus -0.0 -> 0.0 +absx1000 abs 0.0 -> 0.0 +absx1001 abs -0.0 -> 0.0 + +rounding: half_up +plux1010 plus 0.0 -> 0.0 +minx1010 minus 0.0 -> 0.0 +plux1011 plus -0.0 -> 0.0 +minx1011 minus -0.0 -> 0.0 +absx1010 abs 0.0 -> 0.0 +absx1011 abs -0.0 -> 0.0 + +rounding: ceiling +plux1020 plus 0.0 -> 0.0 +minx1020 minus 0.0 -> 0.0 +plux1021 plus -0.0 -> 0.0 +minx1021 minus -0.0 -> 0.0 +absx1020 abs 0.0 -> 0.0 +absx1021 abs -0.0 -> 0.0 + +rounding: floor +plux1030 plus 0.0 -> 0.0 +minx1030 minus 0.0 -> -0.0 +plux1031 plus -0.0 -> -0.0 +minx1031 minus -0.0 -> 0.0 +absx1030 abs 0.0 -> 0.0 +absx1031 abs -0.0 -> 0.0 + +rounding: down +plux1040 plus 0.0 -> 0.0 +minx1040 minus 0.0 -> 0.0 +plux1041 plus -0.0 -> 0.0 +minx1041 minus -0.0 -> 0.0 +absx1040 abs 0.0 -> 0.0 +absx1041 abs -0.0 -> 0.0 + +rounding: up +plux1050 plus 0.0 -> 0.0 +minx1050 minus 0.0 -> 0.0 +plux1051 plus -0.0 -> 0.0 +minx1051 minus -0.0 -> 0.0 +absx1050 abs 0.0 -> 0.0 +absx1051 abs -0.0 -> 0.0 + +rounding: half_down +plux1060 plus 0.0 -> 0.0 +minx1060 minus 0.0 -> 0.0 +plux1061 plus -0.0 -> 0.0 +minx1061 minus -0.0 -> 0.0 +absx1060 abs 0.0 -> 0.0 +absx1061 abs -0.0 -> 0.0 + +rounding: 05up +plux1070 plus 0.0 -> 0.0 +minx1070 minus 0.0 -> 0.0 +plux1071 plus -0.0 -> 0.0 +minx1071 minus -0.0 -> 0.0 +absx1070 abs 0.0 -> 0.0 +absx1071 abs -0.0 -> 0.0 diff --git a/Misc/NEWS b/Misc/NEWS index c3117cb..54f23ba 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -43,6 +43,9 @@ Core and Builtins Library ------- +- Issue #11131: Fix sign of zero in plus and minus operations when + the context rounding mode is ROUND_FLOOR. + - Issue #5622: Fix curses.wrapper to raise correct exception if curses initialization fails. -- cgit v0.12 From 3c5edd00a76c2903a5d7df812860ed5c42015baa Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Sat, 12 Mar 2011 11:58:15 -0600 Subject: trunc -> math.trunc (closes #11475) --- Doc/library/numbers.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/numbers.rst b/Doc/library/numbers.rst index 300a6f3..a0a825f 100644 --- a/Doc/library/numbers.rst +++ b/Doc/library/numbers.rst @@ -47,7 +47,7 @@ The numeric tower To :class:`Complex`, :class:`Real` adds the operations that work on real numbers. - In short, those are: a conversion to :class:`float`, :func:`trunc`, + In short, those are: a conversion to :class:`float`, :func:`math.trunc`, :func:`round`, :func:`math.floor`, :func:`math.ceil`, :func:`divmod`, ``//``, ``%``, ``<``, ``<=``, ``>``, and ``>=``. -- cgit v0.12 From c3ab30b363530c17b71bdfad09a01fa9c653c10e Mon Sep 17 00:00:00 2001 From: Ezio Melotti Date: Sat, 12 Mar 2011 22:21:37 +0200 Subject: Fix typo. --- Doc/library/unittest.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst index 5df6155..42ae8e2 100644 --- a/Doc/library/unittest.rst +++ b/Doc/library/unittest.rst @@ -194,7 +194,7 @@ documentation explores the full feature set from first principles. .. _unittest-command-line-interface: -Command Line Interface +Command-Line Interface ---------------------- The unittest module can be used from the command line to run tests from -- cgit v0.12 From 5ddf51f73587fdd8348f1def87f8cf53aed0dd8e Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Sat, 12 Mar 2011 22:43:23 +0000 Subject: Fixes issue #11476: StreamHandler parameter name change documented. --- Doc/library/logging.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Doc/library/logging.rst b/Doc/library/logging.rst index 9898d08..17a761a 100644 --- a/Doc/library/logging.rst +++ b/Doc/library/logging.rst @@ -1895,6 +1895,8 @@ and :meth:`flush` methods). specified, the instance will use it for logging output; otherwise, *sys.stderr* will be used. + .. versionchanged:: 2.7 + The ``stream`` parameter was called ``strm`` in earlier versions. .. method:: emit(record) -- cgit v0.12 From 4f1a665faec81725bae335ba1d995e18a29383ea Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Sat, 12 Mar 2011 17:12:54 -0600 Subject: clarify condition for readline to return EOF (closes #11312) --- Doc/library/stdtypes.rst | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst index b532285..77c2da7 100644 --- a/Doc/library/stdtypes.rst +++ b/Doc/library/stdtypes.rst @@ -2388,11 +2388,12 @@ Files have the following methods: .. method:: file.readline([size]) - Read one entire line from the file. A trailing newline character is kept in the - string (but may be absent when a file ends with an incomplete line). [#]_ If - the *size* argument is present and non-negative, it is a maximum byte count - (including the trailing newline) and an incomplete line may be returned. An - empty string is returned *only* when EOF is encountered immediately. + Read one entire line from the file. A trailing newline character is kept in + the string (but may be absent when a file ends with an incomplete line). [#]_ + If the *size* argument is present and non-negative, it is a maximum byte + count (including the trailing newline) and an incomplete line may be + returned. When *size* is not 0, an empty string is returned *only* when EOF + is encountered immediately. .. note:: -- cgit v0.12 From dd6833d20a48adcd8abd2aa53a419f41c7295f4a Mon Sep 17 00:00:00 2001 From: Ezio Melotti Date: Sun, 13 Mar 2011 02:13:08 +0200 Subject: Fix markup in inputoutput.rst. --- Doc/tutorial/inputoutput.rst | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/Doc/tutorial/inputoutput.rst b/Doc/tutorial/inputoutput.rst index ca908a3..3441f54 100644 --- a/Doc/tutorial/inputoutput.rst +++ b/Doc/tutorial/inputoutput.rst @@ -103,17 +103,18 @@ Here are two ways to write a table of squares and cubes:: (Note that in the first example, one space between each column was added by the way :keyword:`print` works: it always adds spaces between its arguments.) -This example demonstrates the :meth:`rjust` method of string objects, which -right-justifies a string in a field of a given width by padding it with spaces -on the left. There are similar methods :meth:`ljust` and :meth:`center`. These -methods do not write anything, they just return a new string. If the input -string is too long, they don't truncate it, but return it unchanged; this will -mess up your column lay-out but that's usually better than the alternative, -which would be lying about a value. (If you really want truncation you can -always add a slice operation, as in ``x.ljust(n)[:n]``.) - -There is another method, :meth:`zfill`, which pads a numeric string on the left -with zeros. It understands about plus and minus signs:: +This example demonstrates the :meth:`str.rjust` method of string +objects, which right-justifies a string in a field of a given width by padding +it with spaces on the left. There are similar methods :meth:`str.ljust` and +:meth:`str.center`. These methods do not write anything, they just return a +new string. If the input string is too long, they don't truncate it, but +return it unchanged; this will mess up your column lay-out but that's usually +better than the alternative, which would be lying about a value. (If you +really want truncation you can always add a slice operation, as in +``x.ljust(n)[:n]``.) + +There is another method, :meth:`str.zfill`, which pads a numeric string on the +left with zeros. It understands about plus and minus signs:: >>> '12'.zfill(5) '00012' @@ -128,16 +129,16 @@ Basic usage of the :meth:`str.format` method looks like this:: We are the knights who say "Ni!" The brackets and characters within them (called format fields) are replaced with -the objects passed into the :meth:`~str.format` method. A number in the +the objects passed into the :meth:`str.format` method. A number in the brackets refers to the position of the object passed into the -:meth:`~str.format` method. :: +:meth:`str.format` method. :: >>> print '{0} and {1}'.format('spam', 'eggs') spam and eggs >>> print '{1} and {0}'.format('spam', 'eggs') eggs and spam -If keyword arguments are used in the :meth:`~str.format` method, their values +If keyword arguments are used in the :meth:`str.format` method, their values are referred to by using the name of the argument. :: >>> print 'This {food} is {adjective}.'.format( @@ -195,8 +196,8 @@ notation. :: >>> print 'Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table) Jack: 4098; Sjoerd: 4127; Dcab: 8637678 -This is particularly useful in combination with the new built-in :func:`vars` -function, which returns a dictionary containing all local variables. +This is particularly useful in combination with the built-in function +:func:`vars`, which returns a dictionary containing all local variables. For a complete overview of string formatting with :meth:`str.format`, see :ref:`formatstrings`. -- cgit v0.12 From ec40bab21ebc5758cf28325355f08d98e566979a Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Sun, 13 Mar 2011 08:45:19 +0200 Subject: Issue #11426: use 'with' statements on open files in CSV examples --- Doc/library/csv.rst | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/Doc/library/csv.rst b/Doc/library/csv.rst index 0394170..f4e9d7c 100644 --- a/Doc/library/csv.rst +++ b/Doc/library/csv.rst @@ -442,41 +442,44 @@ Examples The simplest example of reading a CSV file:: import csv - reader = csv.reader(open("some.csv", "rb")) - for row in reader: - print row + with open('some.csv', 'rb') as f: + reader = csv.reader(f) + for row in reader: + print row Reading a file with an alternate format:: import csv - reader = csv.reader(open("passwd", "rb"), delimiter=':', quoting=csv.QUOTE_NONE) - for row in reader: - print row + with open('passwd', 'rb') as f: + reader = csv.reader(f, delimiter=':', quoting=csv.QUOTE_NONE) + for row in reader: + print row The corresponding simplest possible writing example is:: import csv - writer = csv.writer(open("some.csv", "wb")) - writer.writerows(someiterable) + with open('some.csv', 'wb') as f: + writer = csv.writer(f) + writer.writerows(someiterable) Registering a new dialect:: import csv - csv.register_dialect('unixpwd', delimiter=':', quoting=csv.QUOTE_NONE) - - reader = csv.reader(open("passwd", "rb"), 'unixpwd') + with open('passwd', 'rb') as f: + reader = csv.reader(f, 'unixpwd') A slightly more advanced use of the reader --- catching and reporting errors:: import csv, sys - filename = "some.csv" - reader = csv.reader(open(filename, "rb")) - try: - for row in reader: - print row - except csv.Error, e: - sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e)) + filename = 'some.csv' + with open(filename, 'rb') as f: + reader = csv.reader(f) + try: + for row in reader: + print row + except csv.Error, e: + sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e)) And while the module doesn't directly support parsing strings, it can easily be done:: -- cgit v0.12 From 8b78a9d8ca9f7b0456abc40b7b5b6e8383ac781b Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Sun, 13 Mar 2011 19:35:04 +0100 Subject: Remove documentation to non-existent function PyObject_CopyToObject (fixes #11478) --- Doc/c-api/buffer.rst | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/Doc/c-api/buffer.rst b/Doc/c-api/buffer.rst index ee5d17a..a75b07b 100644 --- a/Doc/c-api/buffer.rst +++ b/Doc/c-api/buffer.rst @@ -266,20 +266,6 @@ Buffer related functions :cdata:`~Py_buffer.format`. -.. cfunction:: int PyObject_CopyToObject(PyObject *obj, void *buf, Py_ssize_t len, char fortran) - - Copy *len* bytes of data pointed to by the contiguous chunk of memory - pointed to by *buf* into the buffer exported by obj. The buffer must of - course be writable. Return 0 on success and return -1 and raise an error - on failure. If the object does not have a writable buffer, then an error - is raised. If *fortran* is ``'F'``, then if the object is - multi-dimensional, then the data will be copied into the array in - Fortran-style (first dimension varies the fastest). If *fortran* is - ``'C'``, then the data will be copied into the array in C-style (last - dimension varies the fastest). If *fortran* is ``'A'``, then it does not - matter and the copy will be made in whatever way is more efficient. - - .. cfunction:: int PyBuffer_IsContiguous(Py_buffer *view, char fortran) Return 1 if the memory defined by the *view* is C-style (*fortran* is -- cgit v0.12 From ad797b699df6dcbe360ad86631c54fdb7a31a188 Mon Sep 17 00:00:00 2001 From: Ezio Melotti Date: Sun, 13 Mar 2011 22:55:41 +0200 Subject: #11484: remove paragraph about with_traceback from 2.7 doc. --- Doc/library/exceptions.rst | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/Doc/library/exceptions.rst b/Doc/library/exceptions.rst index 5886768..8bd903a 100644 --- a/Doc/library/exceptions.rst +++ b/Doc/library/exceptions.rst @@ -63,18 +63,6 @@ The following exceptions are only used as base classes for other exceptions. assign a special meaning to the elements of this tuple, while others are usually called only with a single string giving an error message. - .. method:: with_traceback(tb) - - This method sets *tb* as the new traceback for the exception and returns - the exception object. It is usually used in exception handling code like - this:: - - try: - ... - except SomeException: - tb = sys.exc_info()[2] - raise OtherException(...).with_traceback(tb) - .. exception:: Exception -- cgit v0.12