Skip to content

Commit

Permalink
Merge pull request #350 from getlogbook/feature/python-3.12
Browse files Browse the repository at this point in the history
Test on Python 3.12
  • Loading branch information
RazerM authored Sep 17, 2023
2 parents f031bd9 + 4eafa1a commit a4b62fa
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 24 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- uses: "actions/checkout@v3"
- uses: "actions/setup-python@v4"
with:
python-version: "${{ matrix.python-version }}"
cache: pip
allow-prereleases: true
- name: "Install dependencies"
run: |
set -xe
Expand All @@ -53,7 +54,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
arch: ["x86", "x64"]

env:
Expand All @@ -66,6 +67,7 @@ jobs:
python-version: "${{ matrix.python-version }}"
architecture: "${{ matrix.arch }}"
cache: pip
allow-prereleases: true

- run: python -VV
- run: python -m site
Expand All @@ -82,7 +84,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- uses: "actions/checkout@v3"
Expand All @@ -91,6 +93,7 @@ jobs:
python-version: "${{ matrix.python-version }}"
architecture: "${{ matrix.arch }}"
cache: pip
allow-prereleases: true
- name: "Install dependencies"
run: |
set -xe
Expand Down
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Unreleased
- Dropped support for Python 3.7
- Passing (keyfile, certfile) to MailHandler's `secure` argument is deprecated
in favour of passing an ssl.SSLContext.
- Python 3.12 support

Version 1.6.0
-------------
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ classifiers = [
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]
requires-python = ">=3.8"
dynamic = ["version"]
Expand Down
11 changes: 8 additions & 3 deletions src/logbook/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
from weakref import ref as weakref

from logbook.concurrency import greenlet_get_ident, thread_get_ident, thread_get_name
from logbook.helpers import cached_property, parse_iso8601, to_safe_json
from logbook.helpers import (
cached_property,
datetime_utcnow,
parse_iso8601,
to_safe_json,
)

_has_speedups = False
try:
Expand All @@ -39,7 +44,7 @@
group_reflected_property,
)

_datetime_factory = datetime.utcnow
_datetime_factory = datetime_utcnow


def set_datetime_format(datetime_format):
Expand Down Expand Up @@ -94,7 +99,7 @@ def utc_tz():
"""
global _datetime_factory
if datetime_format == "utc":
_datetime_factory = datetime.utcnow
_datetime_factory = datetime_utcnow
elif datetime_format == "local":
_datetime_factory = datetime.now
elif callable(datetime_format):
Expand Down
4 changes: 2 additions & 2 deletions src/logbook/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
lookup_level,
)
from logbook.concurrency import new_fine_grained_lock
from logbook.helpers import rename
from logbook.helpers import datetime_utcnow, rename

DEFAULT_FORMAT_STRING = (
"[{record.time:%Y-%m-%d %H:%M:%S.%f%z}] "
Expand Down Expand Up @@ -506,7 +506,7 @@ def check_delivery(self, record):
try:
allow_delivery = None
suppression_count = old_count = 0
first_count = now = datetime.utcnow()
first_count = now = datetime_utcnow()

if hash in self._record_limits:
last_count, suppression_count = self._record_limits[hash]
Expand Down
17 changes: 15 additions & 2 deletions src/logbook/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import re
import sys
import time
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone

# this regexp also matches incompatible dates like 20070101 because
# some libraries (like the python xmlrpclib modules) use this
Expand Down Expand Up @@ -144,10 +144,23 @@ def _convert(obj):
return _convert(data)


if sys.version_info >= (3, 12):

def datetime_utcnow():
"""datetime.utcnow() but doesn't emit a deprecation warning.
Will be fixed by https://github.com/getlogbook/logbook/issues/353
"""
return datetime.now(timezone.utc).replace(tzinfo=None)

else:
datetime_utcnow = datetime.utcnow


def format_iso8601(d=None):
"""Returns a date in iso8601 format."""
if d is None:
d = datetime.utcnow()
d = datetime_utcnow()
rv = d.strftime("%Y-%m-%dT%H:%M:%S")
if d.microsecond:
rv += "." + str(d.microsecond)
Expand Down
7 changes: 4 additions & 3 deletions tests/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ async def task(handler, msg):

await asyncio.sleep(0) # allow for context switch

asyncio.get_event_loop().run_until_complete(
asyncio.gather(task(h1, "task1"), task(h2, "task2"))
)
async def main():
await asyncio.gather(task(h1, "task1"), task(h2, "task2"))

asyncio.run(main())

assert len(h1.records) == ITERATIONS
assert all(["task1" == r.msg for r in h1.records])
Expand Down
17 changes: 7 additions & 10 deletions tests/test_logging_compat.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import functools
import re
import warnings
from io import StringIO
from random import randrange

Expand Down Expand Up @@ -77,16 +79,11 @@ def test_warning_redirections():
from logbook.compat import redirected_warnings

with logbook.TestHandler() as handler:
redirector = redirected_warnings()
redirector.start()
try:
from warnings import resetwarnings, warn
with redirected_warnings():
warnings.warn(
RuntimeWarning(f"Testing {next(test_warning_redirections_i)}")
)

resetwarnings()
warn(RuntimeWarning("Testing" + str(next(test_warning_redirections_i))))
finally:
redirector.end()

assert len(handler.records) == 1
assert len(handler.formatted_records) == 1
assert handler.formatted_records[0].startswith("[WARNING] RuntimeWarning: Testing")
assert __file_without_pyc__ in handler.records[0].filename
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py{38,39,310,311}{,-nospeedups},pypy,docs
envlist = py{38,39,310,311,312}{,-nospeedups},pypy,docs

[testenv]
extras =
Expand Down Expand Up @@ -31,3 +31,4 @@ python =
3.9: py39
3.10: py310
3.11: py311, docs
3.12: py312

0 comments on commit a4b62fa

Please sign in to comment.