From bb92dde2d71861575e87daecaff57e6bf0e38010 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 31 Jan 2021 21:05:46 +1000 Subject: [PATCH] warn add-ons importing json from anki.utils; use stdout not stderr --- pylib/anki/collection.py | 3 ++- pylib/anki/utils.py | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pylib/anki/collection.py b/pylib/anki/collection.py index c21fc57ac..9a057e23d 100644 --- a/pylib/anki/collection.py +++ b/pylib/anki/collection.py @@ -8,6 +8,7 @@ import enum import os import pprint import re +import sys import time import traceback import weakref @@ -107,7 +108,7 @@ class Collection: @property def backend(self) -> RustBackend: - traceback.print_stack() + traceback.print_stack(file=sys.stdout) print() print( "Accessing the backend directly will break in the future. Please use the public methods on Collection instead." diff --git a/pylib/anki/utils.py b/pylib/anki/utils.py index fa3e21c0d..3e7e5feda 100644 --- a/pylib/anki/utils.py +++ b/pylib/anki/utils.py @@ -3,8 +3,7 @@ from __future__ import annotations -# some add-ons expect json to be in the utils module -import json # pylint: disable=unused-import +import json as _json import os import platform import random @@ -33,8 +32,17 @@ try: from_json_bytes = orjson.loads except: print("orjson is missing; DB operations will be slower") - to_json_bytes = lambda obj: json.dumps(obj).encode("utf8") # type: ignore - from_json_bytes = json.loads + to_json_bytes = lambda obj: _json.dumps(obj).encode("utf8") # type: ignore + from_json_bytes = _json.loads + + +def __getattr__(name: str) -> Any: + if name == "json": + traceback.print_stack(file=sys.stdout) + print("add-on should import json directly, not from anki.utils") + return _json + raise AttributeError(f"module {__name__} has no attribute {name}") + # Time handling ##############################################################################