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

Add support for generators #31

Closed
wants to merge 1 commit into from
Closed

Add support for generators #31

wants to merge 1 commit into from

Conversation

knixeur
Copy link

@knixeur knixeur commented Apr 23, 2019

Due to the nature of Python trating a method as a generator when a yield
keyword is defined, it's hard to make this dynamic.
I ended up adding an additional parameter is_generator which needs to
be set to True when snooping a generator.

An additional decorator could be defined to make it easier, like @pysnooper.snoopgen which could call snoop with is_generator=True

Fixes #9

Due to the nature of Python trating a method as a generator when a yield
keyword is defined, it's hard to make this dynamic.
I ended up adding an additional parameter is_generator which needs to
be set to True when snooping a generator.

Fixes #9
@cool-RR
Copy link
Owner

cool-RR commented Apr 23, 2019

  1. Use inspect.isgeneratorfunction to get rid of your generator argument.
  2. The context manager should be held when doing next. i.e. tracing should be active only when the generator is running. So instead of a for loop, you need a while True loop that does next until a StopIteration is thrown. It should acquire the tracer on every next.

@knixeur
Copy link
Author

knixeur commented Apr 23, 2019

  1. I tried that, but since Python finds a yield statement it always returns a generator. Feel free to patch this PR and/or base a better one from this.
  2. I'll do that, I was actually using yield from when I did these changes but it's not supported in Python 2.7.

Edit: Typo

@knixeur
Copy link
Author

knixeur commented Apr 23, 2019

Sorry, I won't have time to finish this. I tried quickly to apply change 2 but it's giving me some headaches, I'll close this PR and if I have some time in the following weeks I'll give it another chance.

Edit: typo

@knixeur knixeur closed this Apr 23, 2019
@cool-RR
Copy link
Owner

cool-RR commented Apr 23, 2019 via email

@cool-RR cool-RR changed the title Add support for generators, requires a flag when snooping Add support for generators Apr 24, 2019
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

Successfully merging this pull request may close these issues.

Feature request: Support generators
2 participants