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

WeatherGod opened this issue Oct 26, 2017 · 2 comments

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

WeatherGod opened this issue Oct 26, 2017 · 2 comments


Copy link

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


# cython: linetrace=True

cimport cython

def cy_generator(int num):
   x = 1
   for i in range(num):
       yield x + 2

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

def assert_stats(profile, name):
    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
        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 (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')])

    ext_modules = cythonize(extensions)

The test fails:

bash-4.2$ python 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/
bash-4.2$ python 
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 "", line 25, in <module>
    assert_stats(profile, func.__name__)
  File "", line 15, in assert_stats
    assert len(timings) > 0
@scoder scoder added the defect label Oct 29, 2017
Copy link

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
Copy link

Thank you!

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

No branches or pull requests

2 participants