Skip to content

Commit b1b2e95

Browse files
authored
clean up some imports (#1472)
* Remove unneeded import attempts from "tornado4". From what I can tell, this was only ever useful when using system packages on ubuntu 20.04. We don't support installation using system packages anymore (and ubuntu 20.04 is EOL). * Remove misleading comment about monkey-patching the DB connection string in the test suite. We don't monkey-patch it anymore (instead we assume someone else fixed it and assert that it's correct).
1 parent 4e485d8 commit b1b2e95

40 files changed

+108
-183
lines changed

cms/io/web_service.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,7 @@
2828
# Monkey-patch: Tornado 4.5.3 does not work on Python 3.11 by default
2929
collections.MutableMapping = collections.abc.MutableMapping
3030

31-
try:
32-
import tornado4.wsgi as tornado_wsgi
33-
except ImportError:
34-
import tornado.wsgi as tornado_wsgi
31+
import tornado.wsgi
3532
from gevent.pywsgi import WSGIServer
3633
from werkzeug.contrib.fixers import ProxyFix
3734
from werkzeug.middleware.dispatcher import DispatcherMiddleware
@@ -71,7 +68,7 @@ def __init__(
7168
is_proxy_used = parameters.pop('is_proxy_used', None)
7269
num_proxies_used = parameters.pop('num_proxies_used', None)
7370

74-
self.wsgi_app = tornado_wsgi.WSGIApplication(handlers, **parameters)
71+
self.wsgi_app = tornado.wsgi.WSGIApplication(handlers, **parameters)
7572
self.wsgi_app.service = self
7673

7774
for entry in static_files:

cms/server/admin/handlers/base.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,7 @@
4646
# Monkey-patch: Tornado 4.5.3 does not work on Python 3.11 by default
4747
collections.MutableMapping = collections.abc.MutableMapping
4848

49-
try:
50-
import tornado4.web as tornado_web
51-
except ImportError:
52-
import tornado.web as tornado_web
49+
import tornado.web
5350
from sqlalchemy.exc import IntegrityError
5451
from sqlalchemy.orm import Query, subqueryload
5552

@@ -81,7 +78,7 @@ def argument_reader(func: Callable[[str], typing.Any], empty: object = None):
8178
"""
8279

8380
def helper(
84-
self: tornado_web.RequestHandler, dest: dict, name: str, empty: object = empty
81+
self: tornado.web.RequestHandler, dest: dict, name: str, empty: object = empty
8582
):
8683
"""Read the argument called "name" and save it in "dest".
8784
@@ -194,7 +191,7 @@ def decorator(
194191
195192
"""
196193
@wraps(func)
197-
@tornado_web.authenticated
194+
@tornado.web.authenticated
198195
def newfunc(self: _T, *args: _P.args, **kwargs: _P.kwargs):
199196
"""Check if the permission is present before calling the function.
200197
@@ -213,7 +210,7 @@ def newfunc(self: _T, *args: _P.args, **kwargs: _P.kwargs):
213210
# the current user id.
214211
return func(self, *args, **kwargs)
215212
else:
216-
raise tornado_web.HTTPError(403, "Admin is not authorized")
213+
raise tornado.web.HTTPError(403, "Admin is not authorized")
217214

218215
return newfunc
219216

@@ -302,7 +299,7 @@ def safe_get_item(
302299
session = self.sql_session
303300
entity = cls.get_from_id(ident, session)
304301
if entity is None:
305-
raise tornado_web.HTTPError(404)
302+
raise tornado.web.HTTPError(404)
306303
return entity
307304

308305
def prepare(self):
@@ -354,7 +351,7 @@ def render_params(self) -> dict:
354351

355352
def write_error(self, status_code, **kwargs):
356353
if "exc_info" in kwargs and \
357-
kwargs["exc_info"][0] != tornado_web.HTTPError:
354+
kwargs["exc_info"][0] != tornado.web.HTTPError:
358355
exc_info = kwargs["exc_info"]
359356
logger.error(
360357
"Uncaught exception (%r) while processing a request: %s",

cms/server/admin/handlers/contestannouncement.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@
3333
# Monkey-patch: Tornado 4.5.3 does not work on Python 3.11 by default
3434
collections.MutableMapping = collections.abc.MutableMapping
3535

36-
try:
37-
import tornado4.web as tornado_web
38-
except ImportError:
39-
import tornado.web as tornado_web
36+
import tornado.web
4037

4138
from cms.db import Contest, Announcement
4239
from cmscommon.datetime import make_datetime
@@ -77,7 +74,7 @@ def delete(self, contest_id: str, ann_id: str):
7774

7875
# Protect against URLs providing incompatible parameters.
7976
if self.contest is not ann.contest:
80-
raise tornado_web.HTTPError(404)
77+
raise tornado.web.HTTPError(404)
8178

8279
self.sql_session.delete(ann)
8380
self.try_commit()

cms/server/admin/handlers/contestquestion.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@
3535
# Monkey-patch: Tornado 4.5.3 does not work on Python 3.11 by default
3636
collections.MutableMapping = collections.abc.MutableMapping
3737

38-
try:
39-
import tornado4.web as tornado_web
40-
except ImportError:
41-
import tornado.web as tornado_web
38+
import tornado.web
4239

4340
from cms.db import Contest, Question, Participation
4441
from cmscommon.datetime import make_datetime
@@ -88,7 +85,7 @@ def post(self, contest_id, question_id):
8885

8986
# Protect against URLs providing incompatible parameters.
9087
if self.contest is not question.participation.contest:
91-
raise tornado_web.HTTPError(404)
88+
raise tornado.web.HTTPError(404)
9289

9390
self.process_question(question)
9491
self.redirect(ref)
@@ -148,7 +145,7 @@ class QuestionClaimHandler(QuestionActionHandler):
148145
def process_question(self, question):
149146
# Can claim/unclaim only a question not ignored or answered.
150147
if question.ignored or question.reply_timestamp is not None:
151-
raise tornado_web.HTTPError(405)
148+
raise tornado.web.HTTPError(405)
152149

153150
should_claim = self.get_argument("claim", "no") == "yes"
154151

cms/server/admin/handlers/contestuser.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@
3838
# Monkey-patch: Tornado 4.5.3 does not work on Python 3.11 by default
3939
collections.MutableMapping = collections.abc.MutableMapping
4040

41-
try:
42-
import tornado4.web as tornado_web
43-
except ImportError:
44-
import tornado.web as tornado_web
41+
import tornado.web
4542

4643
from cms.db import Contest, Message, Participation, Submission, User, Team
4744
from cmscommon.datetime import make_datetime
@@ -113,7 +110,7 @@ def get(self, contest_id, user_id):
113110
)
114111
# Check that the participation is valid.
115112
if participation is None:
116-
raise tornado_web.HTTPError(404)
113+
raise tornado.web.HTTPError(404)
117114

118115
submission_query = self.sql_session.query(Submission)\
119116
.filter(Submission.participation == participation)
@@ -193,7 +190,7 @@ def get(self, contest_id, user_id):
193190

194191
# Check that the participation is valid.
195192
if participation is None:
196-
raise tornado_web.HTTPError(404)
193+
raise tornado.web.HTTPError(404)
197194

198195
submission_query = self.sql_session.query(Submission)\
199196
.filter(Submission.participation == participation)
@@ -220,7 +217,7 @@ def post(self, contest_id, user_id):
220217

221218
# Check that the participation is valid.
222219
if participation is None:
223-
raise tornado_web.HTTPError(404)
220+
raise tornado.web.HTTPError(404)
224221

225222
try:
226223
attrs = participation.get_attrs()
@@ -281,7 +278,7 @@ def post(self, contest_id, user_id):
281278

282279
# check that the participation is valid
283280
if participation is None:
284-
raise tornado_web.HTTPError(404)
281+
raise tornado.web.HTTPError(404)
285282

286283
message = Message(make_datetime(),
287284
self.get_argument("message_subject", ""),

cms/server/admin/handlers/dataset.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@
3939
# Monkey-patch: Tornado 4.5.3 does not work on Python 3.11 by default
4040
collections.MutableMapping = collections.abc.MutableMapping
4141

42-
try:
43-
import tornado4.web as tornado_web
44-
except ImportError:
45-
import tornado.web as tornado_web
42+
import tornado.web
4643

4744
from cms.db import Dataset, Manager, Message, Participation, \
4845
Session, Submission, Task, Testcase
@@ -102,7 +99,7 @@ def get(self, dataset_id_to_copy):
10299
self.safe_get_item(Dataset, dataset_id_to_copy)
103100
description = "Copy of %s" % original_dataset.description
104101
except ValueError:
105-
raise tornado_web.HTTPError(404)
102+
raise tornado.web.HTTPError(404)
106103

107104
self.r_params = self.render_params()
108105
self.r_params["task"] = task
@@ -125,7 +122,7 @@ def post(self, dataset_id_to_copy):
125122
original_dataset = \
126123
self.safe_get_item(Dataset, dataset_id_to_copy)
127124
except ValueError:
128-
raise tornado_web.HTTPError(404)
125+
raise tornado.web.HTTPError(404)
129126

130127
try:
131128
attrs = dict()
@@ -406,7 +403,7 @@ def delete(self, dataset_id, manager_id):
406403

407404
# Protect against URLs providing incompatible parameters.
408405
if manager.dataset is not dataset:
409-
raise tornado_web.HTTPError(404)
406+
raise tornado.web.HTTPError(404)
410407

411408
task_id = dataset.task_id
412409

@@ -562,7 +559,7 @@ def delete(self, dataset_id, testcase_id):
562559

563560
# Protect against URLs providing incompatible parameters.
564561
if dataset is not testcase.dataset:
565-
raise tornado_web.HTTPError(404)
562+
raise tornado.web.HTTPError(404)
566563

567564
task_id = testcase.dataset.task_id
568565

cms/server/admin/handlers/task.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@
3636
# Monkey-patch: Tornado 4.5.3 does not work on Python 3.11 by default
3737
collections.MutableMapping = collections.abc.MutableMapping
3838

39-
try:
40-
import tornado4.web as tornado_web
41-
except ImportError:
42-
import tornado.web as tornado_web
39+
import tornado.web
4340

4441
from cms.db import Attachment, Dataset, Session, Statement, Submission, Task
4542
from cmscommon.datetime import make_datetime
@@ -296,7 +293,7 @@ def delete(self, task_id, statement_id):
296293

297294
# Protect against URLs providing incompatible parameters.
298295
if task is not statement.task:
299-
raise tornado_web.HTTPError(404)
296+
raise tornado.web.HTTPError(404)
300297

301298
self.sql_session.delete(statement)
302299
self.try_commit()
@@ -368,7 +365,7 @@ def delete(self, task_id, attachment_id):
368365

369366
# Protect against URLs providing incompatible parameters.
370367
if attachment.task is not task:
371-
raise tornado_web.HTTPError(404)
368+
raise tornado.web.HTTPError(404)
372369

373370
self.sql_session.delete(attachment)
374371
self.try_commit()

cms/server/contest/handlers/base.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@
4242
# Monkey-patch: Tornado 4.5.3 does not work on Python 3.11 by default
4343
collections.MutableMapping = collections.abc.MutableMapping
4444

45-
try:
46-
import tornado4.web as tornado_web
47-
except ImportError:
48-
import tornado.web as tornado_web
45+
import tornado.web
4946
from werkzeug.datastructures import LanguageAccept
5047
from werkzeug.http import parse_accept_header
5148

@@ -159,7 +156,7 @@ def render_params(self) -> dict:
159156

160157
def write_error(self, status_code, **kwargs):
161158
if "exc_info" in kwargs and \
162-
kwargs["exc_info"][0] != tornado_web.HTTPError:
159+
kwargs["exc_info"][0] != tornado.web.HTTPError:
163160
exc_info = kwargs["exc_info"]
164161
logger.error(
165162
"Uncaught exception (%r) while processing a request: %s",

cms/server/contest/handlers/communication.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@
3636
# Monkey-patch: Tornado 4.5.3 does not work on Python 3.11 by default
3737
collections.MutableMapping = collections.abc.MutableMapping
3838

39-
try:
40-
import tornado4.web as tornado_web
41-
except ImportError:
42-
import tornado.web as tornado_web
39+
import tornado.web
4340

4441
from cms.server import multi_contest
4542
from cms.server.contest.communication import accept_question, \
@@ -60,7 +57,7 @@ class CommunicationHandler(ContestHandler):
6057
and the contest managers..
6158
6259
"""
63-
@tornado_web.authenticated
60+
@tornado.web.authenticated
6461
@multi_contest
6562
def get(self):
6663
self.render("communication.html", **self.r_params)
@@ -70,7 +67,7 @@ class QuestionHandler(ContestHandler):
7067
"""Called when the user submits a question.
7168
7269
"""
73-
@tornado_web.authenticated
70+
@tornado.web.authenticated
7471
@multi_contest
7572
def post(self):
7673
try:
@@ -79,7 +76,7 @@ def post(self):
7976
self.get_argument("question_text", ""))
8077
self.sql_session.commit()
8178
except QuestionsNotAllowed:
82-
raise tornado_web.HTTPError(404)
79+
raise tornado.web.HTTPError(404)
8380
except UnacceptableQuestion as e:
8481
self.notify_error(e.subject, e.text, e.text_params)
8582
else:

cms/server/contest/handlers/contest.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@
4444
# Monkey-patch: Tornado 4.5.3 does not work on Python 3.11 by default
4545
collections.MutableMapping = collections.abc.MutableMapping
4646

47-
try:
48-
import tornado4.web as tornado_web
49-
except ImportError:
50-
import tornado.web as tornado_web
47+
import tornado.web
5148

5249
from cms import config, TOKEN_MODE_MIXED
5350
from cms.db import Contest, Submission, Task, UserTest, contest
@@ -126,7 +123,7 @@ def choose_contest(self):
126123
# the one from the base class is enough to display a 404 page.
127124
super().prepare()
128125
self.r_params = super().render_params()
129-
raise tornado_web.HTTPError(404)
126+
raise tornado.web.HTTPError(404)
130127
else:
131128
# Select the contest specified on the command line
132129
self.contest = Contest.get_from_id(
@@ -164,7 +161,7 @@ def get_current_user(self) -> Participation | None:
164161
authorization_header = self.request.headers.get(
165162
"X-CMS-Authorization", None)
166163
if authorization_header is not None:
167-
authorization_header = tornado_web.decode_signed_value(self.application.settings["cookie_secret"],
164+
authorization_header = tornado.web.decode_signed_value(self.application.settings["cookie_secret"],
168165
cookie_name, authorization_header)
169166

170167
try:

0 commit comments

Comments
 (0)