Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: support CPython 3.12, update CI jobs #2177

Merged
merged 10 commits into from
Nov 5, 2023
4 changes: 3 additions & 1 deletion falcon/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@

# PERF(kgriffs): Avoid superfluous namespace lookups
strptime: Callable[[str, str], datetime.datetime] = datetime.datetime.strptime
utcnow: Callable[[], datetime.datetime] = datetime.datetime.utcnow
utcnow: Callable[[], datetime.datetime] = functools.partial(
vytas7 marked this conversation as resolved.
Show resolved Hide resolved
vytas7 marked this conversation as resolved.
Show resolved Hide resolved
datetime.datetime.now, datetime.timezone.utc
)


# NOTE(kgriffs,vytas): This is tested in the PyPy gate but we do not want devs
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ filterwarnings = [
"ignore:inspect.getargspec\\(\\) is deprecated:DeprecationWarning",
"ignore:.cgi. is deprecated and slated for removal:DeprecationWarning",
"ignore:path is deprecated\\. Use files\\(\\) instead:DeprecationWarning",
"ignore:This process \\(.+\\) is multi-threaded",
"ignore:There is no current event loop",
]
testpaths = [
"tests"
Expand Down
14 changes: 9 additions & 5 deletions tests/test_cookies.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta, tzinfo
from datetime import datetime, timedelta, timezone, tzinfo
from http import cookies as http_cookies
import re

Expand Down Expand Up @@ -28,6 +28,10 @@ def dst(self, dt):
GMT_PLUS_ONE = TimezoneGMTPlus1()


def utcnow_naive():
return datetime.now(timezone.utc).replace(tzinfo=None)


class CookieResource:
def on_get(self, req, resp):
resp.set_cookie('foo', 'bar', domain='example.com', path='/')
Expand Down Expand Up @@ -171,7 +175,7 @@ def test_response_complex_case(client):
assert cookie.domain is None
assert cookie.same_site == 'Lax'

assert cookie.expires < datetime.utcnow()
assert cookie.expires < utcnow_naive()

# NOTE(kgriffs): I know accessing a private attr like this is
# naughty of me, but we just need to sanity-check that the
Expand All @@ -193,7 +197,7 @@ def test(cookie, path, domain, samesite='Lax'):
assert cookie.domain == domain
assert cookie.path == path
assert cookie.same_site == samesite
assert cookie.expires < datetime.utcnow()
assert cookie.expires < utcnow_naive()

test(result.cookies['foo'], path=None, domain=None)
test(result.cookies['bar'], path='/bar', domain=None)
Expand Down Expand Up @@ -231,7 +235,7 @@ def test_set(cookie, value, samesite=None):
def test_unset(cookie, samesite='Lax'):
assert cookie.value == '' # An unset cookie has an empty value
assert cookie.same_site == samesite
assert cookie.expires < datetime.utcnow()
assert cookie.expires < utcnow_naive()

test_unset(result_unset.cookies['foo'], samesite='Strict')
# default: bar is unset with no samesite param, so should go to Lax
Expand Down Expand Up @@ -325,7 +329,7 @@ def test_response_unset_cookie(client):
assert match

expiration = http_date_to_dt(match.group(1), obs_date=True)
assert expiration < datetime.utcnow()
assert expiration < utcnow_naive()


def test_cookie_timezone(client):
Expand Down
4 changes: 3 additions & 1 deletion tests/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import falcon
from falcon import testing
from falcon.util.deprecation import DeprecatedWarning
from falcon.util.misc import utcnow

from _util import create_app # NOQA


Expand All @@ -31,7 +33,7 @@ def __init__(self, last_modified=None):
if last_modified is not None:
self.last_modified = last_modified
else:
self.last_modified = datetime.utcnow()
self.last_modified = utcnow()

def _overwrite_headers(self, req, resp):
resp.content_type = 'x-falcon/peregrine'
Expand Down
7 changes: 4 additions & 3 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import falcon
import falcon.errors
import falcon.testing as testing
from falcon.util.misc import utcnow

from _util import create_app # NOQA

Expand All @@ -36,15 +37,15 @@ def process_request(self, req, resp):
class RequestTimeMiddleware:
def process_request(self, req, resp):
global context
context['start_time'] = datetime.utcnow()
context['start_time'] = utcnow()

def process_resource(self, req, resp, resource, params):
global context
context['mid_time'] = datetime.utcnow()
context['mid_time'] = utcnow()

def process_response(self, req, resp, resource, req_succeeded):
global context
context['end_time'] = datetime.utcnow()
context['end_time'] = utcnow()
context['req_succeeded'] = req_succeeded

async def process_request_async(self, req, resp):
Expand Down
9 changes: 4 additions & 5 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

from datetime import datetime
from datetime import datetime, timezone
import functools
import http
import itertools
Expand Down Expand Up @@ -109,13 +109,12 @@ def old_thing():
assert msg in str(warn.message)

def test_http_now(self):
expected = datetime.utcnow()
expected = datetime.now(timezone.utc)
actual = falcon.http_date_to_dt(falcon.http_now())

delta = actual - expected
delta_sec = abs(delta.days * 86400 + delta.seconds)
delta = actual.replace(tzinfo=timezone.utc) - expected

assert delta_sec <= 1
assert delta.total_seconds() <= 1

def test_dt_to_http(self):
assert (
Expand Down
Loading