summaryrefslogtreecommitdiff
path: root/django/core
diff options
context:
space:
mode:
authorJacob Walls <jacobtylerwalls@gmail.com>2024-11-29 07:04:48 -0500
committerGitHub <noreply@github.com>2024-11-29 09:04:48 -0300
commit58cc91275a68bb42780cf0e663dad9ecf49039de (patch)
treee60317923d4f2faf61238f46528ed180a5134651 /django/core
parent978aae4334fa71ba78a3e94408f0f3aebde8d07c (diff)
Fixed #35308 -- Handled OSError when launching code formatters.
Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>
Diffstat (limited to 'django/core')
-rw-r--r--django/core/management/commands/makemigrations.py4
-rw-r--r--django/core/management/commands/optimizemigration.py2
-rw-r--r--django/core/management/commands/squashmigrations.py2
-rw-r--r--django/core/management/templates.py2
-rw-r--r--django/core/management/utils.py16
5 files changed, 16 insertions, 10 deletions
diff --git a/django/core/management/commands/makemigrations.py b/django/core/management/commands/makemigrations.py
index d5d3466201..690d0e5053 100644
--- a/django/core/management/commands/makemigrations.py
+++ b/django/core/management/commands/makemigrations.py
@@ -392,7 +392,7 @@ class Command(BaseCommand):
)
)
self.log(writer.as_string())
- run_formatters(self.written_files)
+ run_formatters(self.written_files, stderr=self.stderr)
@staticmethod
def get_relative_path(path):
@@ -499,7 +499,7 @@ class Command(BaseCommand):
# Write the merge migrations file to the disk
with open(writer.path, "w", encoding="utf-8") as fh:
fh.write(writer.as_string())
- run_formatters([writer.path])
+ run_formatters([writer.path], stderr=self.stderr)
if self.verbosity > 0:
self.log("\nCreated new merge migration %s" % writer.path)
if self.scriptable:
diff --git a/django/core/management/commands/optimizemigration.py b/django/core/management/commands/optimizemigration.py
index 2064dfbf3c..90db2d2773 100644
--- a/django/core/management/commands/optimizemigration.py
+++ b/django/core/management/commands/optimizemigration.py
@@ -121,7 +121,7 @@ class Command(BaseCommand):
)
with open(writer.path, "w", encoding="utf-8") as fh:
fh.write(migration_file_string)
- run_formatters([writer.path])
+ run_formatters([writer.path], stderr=self.stderr)
if verbosity > 0:
self.stdout.write(
diff --git a/django/core/management/commands/squashmigrations.py b/django/core/management/commands/squashmigrations.py
index 6b5ddeeba5..bb636116a5 100644
--- a/django/core/management/commands/squashmigrations.py
+++ b/django/core/management/commands/squashmigrations.py
@@ -221,7 +221,7 @@ class Command(BaseCommand):
)
with open(writer.path, "w", encoding="utf-8") as fh:
fh.write(writer.as_string())
- run_formatters([writer.path])
+ run_formatters([writer.path], stderr=self.stderr)
if self.verbosity > 0:
self.stdout.write(
diff --git a/django/core/management/templates.py b/django/core/management/templates.py
index 633eed781d..dbaea11200 100644
--- a/django/core/management/templates.py
+++ b/django/core/management/templates.py
@@ -229,7 +229,7 @@ class TemplateCommand(BaseCommand):
else:
shutil.rmtree(path_to_remove)
- run_formatters([top_dir], **formatter_paths)
+ run_formatters([top_dir], **formatter_paths, stderr=self.stderr)
def handle_template(self, template, subdir):
"""
diff --git a/django/core/management/utils.py b/django/core/management/utils.py
index fca61f2c23..6ca88fa71a 100644
--- a/django/core/management/utils.py
+++ b/django/core/management/utils.py
@@ -2,6 +2,8 @@ import fnmatch
import os
import shutil
import subprocess
+import sys
+import traceback
from pathlib import Path
from subprocess import run
@@ -161,7 +163,7 @@ def find_formatters():
return {"black_path": shutil.which("black")}
-def run_formatters(written_files, black_path=(sentinel := object())):
+def run_formatters(written_files, black_path=(sentinel := object()), stderr=sys.stderr):
"""
Run the black formatter on the specified files.
"""
@@ -169,7 +171,11 @@ def run_formatters(written_files, black_path=(sentinel := object())):
if black_path is sentinel:
black_path = shutil.which("black")
if black_path:
- subprocess.run(
- [black_path, "--fast", "--", *written_files],
- capture_output=True,
- )
+ try:
+ subprocess.run(
+ [black_path, "--fast", "--", *written_files],
+ capture_output=True,
+ )
+ except OSError:
+ stderr.write("Formatters failed to launch:")
+ traceback.print_exc(file=stderr)