Skip to content

bpo-39323: Fix directory separator of imghdr cli output #17993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
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
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
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.