Skip to content

Commit

Permalink
Fix timedelta segmentation fault (#433)
Browse files Browse the repository at this point in the history
Fixes #431.
  • Loading branch information
adamchainz authored Mar 22, 2024
1 parent 8e28b9e commit ee93b04
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Changelog
=========

* Fix segmentation fault when the first ``travel()`` call in a process uses a ``timedelta``.

Thanks to Marcin Sulikowski for the report in `Issue #431 <https://github.com/adamchainz/time-machine/issues/431>`__.

2.14.0 (2024-03-03)
-------------------

Expand Down
3 changes: 2 additions & 1 deletion src/time_machine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import inspect
import os
import sys
import time as time_module
import uuid
from collections.abc import Generator
from time import gmtime as orig_gmtime
Expand Down Expand Up @@ -126,7 +127,7 @@ def extract_timestamp_tzname(
dest = dest.replace(tzinfo=dt.timezone.utc)
timestamp = dest.timestamp()
elif isinstance(dest, dt.timedelta):
timestamp = time() + dest.total_seconds()
timestamp = time_module.time() + dest.total_seconds()
elif isinstance(dest, dt.date):
timestamp = dt.datetime.combine(
dest, dt.time(0, 0), tzinfo=dt.timezone.utc
Expand Down
21 changes: 21 additions & 0 deletions tests/test_time_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import asyncio
import datetime as dt
import os
import subprocess
import sys
import time
import typing
import uuid
from contextlib import contextmanager
from importlib.util import module_from_spec
from importlib.util import spec_from_file_location
from textwrap import dedent
from unittest import SkipTest
from unittest import TestCase
from unittest import mock
Expand Down Expand Up @@ -472,6 +474,25 @@ def test_destination_timedelta():
assert now + 3600 <= time.time() <= now + 3601


def test_destination_timedelta_first_travel_in_process():
# Would previously segfault
subprocess.run(
[
sys.executable,
"-c",
dedent(
"""
from datetime import timedelta
import time_machine
with time_machine.travel(timedelta()):
pass
"""
),
],
check=True,
)


def test_destination_timedelta_negative():
now = time.time()
with time_machine.travel(dt.timedelta(seconds=-3600)):
Expand Down

0 comments on commit ee93b04

Please sign in to comment.