Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
upgrade
  • Loading branch information
trim21 committed Sep 11, 2024
commit 245b139b9075545844255d5d1604447f2185d8c3
27 changes: 0 additions & 27 deletions loguru/_asyncio_loop.py

This file was deleted.

15 changes: 0 additions & 15 deletions loguru/_contextvars.py

This file was deleted.

6 changes: 3 additions & 3 deletions loguru/_file_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ def generate_rename_path(root, ext, creation_time):
creation_datetime = datetime.datetime.fromtimestamp(creation_time)
date = FileDateFormatter(creation_datetime)

renamed_path = "{}.{}{}".format(root, date, ext)
renamed_path = f"{root}.{date}{ext}"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is still valid, even in Python 3.13

May I ask what the scope of your PR is?
Are you simply trying to drop support for Python 3.5 and 3.6, or are you also resolving deprecated code and taking advantage of new syntax?

@trim21 trim21 Nov 27, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This diff is generated by pyupgrade

I'm tring to resolving deprecated code and taking advantage of new syntax. and try add mypyc or cython on it to make it faster. but py35 is just too old to do that.

counter = 1

while os.path.exists(renamed_path):
counter += 1
renamed_path = "{}.{}.{}{}".format(root, date, counter, ext)
renamed_path = f"{root}.{date}.{counter}{ext}"

return renamed_path

Expand Down Expand Up @@ -56,7 +56,7 @@ def copy_compress(path_in, path_out, opener, **kwargs):

@staticmethod
def compression(path_in, ext, compress_function):
path_out = "{}{}".format(path_in, ext)
path_out = f"{path_in}{ext}"

if os.path.exists(path_out):
creation_time = get_ctime(path_out)
Expand Down
20 changes: 11 additions & 9 deletions loguru/_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,19 @@
import re
import sys
import warnings
from collections import namedtuple
from asyncio import get_running_loop
from contextvars import ContextVar
from inspect import isclass, iscoroutinefunction, isgeneratorfunction
from multiprocessing import current_process, get_context
from multiprocessing.context import BaseContext
from os import PathLike
from os.path import basename, splitext
from threading import current_thread
from typing import NamedTuple

from . import _asyncio_loop, _colorama, _defaults, _filters
from . import _colorama, _defaults, _filters
from ._better_exceptions import ExceptionFormatter
from ._colorizer import Colorizer
from ._contextvars import ContextVar
from ._datetime import aware_now
from ._error_interceptor import ErrorInterceptor
from ._file_sink import FileSink
Expand All @@ -110,13 +112,13 @@
from ._recattrs import RecordException, RecordFile, RecordLevel, RecordProcess, RecordThread
from ._simple_sinks import AsyncSink, CallableSink, StandardSink, StreamSink

if sys.version_info >= (3, 6):
from os import PathLike
else:
from pathlib import PurePath as PathLike

class Level(NamedTuple):
name: str
no: int
color: str
icon: str

Level = namedtuple("Level", ["name", "no", "color", "icon"])

start_time = aware_now()

Expand Down Expand Up @@ -840,7 +842,7 @@ def add(
# running loop in Python 3.5.2 and earlier versions, see python/asyncio#452.
if enqueue and loop is None:
try:
loop = _asyncio_loop.get_running_loop()
loop = get_running_loop()
except RuntimeError as e:
raise ValueError(
"An event loop is required to add a coroutine sink with `enqueue=True`, "
Expand Down
12 changes: 7 additions & 5 deletions loguru/_recattrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __init__(self, name, no, icon):
self.icon = icon

def __repr__(self):
return "(name=%r, no=%r, icon=%r)" % (self.name, self.no, self.icon)
return f"(name={self.name!r}, no={self.no!r}, icon={self.icon!r})"

def __format__(self, spec):
return self.name.__format__(spec)
Expand All @@ -25,7 +25,7 @@ def __init__(self, name, path):
self.path = path

def __repr__(self):
return "(name=%r, path=%r)" % (self.name, self.path)
return f"(name={self.name!r}, path={self.path!r})"

def __format__(self, spec):
return self.name.__format__(spec)
Expand All @@ -39,7 +39,7 @@ def __init__(self, id_, name):
self.name = name

def __repr__(self):
return "(id=%r, name=%r)" % (self.id, self.name)
return f"(id={self.id!r}, name={self.name!r})"

def __format__(self, spec):
return self.id.__format__(spec)
Expand All @@ -53,15 +53,17 @@ def __init__(self, id_, name):
self.name = name

def __repr__(self):
return "(id=%r, name=%r)" % (self.id, self.name)
return f"(id={self.id!r}, name={self.name!r})"

def __format__(self, spec):
return self.id.__format__(spec)


class RecordException(namedtuple("RecordException", ("type", "value", "traceback"))):
def __repr__(self):
return "(type=%r, value=%r, traceback=%r)" % (self.type, self.value, self.traceback)
return "(type={!r}, value={!r}, traceback={!r})".format(
self.type, self.value, self.traceback
)

def __reduce__(self):
# The traceback is not picklable, therefore it needs to be removed. Additionally, there's a
Expand Down
5 changes: 2 additions & 3 deletions loguru/_simple_sinks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import asyncio
import logging
import weakref

from ._asyncio_loop import get_running_loop, get_task_loop
from asyncio import get_running_loop


class StreamSink:
Expand Down Expand Up @@ -97,7 +96,7 @@ def tasks_to_complete(self):

async def _complete_task(self, task):
loop = get_running_loop()
if get_task_loop(task) is not loop:
if task.get_loop() is not loop:
return
try:
await task
Expand Down
29 changes: 0 additions & 29 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import io
import logging
import os
import pathlib
import sys
import threading
import time
Expand All @@ -18,34 +17,6 @@

import loguru

if sys.version_info < (3, 5, 3):

def run(coro):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
res = loop.run_until_complete(coro)
loop.close()
asyncio.set_event_loop(None)
return res

asyncio.run = run
elif sys.version_info < (3, 7):

def run(coro):
loop = asyncio.new_event_loop()
res = loop.run_until_complete(coro)
loop.close()
asyncio.set_event_loop(None)
return res

asyncio.run = run

if sys.version_info < (3, 6):

@pytest.fixture
def tmp_path(tmp_path):
yield pathlib.Path(str(tmp_path))


@contextlib.contextmanager
def new_event_loop_context():
Expand Down
12 changes: 0 additions & 12 deletions tests/test_contextualize.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import asyncio
import sys
import threading
from unittest.mock import MagicMock

import pytest

from loguru import logger
from loguru._contextvars import load_contextvar_class


def test_contextualize(writer):
Expand Down Expand Up @@ -209,13 +207,3 @@ def test_context_reset_despite_error(writer):
logger.info("Error")

assert writer.read() == "Division {'foobar': 456}\nError {}\n"


# There is not CI runner available for Python 3.5.2. Consequently, we are just
# verifying third-library is properly imported to reach 100% coverage.
def test_contextvars_fallback_352(monkeypatch):
mock_module = MagicMock()
with monkeypatch.context() as context:
context.setattr(sys, "version_info", (3, 5, 2))
context.setitem(sys.modules, "contextvars", mock_module)
assert load_contextvar_class() == mock_module.ContextVar
5 changes: 1 addition & 4 deletions tests/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

from loguru import logger

if sys.version_info < (3, 6):
UTC_NAME = "UTC+00:00"
else:
UTC_NAME = "UTC"
UTC_NAME = "UTC"


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_filesink_retention.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def test_not_managed_files(tmp_path):
i = logger.add(tmp_path / "test.log", retention=0, catch=False)
logger.remove(i)

assert set(f.name for f in tmp_path.iterdir()) == others
assert {f.name for f in tmp_path.iterdir()} == others


@pytest.mark.parametrize("filename", ["test", "test.log"])
Expand Down
9 changes: 2 additions & 7 deletions tests/test_repr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import pathlib
import re
import sys

from loguru import logger
Expand Down Expand Up @@ -154,12 +153,8 @@ def __repr__(self):
def test_standard_handler():
handler = logging.StreamHandler(sys.__stderr__)
logger.add(handler)
if sys.version_info >= (3, 6):
r = "<loguru.logger handlers=[(id=0, level=10, sink=<StreamHandler <stderr> (NOTSET)>)]>"
assert repr(logger) == r
else:
r = r"<loguru\.logger handlers=\[\(id=0, level=10, sink=<logging\.StreamHandler .*>\)\]>"
assert re.match(r, repr(logger))
r = "<loguru.logger handlers=[(id=0, level=10, sink=<StreamHandler <stderr> (NOTSET)>)]>"
assert repr(logger) == r


def test_multiple_handlers():
Expand Down