Claude commited on
Commit
21042c7
·
unverified ·
1 Parent(s): 18225f3

Fix bare except and misattributed logger (item 3 from review)

Browse files

- serp.py: query_google_patents's per-item extraction caught bare `except:`,
which also swallows asyncio.CancelledError (BaseException in 3.8+),
silently absorbing task cancellation instead of letting it propagate.
Narrowed to `except Exception:`.
- utils.py: `from asyncio.log import logger` reused asyncio's internal
logger, so every warning here was misattributed to the 'asyncio' logging
namespace instead of the app's own. Now uses logging.getLogger(__name__).
- Updated the characterization test that had pinned the old logger name to
assert the fixed one instead.

Files changed (3) hide show
  1. serp.py +1 -1
  2. tests/test_utils.py +5 -8
  3. utils.py +4 -1
serp.py CHANGED
@@ -155,7 +155,7 @@ async def query_google_patents(browser: Browser, q: str, n_results: int = 10):
155
  try:
156
  title = await item.locator("h3, h4").first.inner_text(timeout=1000)
157
  body = await item.locator("div.abstract, div.result-snippet, .snippet, .result-text").first.inner_text(timeout=1000)
158
- except:
159
  continue # If we can't get title or body, skip this item
160
 
161
  results.append({
 
155
  try:
156
  title = await item.locator("h3, h4").first.inner_text(timeout=1000)
157
  body = await item.locator("div.abstract, div.result-snippet, .snippet, .result-text").first.inner_text(timeout=1000)
158
+ except Exception:
159
  continue # If we can't get title or body, skip this item
160
 
161
  results.append({
tests/test_utils.py CHANGED
@@ -28,16 +28,13 @@ def test_logs_nothing_when_all_results_succeeded(caplog):
28
  assert [r for r in caplog.records if r.levelno == logging.WARNING] == []
29
 
30
 
31
- def test_uses_the_asyncio_logger_not_an_app_logger(caplog):
32
- """Characterizes a real bug (flagged in review): `utils.py` imports
33
- `logger` from `asyncio.log` instead of using `logging.getLogger(__name__)`,
34
- so these warnings are misattributed to the 'asyncio' logging namespace
35
- instead of the app's own. Pinned here so the fix (giving `utils.py` its
36
- own logger) shows up as an intentional assertion change, not a silent
37
- behavior drift.
38
  """
39
  params = SerpQuery(queries=["q1"])
40
  with caplog.at_level(logging.WARNING):
41
  log_gathered_exceptions([ValueError("boom")], "ctx", params)
42
 
43
- assert caplog.records[0].name == "asyncio"
 
28
  assert [r for r in caplog.records if r.levelno == logging.WARNING] == []
29
 
30
 
31
+ def test_uses_its_own_logger_not_the_asyncio_one(caplog):
32
+ """Was `from asyncio.log import logger` (flagged in review), which
33
+ misattributed these warnings to the 'asyncio' logging namespace instead
34
+ of the app's own. Fixed to `logging.getLogger(__name__)`.
 
 
 
35
  """
36
  params = SerpQuery(queries=["q1"])
37
  with caplog.at_level(logging.WARNING):
38
  log_gathered_exceptions([ValueError("boom")], "ctx", params)
39
 
40
+ assert caplog.records[0].name == "utils"
utils.py CHANGED
@@ -1,6 +1,9 @@
1
- from asyncio.log import logger
 
2
  from serp import SerpQuery
3
 
 
 
4
 
5
  def log_gathered_exceptions(results: list, context: str, params: SerpQuery):
6
  """Logs gathered exceptions with context and parameters."""
 
1
+ import logging
2
+
3
  from serp import SerpQuery
4
 
5
+ logger = logging.getLogger(__name__)
6
+
7
 
8
  def log_gathered_exceptions(results: list, context: str, params: SerpQuery):
9
  """Logs gathered exceptions with context and parameters."""