Skip to content

Commit

Permalink
Remove dangerous use of "importlib.reload()" in tests
Browse files Browse the repository at this point in the history
It changes the global state and may cause unexpected behavior.
  • Loading branch information
Delgan committed Jan 20, 2022
1 parent 0975926 commit 7779678
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 46 deletions.
66 changes: 35 additions & 31 deletions loguru/_ctime_functions.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,53 @@
import os

if os.name == "nt":
import win32_setctime

def get_ctime(filepath):
return os.stat(filepath).st_ctime
def load_ctime_functions():
if os.name == "nt":
import win32_setctime

def set_ctime(filepath, timestamp):
if not win32_setctime.SUPPORTED:
return
def get_ctime(filepath):
return os.stat(filepath).st_ctime

try:
win32_setctime.setctime(filepath, timestamp)
except (OSError, ValueError):
pass
def set_ctime(filepath, timestamp):
if not win32_setctime.SUPPORTED:
return

try:
win32_setctime.setctime(filepath, timestamp)
except (OSError, ValueError):
pass

elif hasattr(os.stat_result, "st_birthtime"):

elif hasattr(os.stat_result, "st_birthtime"):
def get_ctime(filepath):
return os.stat(filepath).st_birthtime

def get_ctime(filepath):
return os.stat(filepath).st_birthtime
def set_ctime(filepath, timestamp):
pass

def set_ctime(filepath, timestamp):
pass
elif hasattr(os, "getxattr") and hasattr(os, "setxattr"):

def get_ctime(filepath):
try:
return float(os.getxattr(filepath, b"user.loguru_crtime"))
except OSError:
return os.stat(filepath).st_mtime

elif hasattr(os, "getxattr") and hasattr(os, "setxattr"):
def set_ctime(filepath, timestamp):
try:
os.setxattr(filepath, b"user.loguru_crtime", str(timestamp).encode("ascii"))
except OSError:
pass

def get_ctime(filepath):
try:
return float(os.getxattr(filepath, b"user.loguru_crtime"))
except OSError:
else:

def get_ctime(filepath):
return os.stat(filepath).st_mtime

def set_ctime(filepath, timestamp):
try:
os.setxattr(filepath, b"user.loguru_crtime", str(timestamp).encode("ascii"))
except OSError:
def set_ctime(filepath, timestamp):
pass

return get_ctime, set_ctime

else:

def get_ctime(filepath):
return os.stat(filepath).st_mtime

def set_ctime(filepath, timestamp):
pass
get_ctime, set_ctime = load_ctime_functions()
13 changes: 9 additions & 4 deletions loguru/_get_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ def get_frame_fallback(n):
return frame


if hasattr(sys, "_getframe"):
get_frame = sys._getframe
else:
get_frame = get_frame_fallback
def load_get_frame_function():
if hasattr(sys, "_getframe"):
get_frame = sys._getframe
else:
get_frame = get_frame_fallback
return get_frame


get_frame = load_get_frame_function()
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ def make_logging_logger(name, handler, fmt="%(message)s", level="DEBUG"):

@pytest.fixture
def f_globals_name_absent(monkeypatch):
getframe_ = loguru._get_frame.get_frame
getframe_ = loguru._get_frame.load_get_frame_function()

def patched_getframe(*args, **kwargs):
frame = getframe_(*args, **kwargs)
monkeypatch.delitem(frame.f_globals, "__name__", raising=False)
frame.f_globals.pop("__name__", None)
return frame

monkeypatch.setattr(loguru._logger, "get_frame", patched_getframe)
8 changes: 4 additions & 4 deletions tests/test_filesink_rotation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import builtins
import datetime
import importlib
import os
import pathlib
import re
Expand All @@ -12,6 +11,7 @@
import pytest

import loguru
from loguru._ctime_functions import load_ctime_functions
from loguru import logger


Expand All @@ -24,9 +24,9 @@ def tmpdir_local(reset_logger):


def reload_filesink_ctime_functions(monkeypatch):
ctime_functions = importlib.reload(loguru._ctime_functions)
monkeypatch.setattr(loguru._file_sink, "get_ctime", ctime_functions.get_ctime)
monkeypatch.setattr(loguru._file_sink, "set_ctime", ctime_functions.set_ctime)
get_ctime, set_ctime = load_ctime_functions()
monkeypatch.setattr(loguru._file_sink, "get_ctime", get_ctime)
monkeypatch.setattr(loguru._file_sink, "set_ctime", set_ctime)


@pytest.fixture
Expand Down
8 changes: 3 additions & 5 deletions tests/test_get_frame.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import importlib
import sys

import loguru
from loguru._get_frame import load_get_frame_function


def test_with_sys_getframe(monkeypatch):
def patched():
return

monkeypatch.setattr(sys, "_getframe", patched())
get_frame_module = importlib.reload(loguru._get_frame)
assert get_frame_module.get_frame == patched()
assert load_get_frame_function() == patched()


def test_without_sys_getframe(monkeypatch):
monkeypatch.delattr(sys, "_getframe")
get_frame_module = importlib.reload(loguru._get_frame)
assert get_frame_module.get_frame == loguru._get_frame.get_frame_fallback
assert load_get_frame_function() == loguru._get_frame.get_frame_fallback


def test_get_frame_fallback():
Expand Down

0 comments on commit 7779678

Please sign in to comment.