Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misleading example in sys.set_coroutine_wrapper docs #74763

Closed
ncoghlan opened this issue Jun 6, 2017 · 3 comments
Closed

Misleading example in sys.set_coroutine_wrapper docs #74763

ncoghlan opened this issue Jun 6, 2017 · 3 comments
Labels
3.7 (EOL) end of life docs Documentation in the Doc dir type-feature A feature request or enhancement

Comments

@ncoghlan
Copy link
Contributor

ncoghlan commented Jun 6, 2017

BPO 30578
Nosy @ncoghlan, @vstinner, @giampaolo, @1st1, @carlbordum

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2020-10-05.09:04:52.713>
created_at = <Date 2017-06-06.02:44:32.976>
labels = ['type-feature', '3.7', 'docs']
title = 'Misleading example in sys.set_coroutine_wrapper docs'
updated_at = <Date 2020-10-05.09:04:52.712>
user = 'https://github.com/ncoghlan'

bugs.python.org fields:

activity = <Date 2020-10-05.09:04:52.712>
actor = 'vstinner'
assignee = 'docs@python'
closed = True
closed_date = <Date 2020-10-05.09:04:52.713>
closer = 'vstinner'
components = ['Documentation']
creation = <Date 2017-06-06.02:44:32.976>
creator = 'ncoghlan'
dependencies = []
files = []
hgrepos = []
issue_num = 30578
keywords = []
message_count = 3.0
messages = ['295231', '377937', '378007']
nosy_count = 6.0
nosy_names = ['ncoghlan', 'vstinner', 'giampaolo.rodola', 'docs@python', 'yselivanov', 'carlbordum']
pr_nums = []
priority = 'normal'
resolution = 'out of date'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue30578'
versions = ['Python 3.6', 'Python 3.7']

@ncoghlan
Copy link
Contributor Author

ncoghlan commented Jun 6, 2017

In bpo-24342, the invocation of coroutine wrappers specified via sys.set_coroutine_wrapper was fixed to catch and report the case of infinite recursion, where the wrapper attempts to instantiate a nested coroutine (which would call the wrapper, which would attempt to instantiate the coroutine, etc, etc)

The docs for sys.set_coroutine_wrapper include an example of this failure case: https://docs.python.org/3/library/sys.html#sys.set_coroutine_wrapper

However, if you're not reading carefully, it looks like an example of how to *use* sys.set_coroutine_wrapper, rather than an example of a case that won't work.

It would be better to include an example that either doesn't wrap the coroutine at all, or else uses one of the non-native coroutine emulations to avoid the infinite recursion problem:

import asyncio, sys

def wrapper(coro):
    @asyncio.coroutine
    def wrap(coro):
        print("Coroutine started")
        result = yield from coro
        print("Coroutine finished")
        return result
    return wrap(coro)

sys.set_coroutine_wrapper(wrapper)

async def foo():
    print("Coroutine running")
    return "Coroutine result"

import asyncio
asyncio.get_event_loop().run_until_complete(foo())

Also related: I discovered in writing this that "sys.set_coroutine_wrapper(None)" doesn't actually turn off coroutine wrapping. Instead, you still get this exception when attempting to recursively define an unwrapped one:

RuntimeError: coroutine wrapper <NULL> attempted to recursively wrap <code object wrap at 0x7eff31fbde40, file "<stdin>", line 2>)

That error was produced as follows:

import sys, contextlib

@contextlib.contextmanager
def disable_coroutine_wrapping():
    wrapper = sys.get_coroutine_wrapper()
    sys.set_coroutine_wrapper(None)
    try:
        yield
    finally:
        sys.set_coroutine_wrapper(wrapper)

def wrapper(coro):
    async def wrap(coro):
        print("Coroutine started")
        result = await coro
        print("Coroutine finished")
        return result
    with disable_coroutine_wrapping():
        return wrap(coro)

sys.set_coroutine_wrapper(wrapper)

async def foo():
    print("Coroutine running")
    return "Coroutine result"

import asyncio
asyncio.get_event_loop().run_until_complete(foo())

@ncoghlan ncoghlan added the 3.7 (EOL) end of life label Jun 6, 2017
@ncoghlan ncoghlan added docs Documentation in the Doc dir type-feature A feature request or enhancement labels Jun 6, 2017
@carlbordum
Copy link
Mannequin

carlbordum mannequin commented Oct 4, 2020

I think this can be closed as sys.set_coroutine_wrapper was removed in https://bugs.python.org/issue36933

@vstinner
Copy link
Member

vstinner commented Oct 5, 2020

Right, I close the issue.

@vstinner vstinner closed this as completed Oct 5, 2020
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life docs Documentation in the Doc dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants