Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix timedelta segmentation fault #433

Merged
merged 1 commit into from Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.rst
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
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
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