Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit98df288

Browse files
MoritzStimgraham
authored andcommitted
Fixed #24978 -- Escaped special characters in loaddata fixture paths
1 parentd58573e commit98df288

File tree

5 files changed

+68
-1
lines changed

5 files changed

+68
-1
lines changed

‎django/core/management/commands/loaddata.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
fromdjango.utils._osimportupath
2222
fromdjango.utils.encodingimportforce_text
2323
fromdjango.utils.functionalimportcached_property
24+
fromdjango.utils.globimportglob_escape
2425

2526
try:
2627
importbz2
@@ -209,7 +210,8 @@ def find_fixtures(self, fixture_label):
209210
ifself.verbosity>=2:
210211
self.stdout.write("Checking %s for fixtures..."%humanize(fixture_dir))
211212
fixture_files_in_dir= []
212-
forcandidateinglob.iglob(os.path.join(fixture_dir,fixture_name+'*')):
213+
path=os.path.join(fixture_dir,fixture_name)
214+
forcandidateinglob.iglob(glob_escape(path)+'*'):
213215
ifos.path.basename(candidate)intargets:
214216
# Save the fixture_dir and fixture_name for future error messages.
215217
fixture_files_in_dir.append((candidate,fixture_dir,fixture_name))

‎django/utils/glob.py‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from __future__importunicode_literals
2+
3+
importos.path
4+
importre
5+
6+
fromdjango.utilsimportsix
7+
8+
# backport of Python 3.4's glob.escape
9+
10+
try:
11+
fromglobimportescapeasglob_escape
12+
exceptImportError:
13+
_magic_check=re.compile('([*?[])')
14+
15+
ifsix.PY3:
16+
_magic_check_bytes=re.compile(b'([*?[])')
17+
18+
defglob_escape(pathname):
19+
"""
20+
Escape all special characters.
21+
"""
22+
drive,pathname=os.path.splitdrive(pathname)
23+
ifisinstance(pathname,bytes):
24+
pathname=_magic_check_bytes.sub(br'[\1]',pathname)
25+
else:
26+
pathname=_magic_check.sub(r'[\1]',pathname)
27+
returndrive+pathname
28+
29+
else:
30+
defglob_escape(pathname):
31+
"""
32+
Escape all special characters.
33+
"""
34+
drive,pathname=os.path.splitdrive(pathname)
35+
pathname=_magic_check.sub(r'[\1]',pathname)
36+
returndrive+pathname
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[
2+
{
3+
"pk":"1",
4+
"model":"fixtures.article",
5+
"fields": {
6+
"headline":"How To Deal With Special Characters",
7+
"pub_date":"2006-06-16 12:00:00"
8+
}
9+
}
10+
]

‎tests/fixtures/tests.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ def test_dumpdata_with_excludes(self):
224224
"Unknown model in excludes: fixtures.FooModel"):
225225
self._dumpdata_assert(['fixtures','sites'],'',exclude_list=['fixtures.FooModel'])
226226

227+
deftest_load_fixture_with_special_characters(self):
228+
management.call_command('loaddata','fixture?with[special]chars*',verbosity=0)
229+
self.assertQuerysetEqual(Article.objects.all(), ['<Article: How To Deal With Special Characters>'])
230+
227231
deftest_dumpdata_with_filtering_manager(self):
228232
spy1=Spy.objects.create(name='Paul')
229233
spy2=Spy.objects.create(name='Alex',cover_blown=True)

‎tests/utils_tests/test_glob.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from __future__importunicode_literals
2+
3+
fromdjango.testimportSimpleTestCase
4+
fromdjango.utils.globimportglob_escape
5+
6+
7+
classTestUtilsGlob(SimpleTestCase):
8+
deftest_glob_escape(self):
9+
filename='/my/file?/name[with special chars*'
10+
expected='/my/file[?]/name[[]with special chars[*]'
11+
filename_b=b'/my/file?/name[with special chars*'
12+
expected_b=b'/my/file[?]/name[[]with special chars[*]'
13+
14+
self.assertEqual(glob_escape(filename),expected)
15+
self.assertEqual(glob_escape(filename_b),expected_b)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2026 Movatter.jp