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

Skip to content

Commit 23105d8

Browse files
committed
Merge.
2 parents ee4cca6 + 71215c5 commit 23105d8

38 files changed

Lines changed: 1004 additions & 466 deletions

Doc/c-api/exceptions.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ in various ways. There is a separate error indicator for each thread.
9090
the class in that case. If the values are already normalized, nothing happens.
9191
The delayed normalization is implemented to improve performance.
9292
93+
.. note::
94+
95+
This function *does not* implicitly set the ``__traceback__``
96+
attribute on the exception value. If setting the traceback
97+
appropriately is desired, the following additional snippet is needed::
98+
99+
if (tb != NULL) {
100+
PyException_SetTraceback(val, tb);
101+
}
102+
93103
94104
.. c:function:: void PyErr_Clear()
95105

Doc/library/concurrent.futures.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ Module Functions
371371

372372
Returns an iterator over the :class:`Future` instances (possibly created by
373373
different :class:`Executor` instances) given by *fs* that yields futures as
374-
they complete (finished or were cancelled). Any futures that completed
374+
they complete (finished or were cancelled). Any futures given by *fs* that
375+
are duplicated will be returned once. Any futures that completed
375376
before :func:`as_completed` is called will be yielded first. The returned
376377
iterator raises a :exc:`TimeoutError` if :meth:`~iterator.__next__` is
377378
called and the result isn't available after *timeout* seconds from the

Doc/reference/expressions.rst

Lines changed: 47 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -319,27 +319,25 @@ Yield expressions
319319
yield_atom: "(" `yield_expression` ")"
320320
yield_expression: "yield" [`expression_list` | "from" `expression`]
321321

322-
The :keyword:`yield` expression is only used when defining a :term:`generator`
323-
function,
324-
and can only be used in the body of a function definition. Using a
325-
:keyword:`yield` expression in a function definition is sufficient to cause that
326-
definition to create a generator function instead of a normal function.
322+
The yield expression is only used when defining a :term:`generator` function and
323+
thus can only be used in the body of a function definition. Using a yield
324+
expression in a function's body causes that function to be a generator.
327325

328326
When a generator function is called, it returns an iterator known as a
329327
generator. That generator then controls the execution of a generator function.
330328
The execution starts when one of the generator's methods is called. At that
331-
time, the execution proceeds to the first :keyword:`yield` expression, where it
332-
is suspended again, returning the value of :token:`expression_list` to
333-
generator's caller. By suspended we mean that all local state is retained,
334-
including the current bindings of local variables, the instruction pointer, and
335-
the internal evaluation stack. When the execution is resumed by calling one of
336-
the generator's methods, the function can proceed exactly as if the
337-
:keyword:`yield` expression was just another external call. The value of the
338-
:keyword:`yield` expression after resuming depends on the method which resumed
339-
the execution. If :meth:`~generator.__next__` is used (typically via either a
340-
:keyword:`for` or the :func:`next` builtin) then the result is :const:`None`,
341-
otherwise, if :meth:`~generator.send` is used, then the result will be the
342-
value passed in to that method.
329+
time, the execution proceeds to the first yield expression, where it is
330+
suspended again, returning the value of :token:`expression_list` to generator's
331+
caller. By suspended, we mean that all local state is retained, including the
332+
current bindings of local variables, the instruction pointer, and the internal
333+
evaluation stack. When the execution is resumed by calling one of the
334+
generator's methods, the function can proceed exactly as if the yield expression
335+
was just another external call. The value of the yield expression after
336+
resuming depends on the method which resumed the execution. If
337+
:meth:`~generator.__next__` is used (typically via either a :keyword:`for` or
338+
the :func:`next` builtin) then the result is :const:`None`. Otherwise, if
339+
:meth:`~generator.send` is used, then the result will be the value passed in to
340+
that method.
343341

344342
.. index:: single: coroutine
345343

@@ -349,11 +347,11 @@ suspended. The only difference is that a generator function cannot control
349347
where should the execution continue after it yields; the control is always
350348
transferred to the generator's caller.
351349

352-
:keyword:`yield` expressions are allowed in the :keyword:`try` clause of a
353-
:keyword:`try` ... :keyword:`finally` construct. If the generator is not
354-
resumed before it is finalized (by reaching a zero reference count or by being
355-
garbage collected), the generator-iterator's :meth:`~generator.close` method
356-
will be called, allowing any pending :keyword:`finally` clauses to execute.
350+
yield expressions are allowed in the :keyword:`try` clause of a :keyword:`try`
351+
... :keyword:`finally` construct. If the generator is not resumed before it is
352+
finalized (by reaching a zero reference count or by being garbage collected),
353+
the generator-iterator's :meth:`~generator.close` method will be called,
354+
allowing any pending :keyword:`finally` clauses to execute.
357355

358356
When ``yield from <expr>`` is used, it treats the supplied expression as
359357
a subiterator. All values produced by that subiterator are passed directly
@@ -373,11 +371,23 @@ the yield expression. It can be either set explicitly when raising
373371
.. versionchanged:: 3.3
374372
Added ``yield from <expr>`` to delegate control flow to a subiterator
375373

376-
The parentheses can be omitted when the :keyword:`yield` expression is the
377-
sole expression on the right hand side of an assignment statement.
374+
The parentheses may be omitted when the yield expression is the sole expression
375+
on the right hand side of an assignment statement.
378376

379-
.. index:: object: generator
377+
.. seealso::
378+
379+
:pep:`0255` - Simple Generators
380+
The proposal for adding generators and the :keyword:`yield` statement to Python.
381+
382+
:pep:`0342` - Coroutines via Enhanced Generators
383+
The proposal to enhance the API and syntax of generators, making them
384+
usable as simple coroutines.
380385

386+
:pep:`0380` - Syntax for Delegating to a Subgenerator
387+
The proposal to introduce the :token:`yield_from` syntax, making delegation
388+
to sub-generators easy.
389+
390+
.. index:: object: generator
381391

382392
Generator-iterator methods
383393
^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -395,13 +405,12 @@ is already executing raises a :exc:`ValueError` exception.
395405
.. method:: generator.__next__()
396406

397407
Starts the execution of a generator function or resumes it at the last
398-
executed :keyword:`yield` expression. When a generator function is resumed
399-
with a :meth:`~generator.__next__` method, the current :keyword:`yield`
400-
expression always evaluates to :const:`None`. The execution then continues
401-
to the next :keyword:`yield` expression, where the generator is suspended
402-
again, and the value of the :token:`expression_list` is returned to
403-
:meth:`next`'s caller.
404-
If the generator exits without yielding another value, a :exc:`StopIteration`
408+
executed yield expression. When a generator function is resumed with a
409+
:meth:`~generator.__next__` method, the current yield expression always
410+
evaluates to :const:`None`. The execution then continues to the next yield
411+
expression, where the generator is suspended again, and the value of the
412+
:token:`expression_list` is returned to :meth:`next`'s caller. If the
413+
generator exits without yielding another value, a :exc:`StopIteration`
405414
exception is raised.
406415

407416
This method is normally called implicitly, e.g. by a :keyword:`for` loop, or
@@ -411,12 +420,12 @@ is already executing raises a :exc:`ValueError` exception.
411420
.. method:: generator.send(value)
412421

413422
Resumes the execution and "sends" a value into the generator function. The
414-
``value`` argument becomes the result of the current :keyword:`yield`
415-
expression. The :meth:`send` method returns the next value yielded by the
416-
generator, or raises :exc:`StopIteration` if the generator exits without
417-
yielding another value. When :meth:`send` is called to start the generator,
418-
it must be called with :const:`None` as the argument, because there is no
419-
:keyword:`yield` expression that could receive the value.
423+
*value* argument becomes the result of the current yield expression. The
424+
:meth:`send` method returns the next value yielded by the generator, or
425+
raises :exc:`StopIteration` if the generator exits without yielding another
426+
value. When :meth:`send` is called to start the generator, it must be called
427+
with :const:`None` as the argument, because there is no yield expression that
428+
could receive the value.
420429

421430

422431
.. method:: generator.throw(type[, value[, traceback]])
@@ -478,20 +487,6 @@ For examples using ``yield from``, see :ref:`pep-380` in "What's New in
478487
Python."
479488

480489

481-
.. seealso::
482-
483-
:pep:`0255` - Simple Generators
484-
The proposal for adding generators and the :keyword:`yield` statement to Python.
485-
486-
:pep:`0342` - Coroutines via Enhanced Generators
487-
The proposal to enhance the API and syntax of generators, making them
488-
usable as simple coroutines.
489-
490-
:pep:`0380` - Syntax for Delegating to a Subgenerator
491-
The proposal to introduce the :token:`yield_from` syntax, making delegation
492-
to sub-generators easy.
493-
494-
495490
.. _primaries:
496491

497492
Primaries

Doc/reference/simple_stmts.rst

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -445,53 +445,26 @@ The :keyword:`yield` statement
445445
.. productionlist::
446446
yield_stmt: `yield_expression`
447447

448-
The :keyword:`yield` statement is only used when defining a generator function,
449-
and is only used in the body of the generator function. Using a :keyword:`yield`
450-
statement in a function definition is sufficient to cause that definition to
451-
create a generator function instead of a normal function.
452-
453-
When a generator function is called, it returns an iterator known as a generator
454-
iterator, or more commonly, a generator. The body of the generator function is
455-
executed by calling the :func:`next` function on the generator repeatedly until
456-
it raises an exception.
457-
458-
When a :keyword:`yield` statement is executed, the state of the generator is
459-
frozen and the value of :token:`expression_list` is returned to :meth:`next`'s
460-
caller. By "frozen" we mean that all local state is retained, including the
461-
current bindings of local variables, the instruction pointer, and the internal
462-
evaluation stack: enough information is saved so that the next time :func:`next`
463-
is invoked, the function can proceed exactly as if the :keyword:`yield`
464-
statement were just another external call.
465-
466-
The :keyword:`yield` statement is allowed in the :keyword:`try` clause of a
467-
:keyword:`try` ... :keyword:`finally` construct. If the generator is not
468-
resumed before it is finalized (by reaching a zero reference count or by being
469-
garbage collected), the generator-iterator's :meth:`close` method will be
470-
called, allowing any pending :keyword:`finally` clauses to execute.
471-
472-
When ``yield from <expr>`` is used, it treats the supplied expression as
473-
a subiterator, producing values from it until the underlying iterator is
474-
exhausted.
475-
476-
.. versionchanged:: 3.3
477-
Added ``yield from <expr>`` to delegate control flow to a subiterator
478-
479-
For full details of :keyword:`yield` semantics, refer to the :ref:`yieldexpr`
480-
section.
448+
A :keyword:`yield` statement is semantically equivalent to a :ref:`yield
449+
expression <yieldexpr>`. The yield statement can be used to omit the parentheses
450+
that would otherwise be required in the equivalent yield expression
451+
statement. For example, the yield statements ::
481452

482-
.. seealso::
453+
yield <expr>
454+
yield from <expr>
483455

484-
:pep:`0255` - Simple Generators
485-
The proposal for adding generators and the :keyword:`yield` statement to Python.
456+
are equivalent to the yield expression statements ::
486457

487-
:pep:`0342` - Coroutines via Enhanced Generators
488-
The proposal to enhance the API and syntax of generators, making them
489-
usable as simple coroutines.
458+
(yield <expr>)
459+
(yield from <expr>)
490460

491-
:pep:`0380` - Syntax for Delegating to a Subgenerator
492-
The proposal to introduce the :token:`yield_from` syntax, making delegation
493-
to sub-generators easy.
461+
Yield expressions and statements are only used when defining a :term:`generator`
462+
function, and are only used in the body of the generator function. Using yield
463+
in a function definition is sufficient to cause that definition to create a
464+
generator function instead of a normal function.
494465

466+
For full details of :keyword:`yield` semantics, refer to the
467+
:ref:`yieldexpr` section.
495468

496469
.. _raise:
497470

Doc/whatsnew/3.4.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ name of the codec responsible for producing the error::
264264
>>> import codecs
265265

266266
>>> codecs.decode(b"abcdefgh", "hex")
267+
Traceback (most recent call last):
268+
File "/usr/lib/python3.4/encodings/hex_codec.py", line 20, in hex_decode
269+
return (binascii.a2b_hex(input), len(input))
267270
binascii.Error: Non-hexadecimal digit found
268271

269272
The above exception was the direct cause of the following exception:
@@ -273,6 +276,11 @@ name of the codec responsible for producing the error::
273276
binascii.Error: decoding with 'hex' codec failed (Error: Non-hexadecimal digit found)
274277

275278
>>> codecs.encode("hello", "bz2")
279+
Traceback (most recent call last):
280+
File "/usr/lib/python3.4/encodings/bz2_codec.py", line 17, in bz2_encode
281+
return (bz2.compress(input), len(input))
282+
File "/usr/lib/python3.4/bz2.py", line 498, in compress
283+
return comp.compress(data) + comp.flush()
276284
TypeError: 'str' does not support the buffer interface
277285

278286
The above exception was the direct cause of the following exception:

Lib/asyncio/base_subprocess.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ def _process_exited(self, returncode):
114114
assert returncode is not None, returncode
115115
assert self._returncode is None, self._returncode
116116
self._returncode = returncode
117-
self._loop._subprocess_closed(self)
118117
self._call(self._protocol.process_exited)
119118
self._try_finish()
120119

Lib/asyncio/unix_events.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,6 @@ def _make_subprocess_transport(self, protocol, args, shell,
169169
def _child_watcher_callback(self, pid, returncode, transp):
170170
self.call_soon_threadsafe(transp._process_exited, returncode)
171171

172-
def _subprocess_closed(self, transp):
173-
pass
174-
175172

176173
def _set_nonblocking(fd):
177174
flags = fcntl.fcntl(fd, fcntl.F_GETFL)

Lib/asyncio/windows_events.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,6 @@ def _make_subprocess_transport(self, protocol, args, shell,
178178
yield from transp._post_init()
179179
return transp
180180

181-
def _subprocess_closed(self, transport):
182-
pass
183-
184181

185182
class IocpProactor:
186183
"""Proactor implementation using IOCP."""

Lib/codecs.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -475,22 +475,21 @@ def read(self, size=-1, chars=-1, firstline=False):
475475
# read until we get the required number of characters (if available)
476476
while True:
477477
# can the request be satisfied from the character buffer?
478-
if chars < 0:
479-
if size < 0:
480-
if self.charbuffer:
481-
break
482-
elif len(self.charbuffer) >= size:
483-
break
484-
else:
478+
if chars >= 0:
485479
if len(self.charbuffer) >= chars:
486480
break
481+
elif size >= 0:
482+
if len(self.charbuffer) >= size:
483+
break
487484
# we need more data
488485
if size < 0:
489486
newdata = self.stream.read()
490487
else:
491488
newdata = self.stream.read(size)
492489
# decode bytes (those remaining from the last call included)
493490
data = self.bytebuffer + newdata
491+
if not data:
492+
break
494493
try:
495494
newchars, decodedbytes = self.decode(data, self.errors)
496495
except UnicodeDecodeError as exc:

Lib/concurrent/futures/_base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ def as_completed(fs, timeout=None):
181181
182182
Returns:
183183
An iterator that yields the given Futures as they complete (finished or
184-
cancelled).
184+
cancelled). If any given Futures are duplicated, they will be returned
185+
once.
185186
186187
Raises:
187188
TimeoutError: If the entire result iterator could not be generated
@@ -190,11 +191,12 @@ def as_completed(fs, timeout=None):
190191
if timeout is not None:
191192
end_time = timeout + time.time()
192193

194+
fs = set(fs)
193195
with _AcquireFutures(fs):
194196
finished = set(
195197
f for f in fs
196198
if f._state in [CANCELLED_AND_NOTIFIED, FINISHED])
197-
pending = set(fs) - finished
199+
pending = fs - finished
198200
waiter = _create_and_install_waiters(fs, _AS_COMPLETED)
199201

200202
try:

0 commit comments

Comments
 (0)