lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On 2018-07-16 09:21 AM, Pierre Chapuis wrote:
On Mon, Jul 16, 2018, at 03:17, Daurnimator wrote:

What doesn't nicely translate to lua is what to do in the `yield` case:

local function foo()
     with bar do
         coroutine.yield(baz) -- maybe baz contains something based on `bar`
     end
end
local f = coroutine.wrap(foo)
f()
if xyz then -- only *maybe* does the coroutine get resumed
     f()
end
I was unsure how that was dealt with in Python. This code:

     class Foo(object):

         def __init__(self):
             pass

         def __enter__(self):
             print("enter")
             return self

         def __exit__(self, *args):
             print("exit")

     foo = Foo()

     def my_gen_f():
         while True:
             with foo:
                 yield True

     def f():
         my_gen = my_gen_f()
         next(my_gen)
         print("after")

     f()

     print("finally")

prints:

     enter
     after
     exit
     finally

This is unrelated to the GC, disabling it does not change the output.


Run it on pypy. Keep in mind cpython does refcounting, which cannot be disabled.