Skip to content

[3.12] cProfile counts 0 primitive calls for builtins.exec in certain scenario #106152

Closed
@jacobtylerwalls

Description

@jacobtylerwalls

Bug report

In 3.12, cProfile can count 0 primitive calls and miscalculate cumulative time in the following scenario.

For this layout:

script.py project/ typing.py 

And the file contents:

script.py

import project.typing

project/typing.py

from typing import Protocol class A(Protocol): ...

I observe this behavior difference with python3 -m cProfile script.py:

3.11.2

 697 function calls (693 primitive calls) in 0.001 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 2/1 0.000 0.000 0.001 0.001 {built-in method builtins.exec} 1 0.000 0.000 0.001 0.001 script.py:1(<module>) 

3.12.0b3

 635 function calls (630 primitive calls) in 0.001 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.001 0.001 script.py:1(<module>) ... 1/0 0.000 0.000 0.000 {built-in method builtins.exec} 

Notice the division by zero causing a missing percall stat for exec.


This means that in more substantial examples, the profiler will omit a significant portion of the relevant cumulative time. See this output for a 14.286 second script, I only have access to a cumtime for 0.800:

 45856453 function calls (31647888 primitive calls) in 14.286 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 4054 0.005 0.000 0.800 0.000 modutils.py:620(_spec_from_modpath) ... 

Your environment

  • CPython versions tested on: Python 3.12.0b3
  • Operating system and architecture: MacOS 13.4.1

Linked PRs

Metadata

Metadata

Labels

3.12only security fixes3.13bugs and security fixestype-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions