Skip to content

Commit

Permalink
Redirect stdout to stderr when configuring Django
Browse files Browse the repository at this point in the history
This prevents from a malformed JSON being created if any stdout output
is made during Django setup, e.g.: print().

Signed-off-by: Noam Stolero <nstolero@barracuda.com>
  • Loading branch information
noamsto committed Feb 6, 2023
1 parent b8217c1 commit 5652449
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
13 changes: 9 additions & 4 deletions pylint_django/checkers/foreign_key_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from pylint_django.__pkginfo__ import BASE_ID
from pylint_django.transforms import foreignkey
from pylint_django.utils import redirect_stdout_to_stderr


class ForeignKeyStringsChecker(BaseChecker):
Expand Down Expand Up @@ -89,7 +90,8 @@ def open(self):
try:
import django # pylint: disable=import-outside-toplevel

django.setup()
with redirect_stdout_to_stderr():
django.setup()
from django.apps import ( # noqa pylint: disable=import-outside-toplevel,unused-import
apps,
)
Expand All @@ -108,7 +110,8 @@ def open(self):
)

settings.configure()
django.setup()
with redirect_stdout_to_stderr():
django.setup()
else:
# see if we can load the provided settings module
try:
Expand All @@ -118,7 +121,8 @@ def open(self):
)

settings.configure(Settings(self.config.django_settings_module))
django.setup()
with redirect_stdout_to_stderr():
django.setup()
except ImportError:
# we could not find the provided settings module...
# at least here it is a fatal error so we can just raise this immediately
Expand All @@ -132,7 +136,8 @@ def open(self):
)

settings.configure()
django.setup()
with redirect_stdout_to_stderr():
django.setup()

# Now we can add the transforms specific to this checker
foreignkey.add_transform(astroid.MANAGER)
Expand Down
5 changes: 3 additions & 2 deletions pylint_django/transforms/foreignkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from astroid import MANAGER, InferenceError, UseInferenceDefault, inference_tip, nodes
from astroid.nodes import Attribute, ClassDef

from pylint_django.utils import node_is_subclass
from pylint_django.utils import node_is_subclass, redirect_stdout_to_stderr


def is_foreignkey_in_class(node):
Expand Down Expand Up @@ -41,7 +41,8 @@ def _get_model_class_defs_from_module(module, model_name, module_name):
def _module_name_from_django_model_resolution(model_name, module_name):
import django # pylint: disable=import-outside-toplevel

django.setup()
with redirect_stdout_to_stderr():
django.setup()
from django.apps import apps # pylint: disable=import-outside-toplevel

app = apps.get_app_config(module_name)
Expand Down
10 changes: 10 additions & 0 deletions pylint_django/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Utils."""
from contextlib import contextmanager
import os
import sys

import astroid
Expand Down Expand Up @@ -38,3 +40,11 @@ def is_migrations_module(node):
return False

return "migrations" in node.path[0] and not node.path[0].endswith("__init__.py")


@contextmanager
def redirect_stdout_to_stderr():
orig_stdout = sys.stdout
sys.stdout = sys.stderr
yield
sys.stdout = orig_stdout

0 comments on commit 5652449

Please sign in to comment.