Merged revisions 64089,64098,64100-64102,64113,64115-64116,64118,64120,64132,64342 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r64089 | armin.ronacher | 2008-06-10 22:37:02 +0200 (mar., 10 juin 2008) | 3 lines

  Fix a formatting error in the ast documentation.
........
  r64098 | raymond.hettinger | 2008-06-11 02:25:29 +0200 (mer., 11 juin 2008) | 6 lines

  Mini-PEP: Simplifying numbers.py
  * Convert binary methods in Integral to mixin methods
  * Remove three-arg __pow__ as a required method
  * Make __int__ the root method instead of __long__.
........
  r64100 | raymond.hettinger | 2008-06-11 02:28:51 +0200 (mer., 11 juin 2008) | 1 line

  Update numbers doc for the Integral simplification.
........
  r64101 | raymond.hettinger | 2008-06-11 02:44:47 +0200 (mer., 11 juin 2008) | 3 lines

  Handle the case with zero arguments.
........
  r64102 | benjamin.peterson | 2008-06-11 03:31:28 +0200 (mer., 11 juin 2008) | 4 lines

  convert test_struct to a unittest thanks to Giampaolo Rodola
  I had to disable one test because it was functioning incorrectly, see #1530559
  I also removed the debugging prints
........
  r64113 | thomas.heller | 2008-06-11 09:10:43 +0200 (mer., 11 juin 2008) | 2 lines

  Fix markup.
  Document the new 'offset' parameter for the 'ctypes.byref' function.
........
  r64115 | raymond.hettinger | 2008-06-11 12:30:54 +0200 (mer., 11 juin 2008) | 1 line

  Multi-arg form for set.difference() and set.difference_update().
........
  r64116 | raymond.hettinger | 2008-06-11 14:06:49 +0200 (mer., 11 juin 2008) | 1 line

  Issue 3051:  Let heapq work with either __lt__ or __le__.
........
  r64118 | raymond.hettinger | 2008-06-11 14:39:09 +0200 (mer., 11 juin 2008) | 1 line

  Optimize previous checkin for heapq.
........
  r64120 | raymond.hettinger | 2008-06-11 15:14:50 +0200 (mer., 11 juin 2008) | 1 line

  Add test for heapq using both __lt__ and __le__.
........
  r64132 | gregory.p.smith | 2008-06-11 20:00:52 +0200 (mer., 11 juin 2008) | 3 lines

  Correct an incorrect comment about our #include of stddef.h.
  (see Doug Evans' comment on python-dev 2008-06-10)
........
  r64342 | guido.van.rossum | 2008-06-17 19:38:02 +0200 (mar., 17 juin 2008) | 3 lines

  Roll back Raymond's -r64098 while we think of something better.
  (See issue 3056 -- we're close to a resolution but need unittests.)
........
This commit is contained in:
Amaury Forgeot d'Arc 2008-06-17 20:36:03 +00:00
parent 36817a984c
commit fdfe62d887
10 changed files with 616 additions and 647 deletions

View File

@ -135,7 +135,7 @@ and classes for traversing abstract syntax trees:
from untrusted sources without the need to parse the values oneself. from untrusted sources without the need to parse the values oneself.
.. function:: get_docstring(node, clean=True): .. function:: get_docstring(node, clean=True)
Return the docstring of the given *node* (which must be a Return the docstring of the given *node* (which must be a
:class:`FunctionDef`, :class:`ClassDef` or :class:`Module` node), or ``None`` :class:`FunctionDef`, :class:`ClassDef` or :class:`Module` node), or ``None``

View File

@ -1395,11 +1395,6 @@ GetLastError() and SetLastError() Windows api functions;
to request and change the ctypes private copy of the windows error to request and change the ctypes private copy of the windows error
code. code.
.. versionchanged:: 2.6
The `use_errno` and `use_last_error` parameters were added in Python
2.6.
.. data:: RTLD_GLOBAL .. data:: RTLD_GLOBAL
:noindex: :noindex:
@ -1561,22 +1556,23 @@ They are instances of a private class:
Assign a Python function or another callable to this attribute. The Assign a Python function or another callable to this attribute. The
callable will be called with three or more arguments: callable will be called with three or more arguments:
.. function:: callable(result, func, arguments)
:noindex:
.. function:: callable(result, func, arguments) ``result`` is what the foreign function returns, as specified
:noindex: by the :attr:`restype` attribute.
``result`` is what the foreign function returns, as specified by the ``func`` is the foreign function object itself, this allows
:attr:`restype` attribute. to reuse the same callable object to check or post process
the results of several functions.
``func`` is the foreign function object itself, this allows to reuse the same ``arguments`` is a tuple containing the parameters originally
callable object to check or post process the results of several functions. passed to the function call, this allows to specialize the
behavior on the arguments used.
``arguments`` is a tuple containing the parameters originally passed to the The object that this function returns will be returned from the
function call, this allows to specialize the behavior on the arguments used. foreign function call, but it can also check the result value
and raise an exception if the foreign function call failed.
The object that this function returns will be returned from the foreign
function call, but it can also check the result value and raise an exception
if the foreign function call failed.
.. exception:: ArgumentError() .. exception:: ArgumentError()
@ -1605,10 +1601,6 @@ type and the argument types of the function.
variable is exchanged with the real `errno` value bafore and after the call; variable is exchanged with the real `errno` value bafore and after the call;
`use_last_error` does the same for the Windows error code. `use_last_error` does the same for the Windows error code.
.. versionchanged:: 2.6
The optional `use_errno` and `use_last_error` parameters were
added.
.. function:: WINFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False) .. function:: WINFUNCTYPE(restype, *argtypes, use_errno=False, use_last_error=False)
@ -1786,11 +1778,19 @@ Utility functions
ctypes type or instance. ctypes type or instance.
.. function:: byref(obj) .. function:: byref(obj[, offset])
Returns a light-weight pointer to ``obj``, which must be an instance of a ctypes Returns a light-weight pointer to ``obj``, which must be an
type. The returned object can only be used as a foreign function call parameter. instance of a ctypes type. ``offset`` defaults to zero, it must be
It behaves similar to ``pointer(obj)``, but the construction is a lot faster. an integer which is added to the internal pointer value.
``byref(obj, offset)`` corresponds to this C code::
(((char *)&obj) + offset)
The returned object can only be used as a foreign function call
parameter. It behaves similar to ``pointer(obj)``, but the
construction is a lot faster.
.. function:: cast(obj, type) .. function:: cast(obj, type)

View File

@ -71,10 +71,10 @@ The numeric tower
.. class:: Integral .. class:: Integral
Subtypes :class:`Rational` and adds a conversion to :class:`long`, the Subtypes :class:`Rational` and adds a conversion to :class:`int`.
3-argument form of :func:`pow`, and the bit-string operations: ``<<``, Provides defaults for :func:`float`, :attr:`Rational.numerator`, and
``>>``, ``&``, ``^``, ``|``, ``~``. Provides defaults for :func:`float`, :attr:`Rational.denominator`, and bit-string operations: ``<<``,
:attr:`Rational.numerator`, and :attr:`Rational.denominator`. ``>>``, ``&``, ``^``, ``|``, ``~``.
Notes for type implementors Notes for type implementors

View File

@ -1517,21 +1517,15 @@ The constructors for both classes work the same:
Return a new set with elements from both sets. Return a new set with elements from both sets.
.. versionchanged:: 2.6
Accepts multiple input iterables.
.. method:: intersection(other, ...) .. method:: intersection(other, ...)
set & other & ... set & other & ...
Return a new set with elements common to both sets. Return a new set with elements common to both sets.
.. versionchanged:: 2.6 .. method:: difference(other, ...)
Accepts multiple input iterables. set - other - ...
.. method:: difference(other) Return a new set with elements in the set that are not in the others.
set - other
Return a new set with elements in the set that are not in *other*.
.. method:: symmetric_difference(other) .. method:: symmetric_difference(other)
set ^ other set ^ other
@ -1595,10 +1589,13 @@ The constructors for both classes work the same:
.. versionchanged:: 2.6 .. versionchanged:: 2.6
Accepts multiple input iterables. Accepts multiple input iterables.
.. method:: difference_update(other) .. method:: difference_update(other, ...)
set -= other set -= other | ...
Update the set, removing elements found in *other*. Update the set, removing elements found in others.
.. versionchanged:: 2.6
Accepts multiple input iterables.
.. method:: symmetric_difference_update(other) .. method:: symmetric_difference_update(other)
set ^= other set ^= other

View File

@ -35,7 +35,7 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
/* For uintptr_t, intptr_t */ /* For size_t? */
#ifdef HAVE_STDDEF_H #ifdef HAVE_STDDEF_H
#include <stddef.h> #include <stddef.h>
#endif #endif

View File

@ -197,6 +197,27 @@ class TestHeapPython(TestHeap):
class TestHeapC(TestHeap): class TestHeapC(TestHeap):
module = c_heapq module = c_heapq
def test_comparison_operator(self):
# Issue 3501: Make sure heapq works with both __lt__ and __le__
def hsort(data, comp):
data = [comp(x) for x in data]
self.module.heapify(data)
return [self.module.heappop(data).x for i in range(len(data))]
class LT:
def __init__(self, x):
self.x = x
def __lt__(self, other):
return self.x > other.x
class LE:
def __init__(self, x):
self.x = x
def __lt__(self, other):
return self.x >= other.x
data = [random.random() for i in range(100)]
target = sorted(data, reverse=True)
self.assertEqual(hsort(data, LT), target)
self.assertEqual(hsort(data, LE), target)
#============================================================================== #==============================================================================

View File

@ -105,6 +105,12 @@ class TestJointOps(unittest.TestCase):
self.assertEqual(self.thetype('abcba').intersection(C('ccb')), set('bc')) self.assertEqual(self.thetype('abcba').intersection(C('ccb')), set('bc'))
self.assertEqual(self.thetype('abcba').intersection(C('ef')), set('')) self.assertEqual(self.thetype('abcba').intersection(C('ef')), set(''))
self.assertEqual(self.thetype('abcba').intersection(C('cbcf'), C('bag')), set('b')) self.assertEqual(self.thetype('abcba').intersection(C('cbcf'), C('bag')), set('b'))
s = self.thetype('abcba')
z = s.intersection()
if self.thetype == frozenset():
self.assertEqual(id(s), id(z))
else:
self.assertNotEqual(id(s), id(z))
def test_isdisjoint(self): def test_isdisjoint(self):
def f(s1, s2): def f(s1, s2):
@ -144,6 +150,8 @@ class TestJointOps(unittest.TestCase):
self.assertEqual(self.thetype('abcba').difference(C('efgfe')), set('abc')) self.assertEqual(self.thetype('abcba').difference(C('efgfe')), set('abc'))
self.assertEqual(self.thetype('abcba').difference(C('ccb')), set('a')) self.assertEqual(self.thetype('abcba').difference(C('ccb')), set('a'))
self.assertEqual(self.thetype('abcba').difference(C('ef')), set('abc')) self.assertEqual(self.thetype('abcba').difference(C('ef')), set('abc'))
self.assertEqual(self.thetype('abcba').difference(), set('abc'))
self.assertEqual(self.thetype('abcba').difference(C('a'), C('b')), set('c'))
def test_sub(self): def test_sub(self):
i = self.s.difference(self.otherword) i = self.s.difference(self.otherword)
@ -470,6 +478,18 @@ class TestSet(TestJointOps):
self.assertEqual(s.difference_update(C(p)), None) self.assertEqual(s.difference_update(C(p)), None)
self.assertEqual(s, set(q)) self.assertEqual(s, set(q))
s = self.thetype('abcdefghih')
s.difference_update()
self.assertEqual(s, self.thetype('abcdefghih'))
s = self.thetype('abcdefghih')
s.difference_update(C('aba'))
self.assertEqual(s, self.thetype('cdefghih'))
s = self.thetype('abcdefghih')
s.difference_update(C('cdc'), C('aba'))
self.assertEqual(s, self.thetype('efghih'))
def test_isub(self): def test_isub(self):
self.s -= set(self.otherword) self.s -= set(self.otherword)
for c in (self.word + self.otherword): for c in (self.word + self.otherword):

File diff suppressed because it is too large Load Diff

View File

@ -17,13 +17,18 @@ static int
cmp_lt(PyObject *x, PyObject *y) cmp_lt(PyObject *x, PyObject *y)
{ {
int cmp; int cmp;
cmp = PyObject_RichCompareBool(x, y, Py_LT); static PyObject *lt = NULL;
if (cmp == -1 && PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear(); if (lt == NULL) {
cmp = PyObject_RichCompareBool(y, x, Py_LE); lt = PyUnicode_FromString("__lt__");
if (cmp != -1) if (lt == NULL)
cmp = 1 - cmp; return -1;
} }
if (PyObject_HasAttr(x, lt))
return PyObject_RichCompareBool(x, y, Py_LT);
cmp = PyObject_RichCompareBool(y, x, Py_LE);
if (cmp != -1)
cmp = 1 - cmp;
return cmp; return cmp;
} }

View File

@ -1304,6 +1304,9 @@ set_intersection_multi(PySetObject *so, PyObject *args)
Py_ssize_t i; Py_ssize_t i;
PyObject *result = (PyObject *)so; PyObject *result = (PyObject *)so;
if (PyTuple_GET_SIZE(args) == 0)
return set_copy(so);
Py_INCREF(so); Py_INCREF(so);
for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) { for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
PyObject *other = PyTuple_GET_ITEM(args, i); PyObject *other = PyTuple_GET_ITEM(args, i);
@ -1484,11 +1487,16 @@ set_difference_update_internal(PySetObject *so, PyObject *other)
} }
static PyObject * static PyObject *
set_difference_update(PySetObject *so, PyObject *other) set_difference_update(PySetObject *so, PyObject *args)
{ {
if (set_difference_update_internal(so, other) != -1) Py_ssize_t i;
Py_RETURN_NONE;
return NULL; for (i=0 ; i<PyTuple_GET_SIZE(args) ; i++) {
PyObject *other = PyTuple_GET_ITEM(args, i);
if (set_difference_update_internal(so, other) == -1)
return NULL;
}
Py_RETURN_NONE;
} }
PyDoc_STRVAR(difference_update_doc, PyDoc_STRVAR(difference_update_doc,
@ -1546,10 +1554,34 @@ set_difference(PySetObject *so, PyObject *other)
return result; return result;
} }
static PyObject *
set_difference_multi(PySetObject *so, PyObject *args)
{
Py_ssize_t i;
PyObject *result, *other;
if (PyTuple_GET_SIZE(args) == 0)
return set_copy(so);
other = PyTuple_GET_ITEM(args, 0);
result = set_difference(so, other);
if (result == NULL)
return NULL;
for (i=1 ; i<PyTuple_GET_SIZE(args) ; i++) {
other = PyTuple_GET_ITEM(args, i);
if (set_difference_update_internal((PySetObject *)result, other) == -1) {
Py_DECREF(result);
return NULL;
}
}
return result;
}
PyDoc_STRVAR(difference_doc, PyDoc_STRVAR(difference_doc,
"Return the difference of two sets as a new set.\n\ "Return the difference of two or more sets as a new set.\n\
\n\ \n\
(i.e. all elements that are in this set but not the other.)"); (i.e. all elements that are in this set but not the others.)");
static PyObject * static PyObject *
set_sub(PySetObject *so, PyObject *other) set_sub(PySetObject *so, PyObject *other)
{ {
@ -1563,16 +1595,12 @@ set_sub(PySetObject *so, PyObject *other)
static PyObject * static PyObject *
set_isub(PySetObject *so, PyObject *other) set_isub(PySetObject *so, PyObject *other)
{ {
PyObject *result;
if (!PyAnySet_Check(other)) { if (!PyAnySet_Check(other)) {
Py_INCREF(Py_NotImplemented); Py_INCREF(Py_NotImplemented);
return Py_NotImplemented; return Py_NotImplemented;
} }
result = set_difference_update(so, other); if (set_difference_update_internal(so, other) == -1)
if (result == NULL)
return NULL; return NULL;
Py_DECREF(result);
Py_INCREF(so); Py_INCREF(so);
return (PyObject *)so; return (PyObject *)so;
} }
@ -1963,9 +1991,9 @@ static PyMethodDef set_methods[] = {
copy_doc}, copy_doc},
{"discard", (PyCFunction)set_discard, METH_O, {"discard", (PyCFunction)set_discard, METH_O,
discard_doc}, discard_doc},
{"difference", (PyCFunction)set_difference, METH_O, {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
difference_doc}, difference_doc},
{"difference_update", (PyCFunction)set_difference_update, METH_O, {"difference_update", (PyCFunction)set_difference_update, METH_VARARGS,
difference_update_doc}, difference_update_doc},
{"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS, {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
intersection_doc}, intersection_doc},
@ -2087,7 +2115,7 @@ static PyMethodDef frozenset_methods[] = {
contains_doc}, contains_doc},
{"copy", (PyCFunction)frozenset_copy, METH_NOARGS, {"copy", (PyCFunction)frozenset_copy, METH_NOARGS,
copy_doc}, copy_doc},
{"difference", (PyCFunction)set_difference, METH_O, {"difference", (PyCFunction)set_difference_multi, METH_VARARGS,
difference_doc}, difference_doc},
{"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS, {"intersection",(PyCFunction)set_intersection_multi, METH_VARARGS,
intersection_doc}, intersection_doc},