Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

tools: race_calendar.py names a connection that closes before it answers

xbsheng838667d658b5
diff --git a/tests/test_race_calendar_refusals.py b/tests/test_race_calendar_refusals.py
index 4c5e00f0e..4a9bd14c5 100644
--- a/tests/test_race_calendar_refusals.py
+++ b/tests/test_race_calendar_refusals.py
@@ -9,6 +9,11 @@ a sentence on stderr naming the path or the URL and saying that nothing was writ
code the other tools use for a run that could not do its job, next to 1 for a real answer (this season
has no race left to show) and 0 for a calendar in the theme.
+The two failures urllib does not wrap are here too: a connection closed before its status line
+(`http.client.RemoteDisconnected`) and a body that stops early (`IncompleteRead`, out of `json.load()`)
+are raised by `http.client` itself, so they went past every handler the script had and ended the run in
+a traceback with exit 1, which is this script's "no race left this season".
+
The exit codes and the folder rule are in the script's docstring as well, so the page a reader opens
first is not the only place they are written down.
@@ -17,6 +22,7 @@ Run from the repository root: python3 -m unittest discover -s tests -v
import argparse
import contextlib
import datetime
+import http.client
import io
import json
import os
@@ -55,6 +61,24 @@ def clock():
timezone=datetime.timezone)
+class CutShort:
+ """A response whose status line arrived and whose body stops early.
+
+ `json.load(response)` calls `read()`, and http.client answers a body shorter than its own
+ Content-Length with IncompleteRead - not a ValueError, so the "answered something that is not JSON"
+ refusal never saw it either.
+ """
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, *exc_info):
+ return False
+
+ def read(self, *args):
+ raise http.client.IncompleteRead(b'{"MRData": ', 3989)
+
+
class SeasonArgumentTest(unittest.TestCase):
"""--season takes the two forms Jolpica takes, and nothing else reaches it."""
@@ -359,6 +383,42 @@ class RefusalTest(unittest.TestCase):
status, stderr = self.run_main(str(self.theme), "--season", "2026")
self.assert_refused(status, stderr, "could not reach Jolpica at", "timed out")
+ def test_a_connection_closed_without_an_answer_is_named(self):
+ # A captive portal, a proxy that filters the host and a link that drops all accept the
+ # connection and close it without a byte. urllib wraps a refused connect in URLError, but
+ # http.client raises this one itself out of getresponse(), so it passed every handler and ended
+ # the run in a traceback whose exit status, 1, is this script's "no race left this season".
+ self.stub(raises=http.client.RemoteDisconnected("Remote end closed connection without response"))
+ status, stderr = self.run_main(str(self.theme), "--season", "2026")
+ self.assert_refused(status, stderr, "closed the connection before the answer was complete",
+ "api.jolpi.ca/ergast/f1/2026.json", "Remote end closed connection")
+ self.assertFalse(self.config.exists(), "a failed fetch wrote a config.json")
+
+ def test_an_answer_cut_short_is_named(self):
+ # The status line arrived and the body stopped halfway: IncompleteRead comes out of json.load(),
+ # not a ValueError, so the "answered something that is not JSON" refusal never saw it either.
+ with mock.patch.object(race_calendar.urllib.request, "urlopen", return_value=CutShort()) as urlopen:
+ status, stderr = self.run_main(str(self.theme), "--season", "2026")
+ self.assert_refused(status, stderr, "closed the connection before the answer was complete",
+ "api.jolpi.ca/ergast/f1/2026.json", "IncompleteRead")
+ urlopen.assert_called_once()
+ self.assertFalse(self.config.exists(), "a failed fetch wrote a config.json")
+
+ def test_the_two_unwrapped_fetch_failures_leave_an_existing_theme_byte_identical(self):
+ original = json.dumps({"greeting_name": "Olohuone", "columns": 5}, ensure_ascii=False) + "\n"
+ for payload in (
+ mock.patch.object(race_calendar.urllib.request, "urlopen",
+ side_effect=http.client.RemoteDisconnected("Remote end closed the connection")),
+ mock.patch.object(race_calendar.urllib.request, "urlopen", return_value=CutShort()),
+ ):
+ with self.subTest(payload=payload):
+ self.config.write_text(original, encoding="utf-8")
+ with payload as urlopen:
+ status, stderr = self.run_main(str(self.theme), "--season", "2026")
+ self.assert_refused(status, stderr, "closed the connection before the answer was complete")
+ urlopen.assert_called_once()
+ self.assertEqual(self.config.read_text(encoding="utf-8"), original)
+
def test_an_http_error_names_the_status_the_api_gave(self):
self.stub(raises=urllib.error.HTTPError(race_calendar.API.format(season="2026"), 503,
"Service Unavailable", {}, None))
diff --git a/tools/race_calendar.py b/tools/race_calendar.py
index d3c1f4b94..d4d55be1b 100644
--- a/tools/race_calendar.py
+++ b/tools/race_calendar.py
@@ -24,6 +24,7 @@ written on 1 or 2: the theme stays as it was.
"""
import argparse
import datetime
+import http.client
import json
from pathlib import Path
import sys
@@ -183,6 +184,17 @@ def main():
return refuse(f"could not reach Jolpica at {endpoint} ({getattr(e, 'reason', e)}). Nothing was "
f"written: this is the one step that goes online, so check the connection - a "
f"proxy or a captive portal is the usual cause - and run it again.")
+ except (ConnectionError, http.client.HTTPException) as e:
+ # The two shapes urllib does not wrap: a connection closed before a status line
+ # (http.client.RemoteDisconnected) and a body that stops early (IncompleteRead, raised out of
+ # json.load()) come from http.client itself, so they passed every handler above and ended the
+ # run in a traceback with exit 1 - the code this script keeps for "no race left this season".
+ # A captive portal, a proxy that filters the host and a link that drops all do this; the fetch
+ # is the one step here that depends on someone else's network.
+ return refuse(f"Jolpica closed the connection before the answer was complete ({e}) for "
+ f"{endpoint}. Nothing was written: this is the one step that goes online, so "
+ f"check the connection - a proxy, a captive portal or a filtered network is the "
+ f"usual cause - and run it again.")
except ValueError as e:
return refuse(f"Jolpica answered something that is not JSON ({e}) for {endpoint}. Nothing was "
f"written: this is usually a proxy or a login page answering instead of the API, "