Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9e7f1d2

Browse files
committed
Merged revisions 61038,61042-61045,61047,61050,61053,61055-61056,61061-61062,61066,61068,61070,61083,61085,61092-61103 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r61098 | jeffrey.yasskin | 2008-02-28 05:45:36 +0100 (Thu, 28 Feb 2008) | 7 lines Move abc._Abstract into object by adding a new flag Py_TPFLAGS_IS_ABSTRACT, which forbids constructing types that have it set. The effect is to speed ./python.exe -m timeit -s 'import abc' -s 'class Foo(object): __metaclass__ = abc.ABCMeta' 'Foo()' up from 2.5us to 0.201us. This fixes issue 1762. ........ r61099 | jeffrey.yasskin | 2008-02-28 06:53:18 +0100 (Thu, 28 Feb 2008) | 3 lines Speed test_socketserver up from 28.739s to 0.226s, simplify the logic, and make sure all tests run even if some fail. ........ r61100 | jeffrey.yasskin | 2008-02-28 07:09:19 +0100 (Thu, 28 Feb 2008) | 21 lines Thread.start() used sleep(0.000001) to make sure it didn't return before the new thread had started. At least on my MacBook Pro, that wound up sleeping for a full 10ms (probably 1 jiffy). By using an Event instead, we can be absolutely certain that the thread has started, and return more quickly (217us). Before: $ ./python.exe -m timeit -s 'from threading import Thread' 't = Thread(); t.start(); t.join()' 100 loops, best of 3: 10.3 msec per loop $ ./python.exe -m timeit -s 'from threading import Thread; t = Thread()' 't.isAlive()' 1000000 loops, best of 3: 0.47 usec per loop After: $ ./python.exe -m timeit -s 'from threading import Thread' 't = Thread(); t.start(); t.join()' 1000 loops, best of 3: 217 usec per loop $ ./python.exe -m timeit -s 'from threading import Thread; t = Thread()' 't.isAlive()' 1000000 loops, best of 3: 0.86 usec per loop To be fair, the 10ms isn't CPU time, and other threads including the spawned one get to run during it. There are also some slightly more complicated ways to get back the .4us in isAlive() if we want. ........ r61101 | raymond.hettinger | 2008-02-28 10:23:48 +0100 (Thu, 28 Feb 2008) | 1 line Add repeat keyword argument to itertools.product(). ........ r61102 | christian.heimes | 2008-02-28 12:18:49 +0100 (Thu, 28 Feb 2008) | 1 line The empty tuple is usually a singleton with a much higher refcnt than 1 ........
1 parent 380f7f2 commit 9e7f1d2

8 files changed

Lines changed: 248 additions & 176 deletions

File tree

Doc/library/itertools.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ loops that truncate the stream.
327327
example :func:`islice` or :func:`takewhile`).
328328

329329

330-
.. function:: product(*iterables)
330+
.. function:: product(*iterables[, repeat])
331331

332332
Cartesian product of input iterables.
333333

@@ -340,11 +340,15 @@ loops that truncate the stream.
340340
so that if the inputs iterables are sorted, the product tuples are emitted
341341
in sorted order.
342342

343+
To compute the product of an iterable with itself, specify the number of
344+
repetitions with the optional *repeat* keyword argument. For example,
345+
``product(A, repeat=4)`` means the same as ``product(A, A, A, A)``.
346+
343347
Equivalent to the following except that the actual implementation does not
344348
build-up intermediate results in memory::
345349

346-
def product(*args):
347-
pools = map(tuple, args)
350+
def product(*args, **kwds):
351+
pools = map(tuple, args) * kwds.get('repeat', 1)
348352
if pools:
349353
result = [[]]
350354
for pool in pools:

Include/object.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,9 @@ given type object has a specified feature.
532532
#define Py_TPFLAGS_HAVE_VERSION_TAG (1L<<18)
533533
#define Py_TPFLAGS_VALID_VERSION_TAG (1L<<19)
534534

535+
/* Type is abstract and cannot be instantiated */
536+
#define Py_TPFLAGS_IS_ABSTRACT (1L<<20)
537+
535538
/* These flags are used to determine if a type is a subclass. */
536539
#define Py_TPFLAGS_INT_SUBCLASS (1L<<23)
537540
#define Py_TPFLAGS_LONG_SUBCLASS (1L<<24)

Lib/abc.py

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -52,50 +52,6 @@ def setx(self, value): ...
5252
__isabstractmethod__ = True
5353

5454

55-
class _Abstract(object):
56-
57-
"""Helper class inserted into the bases by ABCMeta (using _fix_bases()).
58-
59-
You should never need to explicitly subclass this class.
60-
"""
61-
62-
def __new__(cls, *args, **kwds):
63-
am = cls.__dict__.get("__abstractmethods__")
64-
if am:
65-
raise TypeError("Can't instantiate abstract class %s "
66-
"with abstract methods %s" %
67-
(cls.__name__, ", ".join(sorted(am))))
68-
if (args or kwds) and cls.__init__ is object.__init__:
69-
raise TypeError("Can't pass arguments to __new__ "
70-
"without overriding __init__")
71-
return super().__new__(cls)
72-
73-
@classmethod
74-
def __subclasshook__(cls, subclass):
75-
"""Abstract classes can override this to customize issubclass().
76-
77-
This is invoked early on by __subclasscheck__() below. It
78-
should return True, False or NotImplemented. If it returns
79-
NotImplemented, the normal algorithm is used. Otherwise, it
80-
overrides the normal algorithm (and the outcome is cached).
81-
"""
82-
return NotImplemented
83-
84-
85-
def _fix_bases(bases):
86-
"""Helper method that inserts _Abstract in the bases if needed."""
87-
for base in bases:
88-
if issubclass(base, _Abstract):
89-
# _Abstract is already a base (maybe indirectly)
90-
return bases
91-
if object in bases:
92-
# Replace object with _Abstract
93-
return tuple([_Abstract if base is object else base
94-
for base in bases])
95-
# Append _Abstract to the end
96-
return bases + (_Abstract,)
97-
98-
9955
class ABCMeta(type):
10056

10157
"""Metaclass for defining Abstract Base Classes (ABCs).
@@ -118,7 +74,6 @@ class ABCMeta(type):
11874
_abc_invalidation_counter = 0
11975

12076
def __new__(mcls, name, bases, namespace):
121-
bases = _fix_bases(bases)
12277
cls = super().__new__(mcls, name, bases, namespace)
12378
# Compute set of abstract method names
12479
abstracts = {name
@@ -129,7 +84,7 @@ def __new__(mcls, name, bases, namespace):
12984
value = getattr(cls, name, None)
13085
if getattr(value, "__isabstractmethod__", False):
13186
abstracts.add(name)
132-
cls.__abstractmethods__ = abstracts
87+
cls.__abstractmethods__ = frozenset(abstracts)
13388
# Set up inheritance registry
13489
cls._abc_registry = WeakSet()
13590
cls._abc_cache = WeakSet()

Lib/decimal.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,9 @@ def _round_down(self, prec):
16491649
else:
16501650
return -1
16511651

1652+
def __round__(self):
1653+
return self._round_down(0)
1654+
16521655
def _round_up(self, prec):
16531656
"""Rounds away from 0."""
16541657
return -self._round_down(prec)
@@ -1684,13 +1687,19 @@ def _round_ceiling(self, prec):
16841687
else:
16851688
return -self._round_down(prec)
16861689

1690+
def __ceil__(self):
1691+
return self._round_ceiling(0)
1692+
16871693
def _round_floor(self, prec):
16881694
"""Rounds down (not towards 0 if negative)"""
16891695
if not self._sign:
16901696
return self._round_down(prec)
16911697
else:
16921698
return -self._round_down(prec)
16931699

1700+
def __floor__(self):
1701+
return self._round_floor(0)
1702+
16941703
def _round_05up(self, prec):
16951704
"""Round down unless digit prec-1 is 0 or 5."""
16961705
if prec and self._int[prec-1] not in '05':

Lib/test/test_descrtut.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ def merge(self, other):
196196
'__setattr__',
197197
'__setitem__',
198198
'__str__',
199+
'__subclasshook__',
199200
'append',
200201
'count',
201202
'extend',

0 commit comments

Comments
 (0)