Skip to content

Commit ee93b04

Browse files
authored
Fix timedelta segmentation fault (#433)
Fixes #431.
1 parent 8e28b9e commit ee93b04

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

CHANGELOG.rst

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
Changelog
33
=========
44

5+
* Fix segmentation fault when the first ``travel()`` call in a process uses a ``timedelta``.
6+
7+
Thanks to Marcin Sulikowski for the report in `Issue #431 <https://github.com/adamchainz/time-machine/issues/431>`__.
8+
59
2.14.0 (2024-03-03)
610
-------------------
711

src/time_machine/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import inspect
66
import os
77
import sys
8+
import time as time_module
89
import uuid
910
from collections.abc import Generator
1011
from time import gmtime as orig_gmtime
@@ -126,7 +127,7 @@ def extract_timestamp_tzname(
126127
dest = dest.replace(tzinfo=dt.timezone.utc)
127128
timestamp = dest.timestamp()
128129
elif isinstance(dest, dt.timedelta):
129-
timestamp = time() + dest.total_seconds()
130+
timestamp = time_module.time() + dest.total_seconds()
130131
elif isinstance(dest, dt.date):
131132
timestamp = dt.datetime.combine(
132133
dest, dt.time(0, 0), tzinfo=dt.timezone.utc

tests/test_time_machine.py

+21
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import asyncio
44
import datetime as dt
55
import os
6+
import subprocess
67
import sys
78
import time
89
import typing
910
import uuid
1011
from contextlib import contextmanager
1112
from importlib.util import module_from_spec
1213
from importlib.util import spec_from_file_location
14+
from textwrap import dedent
1315
from unittest import SkipTest
1416
from unittest import TestCase
1517
from unittest import mock
@@ -472,6 +474,25 @@ def test_destination_timedelta():
472474
assert now + 3600 <= time.time() <= now + 3601
473475

474476

477+
def test_destination_timedelta_first_travel_in_process():
478+
# Would previously segfault
479+
subprocess.run(
480+
[
481+
sys.executable,
482+
"-c",
483+
dedent(
484+
"""
485+
from datetime import timedelta
486+
import time_machine
487+
with time_machine.travel(timedelta()):
488+
pass
489+
"""
490+
),
491+
],
492+
check=True,
493+
)
494+
495+
475496
def test_destination_timedelta_negative():
476497
now = time.time()
477498
with time_machine.travel(dt.timedelta(seconds=-3600)):

0 commit comments

Comments
 (0)