cProfile doesn't work for profiling Cython code

298 views
Skip to first unread message

Hao Wang

unread,
Dec 9, 2021, 6:21:59 AM12/9/21
to cython-users
Hi community:

I ran the following code snippet, and got the following error. Is there a way to fix the bug ?

#!/usr/bin/env python

from churn_predict import *
import cProfile

if __name__ == '__main__':

    for k in range(5, 6):
        for z in range(30, 31):
            cProfile.run(compute_main((k, z, 'output_'+str(k)+'_'+str(z))))

The error is below :

Traceback (most recent call last):
  File "churn_p.py", line 10, in <module>
    cProfile.run(compute_main((k, z, 'output_'+str(k)+'_'+str(z))))
  File "/usr/lib64/python3.6/cProfile.py", line 16, in run
    return _pyprofile._Utils(Profile).run(statement, filename, sort)
  File "/usr/lib64/python3.6/profile.py", line 55, in run
    prof.run(statement)
  File "/usr/lib64/python3.6/cProfile.py", line 95, in run
    return self.runctx(cmd, dict, dict)
  File "/usr/lib64/python3.6/cProfile.py", line 100, in runctx
    exec(cmd, globals, locals)
ValueError: source code string cannot contain null bytes

Stefan Behnel

unread,
Dec 9, 2021, 6:25:44 AM12/9/21
to cython...@googlegroups.com
This has nothing to do with Cython, just with your usage of cProfile. You are executing the compute_main function and then passing the return value (probably a string) to CProfile. Instead, you need to pass something executable into cProfile.

Stefan
Reply all
Reply to author
Forward
0 new messages