summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZeynel Ă–zdemir <ozdemir.zynl@gmail.com>2019-12-15 00:13:33 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-12-16 11:20:41 +0100
commit9d40b6bbf44d0de4d446dde5b9b51cadb60a24a3 (patch)
tree20713c0455cf9f2b1baeb45048a5a117181f8798
parent4161e35048d91fa84abf4dd2ebf64e04e0c37ca4 (diff)
downloaddjango-9d40b6bbf44d0de4d446dde5b9b51cadb60a24a3.tar.gz
Fixed #31076 -- Fixed dbshell crash on Windows with Python < 3.8.
subprocess.run()'s args parameter accepts path-like objects on Windows since Python 3.8.
-rw-r--r--django/db/backends/sqlite3/client.py4
-rw-r--r--tests/dbshell/test_sqlite.py31
2 files changed, 34 insertions, 1 deletions
diff --git a/django/db/backends/sqlite3/client.py b/django/db/backends/sqlite3/client.py
index 485d540188..f6b7077a0c 100644
--- a/django/db/backends/sqlite3/client.py
+++ b/django/db/backends/sqlite3/client.py
@@ -7,6 +7,8 @@ class DatabaseClient(BaseDatabaseClient):
executable_name = 'sqlite3'
def runshell(self):
+ # TODO: Remove str() when dropping support for PY37.
+ # args parameter accepts path-like objects on Windows since Python 3.8.
args = [self.executable_name,
- self.connection.settings_dict['NAME']]
+ str(self.connection.settings_dict['NAME'])]
subprocess.run(args, check=True)
diff --git a/tests/dbshell/test_sqlite.py b/tests/dbshell/test_sqlite.py
new file mode 100644
index 0000000000..46d09895c8
--- /dev/null
+++ b/tests/dbshell/test_sqlite.py
@@ -0,0 +1,31 @@
+from pathlib import Path
+from subprocess import CompletedProcess
+from unittest import mock, skipUnless
+
+from django.db import connection
+from django.db.backends.sqlite3.client import DatabaseClient
+from django.test import SimpleTestCase
+
+
+@skipUnless(connection.vendor == 'sqlite', 'SQLite tests.')
+class SqliteDbshellCommandTestCase(SimpleTestCase):
+ def _run_dbshell(self):
+ """Run runshell command and capture its arguments."""
+ def _mock_subprocess_run(*args, **kwargs):
+ self.subprocess_args = list(*args)
+ return CompletedProcess(self.subprocess_args, 0)
+
+ client = DatabaseClient(connection)
+ with mock.patch('subprocess.run', new=_mock_subprocess_run):
+ client.runshell()
+ return self.subprocess_args
+
+ def test_path_name(self):
+ with mock.patch.dict(
+ connection.settings_dict,
+ {'NAME': Path('test.db.sqlite3')},
+ ):
+ self.assertEqual(
+ self._run_dbshell(),
+ ['sqlite3', 'test.db.sqlite3'],
+ )