Visitar URL original
bpo-39323: Fix directory separator of imghdr cli output by tirkarthi · Pull Request #17993 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20260522004850/https://github.com/python/cpython/pull/17993/files
Skip to content
/ cpython Public
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/imghdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def testall(list, recursive, toplevel):
import os
for filename in list:
if os.path.isdir(filename):
print(filename + '/:', end=' ')
print(filename + os.sep +':', end=' ')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know this kind of code churn is proper,
But if this is needed, what about this?

print(f'{filename}{os.sep}:', end=' ')

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a personal opinion, I don't see much improvement here in changing. It's just concatenating two separate variables and there are no other strings also as part of string like a + " equals " + b to f"{a} equals {b}" .

if recursive or toplevel:
print('recursing down:')
import glob
Expand Down
51 changes: 50 additions & 1 deletion Lib/test/test_imghdr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import io
import os
import pathlib
import tempfile
import unittest
import warnings
from test.support import findfile, TESTFN, unlink
from test.support import findfile, TESTFN, rmtree, script_helper, unlink

TEST_FILES = (
('python.png', 'png'),
Expand Down Expand Up @@ -136,5 +137,53 @@ def test_output_stream(self):
with self.assertRaises(OSError) as cm:
imghdr.what(stream)


class TestImghdrCli(unittest.TestCase):

def setUp(self):
png_file = findfile('python.png', subdir='imghdrdata')
bmp_file = findfile('python.bmp', subdir='imghdrdata')
self.tempdir = tempfile.mkdtemp()
toplevel_dir = pathlib.Path(self.tempdir)
self.child_dir = toplevel_dir / 'child'
self.child_dir.mkdir()

with open(png_file, 'rb') as stream:
self.png_path = toplevel_dir / 'python.png'
self.png_path.write_bytes(stream.read())

with open(bmp_file, 'rb') as stream:
self.bmp_path = self.child_dir / 'python.bmp'
self.bmp_path.write_bytes(stream.read())

def tearDown(self):
rmtree(self.tempdir)

def imghdr_cmd(self, *args, **kwargs):
rc, out, err = script_helper.assert_python_ok('-m', 'imghdr', *args,
**kwargs)
out = out.decode()
return out

def test_only_filename(self):
out = self.imghdr_cmd(self.png_path)
self.assertIn(f'{self.png_path}: png', out)

def test_file_not_found(self):
out = self.imghdr_cmd(f"{self.png_path}.invalid")
self.assertIn('*** not found ***', out)

def test_toplevel_only(self):
out = self.imghdr_cmd(self.tempdir)
self.assertIn(f'{self.png_path}: png', out)
self.assertIn(f'{self.child_dir}{os.sep}: *** directory (use -r)', out)
self.assertNotIn(f'{self.bmp_path}: bmp', out)

def test_recursive(self):
out = self.imghdr_cmd('-r', self.tempdir)
self.assertIn(f'{self.png_path}: png', out)
self.assertIn(f'{self.bmp_path}: bmp', out)


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Use :data:`os.sep` instead of '/' in imghdr command line output to indicate
directory. Patch by Karthikeyan Singaravelan.