diff --git a/Lib/test/imghdrdata/python.bmp b/Lib/test/imghdrdata/python.bmp new file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..675f95191a45fdb3ae845996d6871b86286f848a GIT binary patch literal 1162 zc$|HbYe72(!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`RRR*1K(KF!;+xX6P=_U({r?r}q1nTVw=6>(+tIOkjLS2j z|AG-7lgg9Qd7V zsA>=Noj1V!vs&mnr3Ob94Ovm#c?Z;?($4@W&>@~==W!i4T%Gh}urd*PEA(*hj20YO zH08*k{)p7cLGH767C`rbWnw3PpT@NZd2`RIOvO(vhNhk}ZF6Ulsuh2~y*5*7y=WF& z>oOyq&LiHg-Y?1G4lj$Vye#OO@p5&(&tl--yCaL1+}OWaU^|$;*=1{>($w|%oNk`W XqBtJfF^g-*Vwl=ANew+K4r~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{ diff --git a/Lib/test/imghdrdata/python.pbm b/Lib/test/imghdrdata/python.pbm new file mode 100644 --- /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 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..8349f2a53a9be1d895a561cc0fd31f91d7d42b33 GIT binary patch literal 269 zc$^C` m_Uzm_3*scO?735L_ujg3_2RkzC*l0F7tWtMb#xj7HUIz!yJVLD diff --git a/Lib/test/imghdrdata/python.png b/Lib/test/imghdrdata/python.png new file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..1a987f79fcd248a94fcd1c45934136919cc0ffdd GIT binary patch literal 1020 zc%17D@N?(olHy`uVBq!ia0vp^0wB!63?wyl`GbL!Lb6AYF9SoB8UsT^3j@P1pisjL z28L1t28LG&3=CE?7#PG0=IjczVPIf98sHP+3RK|Nd%&x0mrdb3^NeW>sKBrPh)>sk zkJfF@wQFrl7Fy;^GfSHQkqVl4?C)K#pI7aGY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file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..130e96f817ed9dd45f411f2f93e0749a4b33ba49 GIT binary patch literal 1056 zc$|fkYe-XJ7{^aHsQeO(z;cz6MMPzZ?xJqG&CTWJj36rNL-nE5EDbd>)2^l&Vp=Js zi8h*TVRl`VW?o7^BqfM6qBkY62_ckp}3p zuR)u61rceLP$ZvUPJO@s`RnrU!*ui(^f@kQv+RgWzYJwcIrDYYpEr(^XDfM9V=a1I zYtDDu4Y~Don#@|4IV}10MKHTh zIMXaO3KWKCzUZbk=7P|du}9G54=_aJ*za#k1u$DxMR0rID8$^2d=sm$>+8O ztTkGx$MzdXuyflp*ly&7H#(qC#a!UU5)(b=P2`H1U5eeMHhSjx@o^t7d`$s9?Er>0 z0Ur{8cY4uXO>QtRJPTmI-Dk+XA!Xhw54GI+HIxQ;Qi0FDu7O;&Wo&jJF_oA_%q3)Ae{60n&5fYBYk=;hY^F|}v65Ic!MDr{1ESkc z?knalV}A#BUNO{{i}Pj>Q^xVF-@Bu`Mtan|@G@l7?UMJ8bk5IdsgOmAdz`G;c`Ow7 QsTbeBkx&tf#lCO-0gtGr{r~^~ diff --git a/Lib/test/imghdrdata/python.sgi b/Lib/test/imghdrdata/python.sgi new file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..ffe9081c7a5b67ed14285041fd0650f4010bc36d GIT binary patch literal 1967 zc%1E&Z%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&LY7s&qKT=r|X=&Jc+0E9=+HcwJo}P1@d;ON`+c z3l_Af((cyqGY@7|r|v*-UJC>sYXZL`S2(YP$_iXwj51qD%{O&PCy=H*(F*dcYv7;J z06wX;;F)+4aM_9N@y`}kcn?DIZ$VIw4*WB<;G1?Cypk%7yc}h>Am_5jdFql!b*jSl zdPQ!FR+drUBu%c=d1}t#qsM9aP%rN7up?m zD&xsjuzEwuxlQ#c4;kLbIT-b(EWFr6_>ho-HqKbH=fQFvInM8sDUF8DNgRCG4=}w4 z;C&>(TZ~It7i1m5@YI1Ldn8N3~i<{8RLQ4hFbf#ciAuz#Zh() z*;jgtE3Q*4uO9u@vT{+5P+}VLuW8GEa99;G#IsWhaVdt@i zYtXCk&v3=l98y<2z~?E CRNDst diff --git a/Lib/test/imghdrdata/python.xbm b/Lib/test/imghdrdata/python.xbm new file mode 100644 --- /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 --- /dev/null +++ b/Lib/test/test_imghdr.py @@ -0,0 +1,113 @@ +import imghdr +import io +import unittest +from test.support import findfile, TESTFN, unlink + +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): + 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) + 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_missing_file(self): + with self.assertRaises(FileNotFoundError): + imghdr.what('missing') + + def test_closed_file(self): + with open(TESTFN, "wb") as stream: + stream.close() + with self.assertRaises(ValueError) as cm: + imghdr.what(stream) + self.assertEqual(str(cm.exception), + "I/O operation on closed file") + + def test_text_stream(self): + with open(TESTFN, "w") as stream: + stream.write("text") + with open(TESTFN) as stream: + # certain tests uses `.startswith` + with self.assertRaises(TypeError): + imghdr.what(stream) + + def test_unseekable(self): + with open(TESTFN, "wb") as stream: + stream.write(b"binary") + 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: + with self.assertRaises(io.UnsupportedOperation) as cm: + imghdr.what(stream) + self.assertEqual(str(cm.exception), "read") + + def test_second_arg_bytearray(self): + for filename, expected in TEST_FILES: + filename = findfile(filename, subdir="imghdrdata") + with open(filename, 'rb') as stream: + data = bytearray(stream.read()) + self.assertEqual(expected, imghdr.what(None, data)) + + def test_second_arg_str(self): + with self.assertRaises(TypeError): + imghdr.what(None, "test") + + 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_first_argument(self): + for obj, type in ((1, 'int'), + (b'', 'bytes'), + (None, 'NoneType')): + with self.assertRaises(AttributeError) as cm: + imghdr.what(obj) + self.assertEqual(str(cm.exception), + "{!r} object has no attribute 'tell'" + .format(type)) + +if __name__ == '__main__': + unittest.main() diff --git a/Lib/test/test_sundry.py b/Lib/test/test_sundry.py --- a/Lib/test/test_sundry.py +++ b/Lib/test/test_sundry.py @@ -6,7 +6,7 @@ class TestUntestedModules(unittest.TestCase): def test_untested_modules_can_be_imported(self): - untested = ('bdb', 'encodings', 'formatter', 'imghdr', + untested = ('bdb', 'encodings', 'formatter', 'nturl2path', 'tabnanny') with support.check_warnings(quiet=True): for name in untested: