Skip to content

Commit d230111

Browse files
authored
Use contextlib.nullcontext on Python 3.7+ (#633)
1 parent c146ae7 commit d230111

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

src/django_mysql/compat.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import sys
2+
3+
if sys.version_info >= (3, 7):
4+
from contextlib import nullcontext
5+
else:
6+
7+
class nullcontext:
8+
def __enter__(self):
9+
pass
10+
11+
def __exit__(self, *exc_info):
12+
pass

src/django_mysql/models/query.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from django.utils.functional import cached_property
1414
from django.utils.translation import gettext as _
1515

16+
from django_mysql.compat import nullcontext
1617
from django_mysql.models.handler import Handler
1718
from django_mysql.rewrite_query import REWRITE_MARKER
1819
from django_mysql.status import GlobalStatus
@@ -21,7 +22,6 @@
2122
WeightedAverageRate,
2223
format_duration,
2324
have_program,
24-
noop_context,
2525
settings_to_cmd_args,
2626
)
2727

@@ -305,11 +305,11 @@ def __init__(
305305
self.queryset = self.sanitize_queryset(queryset)
306306

307307
if atomically:
308-
self.maybe_atomic = atomic
308+
self.maybe_atomic = atomic(using=self.queryset.db)
309309
else:
310310
# Work around for `with` statement not supporting variable number
311311
# of contexts
312-
self.maybe_atomic = noop_context
312+
self.maybe_atomic = nullcontext()
313313

314314
self.status_thresholds = status_thresholds
315315
self.pk_range = pk_range
@@ -334,8 +334,7 @@ def __iter__(self):
334334
comp = operator.ge # >=
335335
direction = -1
336336
current_pk = first_pk
337-
db_alias = self.queryset.db
338-
status = GlobalStatus(db_alias)
337+
status = GlobalStatus(self.queryset.db)
339338

340339
self.init_progress(direction)
341340

@@ -350,7 +349,7 @@ def __iter__(self):
350349
else:
351350
end_pk = max(current_pk, last_pk - 1)
352351

353-
with StopWatch() as timer, self.maybe_atomic(using=db_alias):
352+
with StopWatch() as timer, self.maybe_atomic:
354353
if direction == 1:
355354
chunk = self.queryset.filter(pk__gte=start_pk, pk__lt=end_pk)
356355
else:

src/django_mysql/utils.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import subprocess
33
import time
44
from collections import defaultdict
5-
from contextlib import contextmanager
65
from queue import Empty, Queue
76
from threading import Lock, Thread
87
from weakref import WeakKeyDictionary
@@ -74,11 +73,6 @@ def __exit__(self, *args, **kwargs):
7473
self.total_time = self.end_time - self.start_time
7574

7675

77-
@contextmanager
78-
def noop_context(*args, **kwargs):
79-
yield
80-
81-
8276
def format_duration(total_seconds):
8377
hours = total_seconds // 3600
8478
minutes = (total_seconds % 3600) // 60

0 commit comments

Comments
 (0)