Skip to content

Commit 15ebc88

Browse files
committed
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552-60567 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r60553 | neal.norwitz | 2008-02-03 17:53:09 +0100 (Sun, 03 Feb 2008) | 1 line Ignore leaky warnings from test_asynchat ........ r60555 | christian.heimes | 2008-02-03 20:51:13 +0100 (Sun, 03 Feb 2008) | 1 line Another int -> pid_t case ........ r60560 | amaury.forgeotdarc | 2008-02-03 23:51:43 +0100 (Sun, 03 Feb 2008) | 6 lines Ensure that PySet_Add() operates on a newly created frozenset, like PyTuple_SetItem does. Add PyFrozenSet_Check(), which was not needed before; The list of Py*Set_Check* macros seems to be complete now. Add missing NEWS entries about all this. ........ r60563 | amaury.forgeotdarc | 2008-02-04 00:14:32 +0100 (Mon, 04 Feb 2008) | 2 lines Nasty typo in setobject.h ........ r60564 | amaury.forgeotdarc | 2008-02-04 00:15:32 +0100 (Mon, 04 Feb 2008) | 3 lines Correct test_mailbox on win32: since the test sets a custom 'colon' attribute to the main mailbox, copy it to secondary mailbox instances. ........ r60565 | amaury.forgeotdarc | 2008-02-04 00:57:24 +0100 (Mon, 04 Feb 2008) | 2 lines Let test_socketserver pass on win32, which does not have AF_UNIX sockets. ........ r60566 | jeffrey.yasskin | 2008-02-04 02:04:35 +0100 (Mon, 04 Feb 2008) | 2 lines Make int() and long() fall back to __trunc__(). See issue 2002. ........ r60567 | christian.heimes | 2008-02-04 19:00:12 +0100 (Mon, 04 Feb 2008) | 3 lines Patch python#1953 I implemented the function sys._compact_freelists() and C API functions PyInt_/PyFloat_CompactFreeList() to compact the pre-allocated blocks of ints and floats. They allow the user to reduce the memory usage of a Python process that deals with lots of numbers. The patch also renames sys._cleartypecache to sys._clear_type_cache ........
1 parent fdb6bb5 commit 15ebc88

File tree

18 files changed

+257
-37
lines changed

18 files changed

+257
-37
lines changed

Doc/c-api/float.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,9 @@ Floating Point Objects
7272
.. cfunction:: double PyFloat_GetMin(void)
7373

7474
Return the minimum normalized positive float *DBL_MIN* as C :ctype:`double`.
75+
76+
.. cfunction:: void PyFloat_CompactFreeList(size_t *bc, size_t *bf, size_t *sum)
77+
78+
Compact the float free list. *bc* is the number of allocated blocks before
79+
blocks are freed, *bf* is the number of freed blocks and *sum* is the number
80+
of remaining objects in the blocks.

Doc/c-api/set.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ the constructor functions work with any iterable Python object.
5656

5757
.. versionadded:: 2.6
5858

59+
.. cfunction:: int PyFrozenSet_Check(PyObject *p)
60+
61+
Return true if *p* is a :class:`frozenset` object or an instance of a
62+
subtype.
63+
64+
.. versionadded:: 2.6
65+
5966
.. cfunction:: int PyAnySet_Check(PyObject *p)
6067

6168
Return true if *p* is a :class:`set` object, a :class:`frozenset` object, or an

Doc/library/sys.rst

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,28 @@ always available.
5454
A string containing the copyright pertaining to the Python interpreter.
5555

5656

57-
.. function:: _cleartypecache()
57+
.. function:: _compact_freelists()
5858

59-
Clear the internal type lookup cache.
59+
Compact the free list of floats by deallocating unused blocks.
60+
It can reduce the memory usage of the Python process several tenth of
61+
thousands of integers or floats have been allocated at once.
62+
63+
The return value is a tuple of tuples each containing three elements,
64+
amount of used objects, total block count before the blocks are deallocated
65+
and amount of freed blocks.
66+
67+
This function should be used for specialized purposes only.
68+
69+
.. versionadded:: 2.6
70+
71+
72+
.. function:: _clear_type_cache()
73+
74+
Clear the internal type cache. The type cache is used to speed up attribute
75+
and method lookups. Use the function *only* to drop unnecessary references
76+
during reference leak debugging.
77+
78+
This function should be used for internal and specialized purposes only.
6079

6180
.. versionadded:: 2.6
6281

Include/abstract.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,19 @@ xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/
792792

793793
PyAPI_FUNC(Py_ssize_t) PyNumber_AsSsize_t(PyObject *o, PyObject *exc);
794794

795+
/*
796+
Returns the Integral instance converted to an int. The
797+
instance is expected to be int or long or have an __int__
798+
method. Steals integral's reference. error_format will be
799+
used to create the TypeError if integral isn't actually an
800+
Integral instance. error_format should be a format string
801+
that can accept a char* naming integral's type.
802+
*/
803+
804+
PyAPI_FUNC(PyObject *) _PyNumber_ConvertIntegralToInt(
805+
PyObject *integral,
806+
const char* error_format);
807+
795808
/*
796809
Returns the object converted to Py_ssize_t by going through
797810
PyNumber_Index first. If an overflow error occurs while

Include/floatobject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ PyAPI_FUNC(void) _PyFloat_DigitsInit(void);
9191
PyAPI_FUNC(double) _PyFloat_Unpack4(const unsigned char *p, int le);
9292
PyAPI_FUNC(double) _PyFloat_Unpack8(const unsigned char *p, int le);
9393

94+
/* free list api */
95+
PyAPI_FUNC(void) PyFloat_CompactFreeList(size_t *, size_t *, size_t *);
96+
9497
#ifdef __cplusplus
9598
}
9699
#endif

Include/setobject.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ PyAPI_DATA(PyTypeObject) PySetIter_Type;
7575
PyType_IsSubtype(Py_TYPE(ob), &PySet_Type) || \
7676
PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
7777
#define PySet_Check(ob) \
78-
(Py_TYPE(ob) == &PySet_Type || PyType_IsSubtype(Py_TYPE(ob), &PySet_Type))
78+
(Py_TYPE(ob) == &PySet_Type || \
79+
PyType_IsSubtype(Py_TYPE(ob), &PySet_Type))
80+
#define PyFrozenSet_Check(ob) \
81+
(Py_TYPE(ob) == &PyFrozenSet_Type || \
82+
PyType_IsSubtype(Py_TYPE(ob), &PyFrozenSet_Type))
7983

8084
PyAPI_FUNC(PyObject *) PySet_New(PyObject *);
8185
PyAPI_FUNC(PyObject *) PyFrozenSet_New(PyObject *);

Lib/rational.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,6 @@ def __trunc__(a):
406406
else:
407407
return a.numerator // a.denominator
408408

409-
__int__ = __trunc__
410-
411409
def __floor__(a):
412410
"""Will be math.floor(a) in 3.0."""
413411
return a.numerator // a.denominator

Lib/test/regrtest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ def dash_R_cleanup(fs, ps, pic, abcs):
753753
sys.path_importer_cache.update(pic)
754754

755755
# clear type cache
756-
sys._cleartypecache()
756+
sys._clear_type_cache()
757757

758758
# Clear ABC registries, restoring previously saved ABC registries.
759759
for abc in [getattr(_abcoll, a) for a in _abcoll.__all__]:

Lib/test/test_builtin.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,14 @@ def test_int(self):
861861

862862
def test_intconversion(self):
863863
# Test __int__()
864+
class ClassicMissingMethods:
865+
pass
866+
self.assertRaises(TypeError, int, ClassicMissingMethods())
867+
868+
class MissingMethods(object):
869+
pass
870+
self.assertRaises(TypeError, int, MissingMethods())
871+
864872
class Foo0:
865873
def __int__(self):
866874
return 42
@@ -892,6 +900,49 @@ def __int__(self):
892900
self.assertEqual(int(Foo4()), 42)
893901
self.assertRaises(TypeError, int, Foo5())
894902

903+
class Classic:
904+
pass
905+
for base in (object, Classic):
906+
class IntOverridesTrunc(base):
907+
def __int__(self):
908+
return 42
909+
def __trunc__(self):
910+
return -12
911+
self.assertEqual(int(IntOverridesTrunc()), 42)
912+
913+
class JustTrunc(base):
914+
def __trunc__(self):
915+
return 42
916+
self.assertEqual(int(JustTrunc()), 42)
917+
918+
for trunc_result_base in (object, Classic):
919+
class Integral(trunc_result_base):
920+
def __int__(self):
921+
return 42
922+
923+
class TruncReturnsNonInt(base):
924+
def __trunc__(self):
925+
return Integral()
926+
self.assertEqual(int(TruncReturnsNonInt()), 42)
927+
928+
class NonIntegral(trunc_result_base):
929+
def __trunc__(self):
930+
# Check that we avoid infinite recursion.
931+
return NonIntegral()
932+
933+
class TruncReturnsNonIntegral(base):
934+
def __trunc__(self):
935+
return NonIntegral()
936+
try:
937+
int(TruncReturnsNonIntegral())
938+
except TypeError as e:
939+
self.assertEquals(str(e),
940+
"__trunc__ returned non-Integral"
941+
" (type NonIntegral)")
942+
else:
943+
self.fail("Failed to raise TypeError with %s" %
944+
((base, trunc_result_base),))
945+
895946
def test_iter(self):
896947
self.assertRaises(TypeError, iter)
897948
self.assertRaises(TypeError, iter, 42, 42)
@@ -1095,7 +1146,6 @@ def test_long(self):
10951146
self.assertEqual(int('2br45qc', 35), 4294967297)
10961147
self.assertEqual(int('1z141z5', 36), 4294967297)
10971148

1098-
10991149
def test_longconversion(self):
11001150
# Test __long__()
11011151
class Foo0:

Lib/test/test_mailbox.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ def test_consistent_factory(self):
517517
class FakeMessage(mailbox.MaildirMessage):
518518
pass
519519
box = mailbox.Maildir(self._path, factory=FakeMessage)
520+
box.colon = self._box.colon
520521
msg2 = box.get_message(key)
521522
self.assert_(isinstance(msg2, FakeMessage))
522523

0 commit comments

Comments
 (0)