Skip to content

Instantly share code, notes, and snippets.

@beaugunderson
Created October 16, 2018 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beaugunderson/68ea6424a4fdcf763ccad08e42a74974 to your computer and use it in GitHub Desktop.
Save beaugunderson/68ea6424a4fdcf763ccad08e42a74974 to your computer and use it in GitHub Desktop.
timedelta / cProfile repro
working: -25200
broken: no exception
broken: 2177280001.000001
*** PROFILER RESULTS ***
broken (repro.py:18)
function called 1 times
5 function calls in 0.000 seconds
Ordered by: cumulative time, internal time, call count
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 repro.py:18(broken)
2 0.000 0.000 0.000 0.000 {built-in method builtins.print}
1 0.000 0.000 0.000 0.000 {built-in method total_seconds}
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
0 0.000 0.000 profile:0(profiler)
from datetime import timedelta
from profilehooks import profile
# is this a silly thing to do? maybe, but dateutil does so:
# https://github.com/dateutil/dateutil/blob/5128426b52543faef00617125b4a977343fe0664/dateutil/tz/tz.py#L1050
total_seconds = getattr(timedelta, 'total_seconds')
def working(offset):
# this is a simplified version of dateutil.tz.tzoffset.__init__()
try:
value = total_seconds(offset)
print('working: no exception')
except:
value = offset
print('working:', value)
@profile
def broken(offset):
try:
value = total_seconds(offset)
print('broken: no exception')
except:
value = offset
print('broken:', value)
if __name__ == '__main__':
working(-25200)
broken(-25200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment