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

line profiling of a cythonized generator doesn't work #1949

Closed
WeatherGod opened this issue Oct 26, 2017 · 2 comments
Closed

line profiling of a cythonized generator doesn't work #1949

WeatherGod opened this issue Oct 26, 2017 · 2 comments
Labels
Milestone

Comments

@WeatherGod
Copy link

Consider the following, derived from your collatz example in your tests:

collatz.pyx

# cython: linetrace=True

cimport cython

@cython.binding(True)
def cy_generator(int num):
   x = 1
   for i in range(num):
       yield x + 2

test_profile.py

try:
    import line_profiler
except ImportError:
    print("No line profiler, skipping test.")
    import sys
    sys.exit(0)


def assert_stats(profile, name):
    profile.print_stats()
    stats = profile.get_stats()
    assert len(stats.timings) > 0, "No profile stats."
    for key, timings in stats.timings.items():
        if key[-1] == name:
            assert len(timings) > 0
            break
    else:
        raise ValueError("No stats for %s." % name)


from collatz import cy_generator
func = cy_generator
profile = line_profiler.LineProfiler(func)
profile.runcall(func, 10)
assert_stats(profile, func.__name__)

And the usual setup.py (included for completeness):

from distutils.extension import Extension
from distutils.core import setup
from Cython.Build import cythonize

extensions = [
    Extension("collatz", ["collatz.pyx"], define_macros=[('CYTHON_TRACE', '1')])
]

setup(
    ext_modules = cythonize(extensions)
)

The test fails:

bash-4.2$ python setup.py build_ext --inplace
Compiling collatz.pyx because it changed.
[1/1] Cythonizing collatz.pyx
running build_ext
building 'collatz' extension
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -DCYTHON_TRACE=1 -I/home/broot/scratch/miniconda/include/python2.7 -c collatz.c -o build/temp.linux-x86_64-2.7/collatz.o
gcc -pthread -shared -L/home/broot/scratch/miniconda/lib -Wl,-rpath=/home/broot/scratch/miniconda/lib,--no-as-needed build/temp.linux-x86_64-2.7/collatz.o -L/home/broot/scratch/miniconda/lib -lpython2.7 -o /home/broot/Programs/tools/foobar/collatz.so
bash-4.2$ python test_profile.py 
Timer unit: 1e-06 s

Total time: 0 s
File: /home/broot/Programs/tools/foobar/collatz.pyx
Function: cy_generator at line 6

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     6                                           def cy_generator(int num):
     7                                              x = 1
     8                                              for i in range(num):
     9                                                  yield x + 2

Traceback (most recent call last):
  File "test_profile.py", line 25, in <module>
    assert_stats(profile, func.__name__)
  File "test_profile.py", line 15, in assert_stats
    assert len(timings) > 0
AssertionError
@scoder scoder added the defect label Oct 29, 2017
@scoder
Copy link
Contributor

scoder commented Oct 29, 2017

Confirmed. Generators do not currently contain line tracing hooks, but there seems to be a bit more than just that to fix.

@scoder scoder added this to the 0.28 milestone Oct 31, 2017
@scoder scoder closed this as completed in aeeef39 Oct 31, 2017
@WeatherGod
Copy link
Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants