Skip to content

Commit 57e3fe1

Browse files
andy-slacionelmc
authored andcommitted
Improve handling of ResourceWarning from sqlite3.
The patch allows suppressing ResourceWarnings from sqlite3 by specifying filter in command line with -W option.
1 parent f6d4ccf commit 57e3fe1

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

CHANGELOG.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22
Changelog
33
=========
44

5+
* Improve handling of ResourceWarning from sqlite3.
6+
7+
The plugin adds warning filter for sqlite3 ``ResourceWarning`` unclosed database (since 6.2.0).
8+
It checks if there is already existing plugin for this message by comparing filter regular expression.
9+
When filter is specified on command line the message is escaped and does not match an expected message.
10+
A check for an escaped regular expression is added to handle this case.
11+
12+
With this fix one can suppress ``ResourceWarning`` from sqlite3 from command line::
13+
14+
pytest -W "ignore:unclosed database in <sqlite3.Connection object at:ResourceWarning" ...
15+
516
7.0.0 (2025-09-09)
617
------------------
718

src/pytest_cov/plugin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
if TYPE_CHECKING:
1818
from .engine import CovController
1919

20+
# The message is unescaped if it comes from configuration file, and escaped if
21+
# it comes from command line option (-W) or PYTHONWARNINGS envvar.
2022
COVERAGE_SQLITE_WARNING_RE = re.compile('unclosed database in <sqlite3.Connection object at', re.I)
23+
COVERAGE_SQLITE_WARNING_RE2 = re.compile('unclosed\\ database\\ in\\ <sqlite3\\.Connection\\ object\\ at', re.I)
2124

2225

2326
def validate_report(arg):
@@ -325,7 +328,7 @@ def pytest_runtestloop(self, session):
325328

326329
# we add default warning configuration to prevent certain warnings to bubble up as errors due to rigid filterwarnings configuration
327330
for _, message, category, _, _ in warnings.filters:
328-
if category is ResourceWarning and message == COVERAGE_SQLITE_WARNING_RE:
331+
if category is ResourceWarning and message in (COVERAGE_SQLITE_WARNING_RE, COVERAGE_SQLITE_WARNING_RE2):
329332
break
330333
else:
331334
warnings.filterwarnings('default', 'unclosed database in <sqlite3.Connection object at', ResourceWarning)

0 commit comments

Comments
 (0)