From 30d68c66e36864408972149f2ec341f8eda68adf Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 26 Jan 2014 23:48:20 +0200 Subject: [PATCH] Issue #19990: Added tests for the imghdr module. Based on patch by Claudiu Popa. --- Lib/test/imghdrdata/python.bmp | Bin 0 -> 1162 bytes Lib/test/imghdrdata/python.gif | Bin 0 -> 610 bytes Lib/test/imghdrdata/python.jpg | Bin 0 -> 543 bytes Lib/test/imghdrdata/python.pbm | 3 + Lib/test/imghdrdata/python.pgm | Bin 0 -> 269 bytes Lib/test/imghdrdata/python.png | Bin 0 -> 1020 bytes Lib/test/imghdrdata/python.ppm | Bin 0 -> 781 bytes Lib/test/imghdrdata/python.ras | Bin 0 -> 1056 bytes Lib/test/imghdrdata/python.sgi | Bin 0 -> 1967 bytes Lib/test/imghdrdata/python.tiff | Bin 0 -> 1326 bytes Lib/test/imghdrdata/python.xbm | 6 ++ Lib/test/test_imghdr.py | 120 ++++++++++++++++++++++++++++++++ Lib/test/test_sundry.py | 1 - Misc/NEWS | 3 + 14 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 Lib/test/imghdrdata/python.bmp create mode 100644 Lib/test/imghdrdata/python.gif create mode 100644 Lib/test/imghdrdata/python.jpg create mode 100644 Lib/test/imghdrdata/python.pbm create mode 100644 Lib/test/imghdrdata/python.pgm create mode 100644 Lib/test/imghdrdata/python.png create mode 100644 Lib/test/imghdrdata/python.ppm create mode 100644 Lib/test/imghdrdata/python.ras create mode 100644 Lib/test/imghdrdata/python.sgi create mode 100644 Lib/test/imghdrdata/python.tiff create mode 100644 Lib/test/imghdrdata/python.xbm create mode 100644 Lib/test/test_imghdr.py diff --git a/Lib/test/imghdrdata/python.bmp b/Lib/test/imghdrdata/python.bmp new file mode 100644 index 0000000000000000000000000000000000000000..675f95191a45fdb3ae845996d6871b86286f848a GIT binary patch literal 1162 zc-noEYe72(!tz6fyd=ClJ?HhT-a+nRzYk~6Iln#UzoRr-RtmVX zAce>}ikrY+AS4aicP?;zxP?-alv83`2u^wE=jC5!caw*Od6t;XL2|QsbyI!4dyL1F zFfh3)lyCU2*16Uo8<7Y^iD(ct5{Yc0{beg|{D(n0u{av?ie-Yv0enXR){iVsi|Pgn zupNzvT%9+~uuS}WfT(GJov#6igOtw;mch0h@kQK`Ra{&G!3w(eB<}rl0H%)soMQ?d z698N29N1UTdu>DfkhSza0Z0t#9sB~|aL7i!TEm~)JGR>F9RdCJ4)5M2L=D6F)aBor zC(mlhkuiRbjeG?(=gL4I>U$t- zZs9I*q@rUVI*dHW4xVGf;4d4Qp}RDJ=`4zewi9xwJ|wo12#^xzFBN0Nn?ciJkm?8rL4=%{{9!6+g8YntIB#&7DQ6R{Z_;+DxhSqFHRM z%Zzk7k9fO!za)!0yezKrvY>Cq%hmZli-CLZjx1VoWB+D>?O^(5m#u+HQ`h5jx_K^( W;&^DsEUq1kVQSMPHT0~IzyA-2^0_7e literal 0 Hc-jL100001 diff --git a/Lib/test/imghdrdata/python.gif b/Lib/test/imghdrdata/python.gif new file mode 100644 index 0000000000000000000000000000000000000000..96fd9fef76b10899807d3724c97335f35a03743a GIT binary patch literal 610 zc-nLKbhEHb6krfw_?E)(dbh;ueTttCO8q)6`S-N+|1&bL4{7~Bul)bK%Kr4r~kKs$oc;LN)jeQxHW8aYux1CxXGh=i$}{=kJfFTt=qlYcY3w$^6uE})49i|YmaaDKA*1r ze!U0$dk+Tm9}b#$?EnA&4AcgSKUo+V7*ZJ&|8slhCZ?zQCZ=a5XS1g#=H@2a8ZhX9 z)PSOjfqg|oYEx=rRFIFGgL!jLQ*26*my4}MZ%_C{7ds2nX{qtyJ}#C z1Kb^KOqZvsD@&#(#fJoVZ7|($n5rx*JU={SlEZT2gVIO1Q;&PQ&+U~J=VniBcQ;Pe z)z?v1la=D;U`h3vl&WW-qp7ASF37>k)Z}EB`u^k7=d4^2P3cUBn3;hN{veVc*TTYN cJ9T2YqZ5N z4?hnVHy<}AC$AtcAHRTrpa2(-kg$+|Fpw<(GK3MNlbMABs8N8MlUsmf@c#gVAP0j0 zg8(z55(ASUBeNjm|04`yV5cGjb{1BkxQYM+BMT!7(8VxuMkb)kS=l*+gawrtL^utN zfT~2%H8U`<0fm(XfvQ=VSrMX)OoGfTtZa%5LWZJZiH#5c-(uilW&}EeS&+e=L1fvb zEa~Ed&xD1pOqKa8nEXUFb>FV2H<_*u8=op{3RxC}#2pA=S4vz?wZ+4R}$^6SrX zmy0(p4VS5xc~$tx+w%HjrQlCenfI;~dRc5~dg0xvRhTJvR#(ybaiv@y0yEpwOUbgM*7O^DLXw={db;d)fDt@)cR0$^OH?OUFbgEZ%GRs&b)Xw%Qj}a z^XBVSsSz7*1}b*!PuX$#x(y?Y3chU=zZ=W$(@EkZ0<@^X^Yc5NS#h?%feI jL&Kc4v#2uRiBV_%;ikUjK|QY&diPaG%CoPn|9=wzUksC{ literal 0 Hc-jL100001 diff --git a/Lib/test/imghdrdata/python.pbm b/Lib/test/imghdrdata/python.pbm new file mode 100644 index 000000000000..1848ba7ff064 --- /dev/null +++ b/Lib/test/imghdrdata/python.pbm @@ -0,0 +1,3 @@ +P4 +16 16 +ûñ¿úßÕ­±[ñ¥a_ÁX°°ðððð?ÿÿ \ No newline at end of file diff --git a/Lib/test/imghdrdata/python.pgm b/Lib/test/imghdrdata/python.pgm new file mode 100644 index 0000000000000000000000000000000000000000..8349f2a53a9be1d895a561cc0fd31f91d7d42b33 GIT binary patch literal 269 zc-jjv m_Uzm_3*scO?735L_ujg3_2RkzC*l0F7tWtMb#xj7HUIz!yJVLD literal 0 Hc-jL100001 diff --git a/Lib/test/imghdrdata/python.png b/Lib/test/imghdrdata/python.png new file mode 100644 index 0000000000000000000000000000000000000000..1a987f79fcd248a94fcd1c45934136919cc0ffdd GIT binary patch literal 1020 zc-rd>@N?(olHy`uVBq!ia0vp^0wB!63?wyl`GbL!Lb6AYF9SoB8UsT^3j@P1pisjL z28L1t28LG&3=CE?7#PG0=IjczVPIf98sHP+3RK|Nd%&x0mrdb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e=g5+p5Hy^f9`!YJJQc{ z95+kUB95C%4{i!KPBd6bT8EQPK7{f3T_oh!BmO`gAG_~zd1Puuz(k-JFc&w2h5m2@ zG`ZKH&c1?ZYZVk}=ax}E;D7wug8MKWxdm;W3+fy@VlpowGQEQF8p_WZgUPp)KB+Mm zKdvCEO+UE_#ik@AHU1b*9bNu+YmlwXK1E017 zLmPn)$-q0U$gUzbgcqI#v3K|BY&Rs&TjXJ;JHLjkfF}d^?9b|mRZ1-2mbd?Go5%XH z#^>01RTljtnbjume7x)qVmA?M@nb_Jmg!*oMOiqVpH2!U#gZbt{9o4frAy`XJPmhx z+Lt>$Ex}GtQy^1rXO&FjyA0f9(hSmUlFZAG)=i+g(NuRe(7lA!)QB}!kQR*dFZ05H z$o3Qaim^-Cbzt*~r@UOOH;puD4Bq^`JI+1QqvVB`p+43v?;q)!o7Y+?ixGRADA+va Si+yUv@2@9Ckrs-1|M&xssHXk^ literal 0 Hc-jL100001 diff --git a/Lib/test/imghdrdata/python.sgi b/Lib/test/imghdrdata/python.sgi new file mode 100644 index 0000000000000000000000000000000000000000..ffe9081c7a5b67ed14285041fd0650f4010bc36d GIT binary patch literal 1967 zc-rlhZ%kWN7{=ejZ7D4pYMBwG{#oqiV%+9%6v`;{QVJEi&KMVG+=sajlTH*2V8F?= z?S&R_{~J)528J@MB07<&%sDpQKCE#lDjSO<8;P5l``{MSxTHBg_sC_N2$!A@|7$Z!XM zHngBt0E@s=pc700oFTmnNFagC0}C%_wE7MUfW z9()dx$gKby!5-8bK`Cee=&w7E%mbhXd0Qw%Vdbj-zo`x#0=9q7a2F@L%hWCDZs z`xfmx3)}`QV1Y)KHg*GHo;LrZRwjF%{qqp8n4J1yGI63u6{U{li>-HB6w%>JBso95WtBxq1ygzs zl(KVYPyaZ{PpZOSUi9!{o~wvpQqQHN%W7nL{QKz0z}1?$*|XDAlL=L?O0~Bvb*55O>_SAr$+g% zB1J07U3R*fGH{lX>w~S$&(itdepQ>Wld^YUcyJfJuo+Ft3YXbfF1!dfI_@7#B9>&8 z)LvETF16?CD0CquaE!M{g0H+3SfL~7*qP}mc1pIsIPk%Hdpxi_V&PJDKa=0h=-7In zw^DpAiBtu>zWULMvXWH=_WV5hUFoyEtL?QdP4$}tH5=%=nV;E|>>WP%aeQEJcWifr zJY@xJDZC6k#IZF|WSkUv%;)up&LY7s2&jf6bbo9^~X{(XQ+{yrj==im==m! zp^h{cW?31fnUB&xvXaa+BSVEAETIQ+Nf^I==j?7>vOMFv`#b0SKIiv1cZ-a4V{EOM zc_XH*r!DHtgIUF?I}n!N3c<&kA@Il*);FoD61NwlESUv{s7#l@QAdL; zuT?9_Y-kpzR_T1y=XHLm)s*FU1;ZTm z4e%D@Qq~<=7ce|^X35#Iyc=Vtnnj+J2TPj9GLjC@wm_TwK}_J6CAUsw!`Y;5cD!|afd#V!pBu3*2Z0q` zBcX&mA4kq6=dR%F0@O;*pKOSD{#((upm_=yslb}rLOeeRZ5yujZ?3;!-+ywvVIx31aG>o}HPbNpsIkR;!uc*;LyH literal 0 Hc-jL100001 diff --git a/Lib/test/imghdrdata/python.xbm b/Lib/test/imghdrdata/python.xbm new file mode 100644 index 000000000000..cfbee2e98062 --- /dev/null +++ b/Lib/test/imghdrdata/python.xbm @@ -0,0 +1,6 @@ +#define python_width 16 +#define python_height 16 +static char python_bits[] = { + 0xDF, 0xFE, 0x8F, 0xFD, 0x5F, 0xFB, 0xAB, 0xFE, 0xB5, 0x8D, 0xDA, 0x8F, + 0xA5, 0x86, 0xFA, 0x83, 0x1A, 0x80, 0x0D, 0x80, 0x0D, 0x80, 0x0F, 0xE0, + 0x0F, 0xF8, 0x0F, 0xF8, 0x0F, 0xFC, 0xFF, 0xFF, }; diff --git a/Lib/test/test_imghdr.py b/Lib/test/test_imghdr.py new file mode 100644 index 000000000000..9306d9bb23fc --- /dev/null +++ b/Lib/test/test_imghdr.py @@ -0,0 +1,120 @@ +import imghdr +import io +import sys +import unittest +from test.test_support import findfile, TESTFN, unlink, run_unittest + +TEST_FILES = ( + ('python.png', 'png'), + ('python.gif', 'gif'), + ('python.bmp', 'bmp'), + ('python.ppm', 'ppm'), + ('python.pgm', 'pgm'), + ('python.pbm', 'pbm'), + ('python.jpg', 'jpeg'), + ('python.ras', 'rast'), + ('python.sgi', 'rgb'), + ('python.tiff', 'tiff'), + ('python.xbm', 'xbm') +) + +class UnseekableIO(io.FileIO): + def tell(self): + raise io.UnsupportedOperation + + def seek(self, *args, **kwargs): + raise io.UnsupportedOperation + +class TestImghdr(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.testfile = findfile('python.png', subdir='imghdrdata') + with open(cls.testfile, 'rb') as stream: + cls.testdata = stream.read() + + def tearDown(self): + unlink(TESTFN) + + def test_data(self): + for filename, expected in TEST_FILES: + filename = findfile(filename, subdir='imghdrdata') + self.assertEqual(imghdr.what(filename), expected) + ufilename = filename.decode(sys.getfilesystemencoding()) + self.assertEqual(imghdr.what(ufilename), expected) + with open(filename, 'rb') as stream: + self.assertEqual(imghdr.what(stream), expected) + with open(filename, 'rb') as stream: + data = stream.read() + self.assertEqual(imghdr.what(None, data), expected) + + def test_register_test(self): + def test_jumbo(h, file): + if h.startswith(b'eggs'): + return 'ham' + imghdr.tests.append(test_jumbo) + self.addCleanup(imghdr.tests.pop) + self.assertEqual(imghdr.what(None, b'eggs'), 'ham') + + def test_file_pos(self): + with open(TESTFN, 'wb') as stream: + stream.write(b'ababagalamaga') + pos = stream.tell() + stream.write(self.testdata) + with open(TESTFN, 'rb') as stream: + stream.seek(pos) + self.assertEqual(imghdr.what(stream), 'png') + self.assertEqual(stream.tell(), pos) + + def test_bad_args(self): + with self.assertRaises(TypeError): + imghdr.what() + with self.assertRaises(AttributeError): + imghdr.what(None) + with self.assertRaises(TypeError): + imghdr.what(self.testfile, 1) + with open(self.testfile, 'rb') as f: + with self.assertRaises(AttributeError): + imghdr.what(f.fileno()) + + def test_invalid_headers(self): + for header in (b'\211PN\r\n', + b'\001\331', + b'\x59\xA6', + b'cutecat', + b'000000JFI', + b'GIF80'): + self.assertIsNone(imghdr.what(None, header)) + + def test_missing_file(self): + with self.assertRaises(IOError): + imghdr.what('missing') + + def test_closed_file(self): + stream = open(self.testfile, 'rb') + stream.close() + with self.assertRaises(ValueError) as cm: + imghdr.what(stream) + stream = io.BytesIO(self.testdata) + stream.close() + with self.assertRaises(ValueError) as cm: + imghdr.what(stream) + + def test_unseekable(self): + with open(TESTFN, 'wb') as stream: + stream.write(self.testdata) + with UnseekableIO(TESTFN, 'rb') as stream: + with self.assertRaises(io.UnsupportedOperation): + imghdr.what(stream) + + def test_output_stream(self): + with open(TESTFN, 'wb') as stream: + stream.write(self.testdata) + stream.seek(0) + with self.assertRaises(IOError) as cm: + imghdr.what(stream) + +def test_main(): + run_unittest(TestImghdr) + +if __name__ == '__main__': + test_main() diff --git a/Lib/test/test_sundry.py b/Lib/test/test_sundry.py index e190c1151331..46cbab5f474c 100644 --- a/Lib/test/test_sundry.py +++ b/Lib/test/test_sundry.py @@ -49,7 +49,6 @@ class TestUntestedModules(unittest.TestCase): import getpass import htmlentitydefs import ihooks - import imghdr import imputil import keyword import linecache diff --git a/Misc/NEWS b/Misc/NEWS index d1f7b7b69821..9ec391a7f6a2 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -203,6 +203,9 @@ IDLE Tests ----- +- Issue #19990: Added tests for the imghdr module. Based on patch by + Claudiu Popa. + - Issue #19804: The test_find_mac test in test_uuid is now skipped if the ifconfig executable is not available. -- 2.47.3