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

It won't raise expected error when nestedly use course_view decorator #395

Closed
dzhuang opened this issue Jan 3, 2018 · 11 comments
Closed

Comments

@dzhuang
Copy link
Contributor

dzhuang commented Jan 3, 2018

raise RuntimeError(

If I decorate

def render_course_page(pctx, template_name, args,
with course_view, it just failed with 404, not the expected error.

The _is_in_context_manager are unknow to each other in different CoursePageContext instances..

@dzhuang
Copy link
Contributor Author

dzhuang commented Jan 4, 2018

Can we just add an attribute _is_in_course_view to the HttpRequest object (i.e., pctx.request) instead, and raise the error at __init__ stage? The above tests failed at that stage and never has chance to __enter__.

@inducer
Copy link
Owner

inducer commented Jan 4, 2018

I don't understand what the problem is. Could you explain?

@dzhuang
Copy link
Contributor Author

dzhuang commented Jan 4, 2018

In short, I can't find a situation that could trigger the exception.

@inducer
Copy link
Owner

inducer commented Jan 5, 2018

If you do

with ...:
   with ...:
      pass

that should raise, no?

@dzhuang
Copy link
Contributor Author

dzhuang commented Jan 5, 2018

I tested with:

@coures_view
def func_a(pctx):
     ...
    return fuc_b(pctx)


@course_view
def func_b(pctx):
    ...

It doesn't raise the expected RuntimeError(), a 404 instead.

@inducer
Copy link
Owner

inducer commented Jan 5, 2018

But each decorator creates a new CoursePageContext instance. I'm not surprised that doesn't raise.

@dzhuang
Copy link
Contributor Author

dzhuang commented Jan 5, 2018

It's also the case for context manager, unless we are with the same instance?

@inducer
Copy link
Owner

inducer commented Jan 5, 2018

It can be, but it doesn't have to be. This form will make new instances (and hence won't conflict):

with CoursePageContext(...) as cpc:
   with CoursePageContext(...) as cpc2:
      pass

but this will:

cpc = CoursePageContext(...)
with cpc:
   with cpc:
      pass

It's perhaps an unlikely scenario to defend against, but I just don't like code that has the potential to be silently incorrect. (and it's easy to get into that situation if you're modifying state)

@dzhuang
Copy link
Contributor Author

dzhuang commented Jan 6, 2018

Got it. Thanks for the explanation.

@dzhuang
Copy link
Contributor Author

dzhuang commented Jan 14, 2018

Todo: add tests for this part.

@dzhuang
Copy link
Contributor Author

dzhuang commented Apr 22, 2018

Tested by #535 , closing.

@dzhuang dzhuang closed this as completed Apr 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants