[issue35756] Using `return value` in a generator function skips the returned value on for-loop iteration

2019-01-21 Thread bryan.koch


bryan.koch  added the comment:

Thanks for testing that.  I'm off to write an ugly `next()` wrapper then.

--

___
Python tracker 
<https://bugs.python.org/issue35756>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35756] Using `return value` in a generator function skips the returned value on for-loop iteration

2019-01-21 Thread bryan.koch


bryan.koch  added the comment:

steven your generator example is exactly what I wanted to do; looks like I'm 
upgrading to Python 3.8 for the new assignment syntax.

I was actually expecting the SyntaxError to be raised at runtime which would be 
a pretty large behavior change (definitely required to go through python-ideas) 
but I think my use case is covered by 3.8 and just upgrading is simpler to do.

Some details of the implementation that stirred this is that I'm streaming 
output from a hierarchy of generated modules and I get what is essentially 
(final value, EOF) as the last result so I need to yield the final value but 
for external reasons I need to perform the clean-up of native resources before 
yielding.

Let's consider this as closed since what I need is supported in 3.8.  Thank you 
for your help!

--

___
Python tracker 
<https://bugs.python.org/issue35756>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35756] Using `return value` in a generator function skips the returned value on for-loop iteration

2019-01-17 Thread bryan.koch


bryan.koch  added the comment:

Thank you both for the clarifications.  I agree these's no bug in `yield from` 
however is there a way to reference the return value when a generator with a 
return is invoked using `for val in gen` i.e. when the generator is invoked 
without delegation?

I could write my own wrapper around using `next` to work around this but it 
might be an oversight of the new grammar (new being relative) that the return 
value is only available when invoked from the `yield from` syntax.

Essentially I have code that looks like
`
for value in generator:
  do thing with value
  yield value
`
where I need to do something before yielding the value.  It would be awesome if 
invoking a generator above would throw a SyntaxError iff it contained a return 
and it wasn't invoked through `yield from`.

The below isn't valid Python and I'm not sure that it should be but it's what I 
need to do.

`
return_value = for value in generator:
  do thing with value
  yield value

if return_value:
  do something with return_value
`

--

___
Python tracker 
<https://bugs.python.org/issue35756>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35756] Using `return value` in a generator function skips the returned value on for-loop iteration

2019-01-16 Thread bryan.koch


bryan.koch  added the comment:

I understood the PEP to include `return expr` in the iteration values as per 
the first bullet of the proposal.

> Any values that the iterator yields are passed directly to the caller.

This bullet doesn't have any additional formatting on the word "yields" so I 
consider it as not directly referring to the "yield" keyword.

With the current implementation, I have to concern myself if a generator 
function was created with the intention of being called using `last_ret = yield 
from function(); yield last_ret` or as `for ret in function(): yield ret`.  The 
first also yields the return value but would also yield an additional `None` if 
a `return` was not the terminal cause; the second will miss the last value if 
the generator uses `return`.

Essentially, allowing `return expr` in generator functions without invoking the 
generator using `yield from generator` will lose the last value.

I support either of the below resolutions:
* `return expr` being invoked from a generator that is not being iterated using 
`yield from generator` is a SyntaxError
* `return expr` being invoked from a generator that is not being iterated using 
`yield from generator` includes the final return value in the iterated set

--

___
Python tracker 
<https://bugs.python.org/issue35756>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com