summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Doc/c-api/buffer.rst14
-rw-r--r--Doc/library/csv.rst39
-rw-r--r--Doc/library/exceptions.rst12
-rw-r--r--Doc/library/logging.rst2
-rw-r--r--Doc/library/numbers.rst2
-rw-r--r--Doc/library/stdtypes.rst11
-rw-r--r--Doc/library/unittest.rst2
-rw-r--r--Doc/tutorial/inputoutput.rst33
-rw-r--r--Lib/decimal.py19
-rw-r--r--Lib/test/decimaltestdata/extra.decTest70
-rw-r--r--Misc/NEWS3
11 files changed, 132 insertions, 75 deletions
diff --git a/Doc/c-api/buffer.rst b/Doc/c-api/buffer.rst
index ee5d17a510..a75b07bf1c 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
diff --git a/Doc/library/csv.rst b/Doc/library/csv.rst
index 03941708ea..f4e9d7cb94 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::
diff --git a/Doc/library/exceptions.rst b/Doc/library/exceptions.rst
index 5886768567..8bd903a31e 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
diff --git a/Doc/library/logging.rst b/Doc/library/logging.rst
index 9898d08597..17a761a6f1 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)
diff --git a/Doc/library/numbers.rst b/Doc/library/numbers.rst
index 300a6f305c..a0a825f7b2 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 ``>=``.
diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst
index b532285f64..77c2da7e66 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::
diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst
index 5df61554bd..42ae8e2c57 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
diff --git a/Doc/tutorial/inputoutput.rst b/Doc/tutorial/inputoutput.rst
index ca908a3d32..3441f54211 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`.
diff --git a/Lib/decimal.py b/Lib/decimal.py
index feba3d7edd..102fc8e6e5 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 fce8435599..fe8b77a6dd 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 2b52138f2f..2cfafccda3 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.