Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Use ipdb instead of pdb for --pdb when installed (#8052)

Adriand24e5ae80729
diff --git a/pyproject.toml b/pyproject.toml
index 0c0a4bcdd..e3b14fd69 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -163,6 +163,7 @@ module = [
"brotli",
"brotlicffi",
"google.*",
+ "ipdb",
"pydispatch.*",
"pyftpdlib.*",
"pytest_twisted",
diff --git a/scrapy/commands/__init__.py b/scrapy/commands/__init__.py
index 00eb2455b..9d0f405d2 100644
--- a/scrapy/commands/__init__.py
+++ b/scrapy/commands/__init__.py
@@ -8,6 +8,7 @@ import argparse
import builtins
import logging
import os
+import sys
import warnings
from abc import ABC, abstractmethod
from pathlib import Path
@@ -122,7 +123,11 @@ class ScrapyCommand(ABC):
metavar="NAME=VALUE",
help="set/override setting (may be repeated)",
)
- group.add_argument("--pdb", action="store_true", help="enable pdb on failure")
+ group.add_argument(
+ "--pdb",
+ action="store_true",
+ help="enable pdb on failure (uses ipdb if installed)",
+ )
def process_options(self, args: list[str], opts: argparse.Namespace) -> None:
"""Set settings based on the command line options."""
@@ -151,6 +156,12 @@ class ScrapyCommand(ABC):
)
if opts.pdb:
+ try:
+ import ipdb # noqa: T100,PLC0415
+ except ImportError:
+ pass
+ else:
+ sys.modules["pdb"] = ipdb
failure.startDebugMode()
@abstractmethod
diff --git a/tests/test_commands.py b/tests/test_commands.py
index f20ecc153..08882fd52 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -2,11 +2,13 @@ from __future__ import annotations
import argparse
import json
+import pdb # noqa: T100
import sys
from pathlib import Path
-from typing import TYPE_CHECKING
+from typing import TYPE_CHECKING, Any
import pytest
+from twisted.python import failure
import scrapy
from scrapy.cmdline import _pop_command_name, execute
@@ -97,6 +99,15 @@ class TestCommandSettings:
assert isinstance(self.command.settings["FEEDS"], scrapy.settings.BaseSettings)
assert dict(self.command.settings["FEEDS"]) == json.loads(feeds_json)
+ def test_pdb_uses_ipdb_if_installed(self, monkeypatch):
+ monkeypatch.setattr(failure, "startDebugMode", lambda: None)
+ fake_ipdb: Any = argparse.Namespace(post_mortem=lambda tb: None)
+ monkeypatch.setitem(sys.modules, "pdb", pdb)
+ monkeypatch.setitem(sys.modules, "ipdb", fake_ipdb)
+ opts, args = self.parser.parse_known_args(args=["--pdb", "spider.py"])
+ self.command.process_options(args, opts)
+ assert sys.modules["pdb"] is fake_ipdb
+
def test_help_formatter(self):
formatter = ScrapyHelpFormatter(prog="scrapy")
part_strings = [