From 1ac00950b28e91fc35fed6445722ade92732648e Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 26 Jan 2014 23:48:38 +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 | 131 ++++++++++++++++++++++++++++++++ Lib/test/test_sundry.py | 1 - Misc/NEWS | 3 + 14 files changed, 143 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 zcmZ{kYempK|q2sSaMQcOvV!f#cTTaT5pwGN1$s5CJv=mUkMll5Jgcy;ItSC?Tnd^`$!ZyO|7%HXiN}owe^9#6tKCr10S#!uzCDIGzdHNjmWMc9{p|zePQa{W;AvB4VXGT z`>Y5WM+&f)Y5BJ0OR`&Y#e7pvBu<7?XPJl`HRPB@gg%1MhyQDN3OWN`{bX)5G|R z)Zkw1O;Wu2St}P^;?17p-|QWcLE;K)~*-(-^GMc0W0vFmLgqB#t_8FRmYt=IheKYf;2%Ew2~5z(0uc Bxh4Pr literal 0 HcmV?d00001 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 zcmZ?wbhEHb6krfw_?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>;pv=1N(}G)TY$Ls30FV2lM8hrr4AqFBe;j-k$J@E_N2C(^BKZeOxS!EG?$?Bqzp% z2Dm%em@ZFMSC&joiVq3!+F-ihFjZMrcz$@uB!}h32c?g2ryloqpW7=d&dr|M?rxl_ ztFNQ3CM(6w!IJ7TDOJxvM^jBvT#$p6smaMM_5H`E&sn)5n$npLF*5@l{6Qo?u7!ok ccIw1(M<*tumnzd%e%Nq;p;v*Om5IR`0Io9gE&u=k literal 0 HcmV?d00001 diff --git a/Lib/test/imghdrdata/python.jpg b/Lib/test/imghdrdata/python.jpg new file mode 100644 index 0000000000000000000000000000000000000000..21222c09f5a71d9d86a0323e49ebc5da6639b22f GIT binary patch literal 543 zcmex=N z4?hnVHy<}AC$AtcAHRTrpa2(-kg$+|Fpw<(GK3MNlbMABs8N8MlN%^XBKUuRL6Cz% zfI)znQHg;`kdaxC@&6G9F|bn^kN`UiD^OfTfPs;Pkp<{tm=q%u(B-V`974i^N(>^L zhDJbDq9A{w0ydzqvLFK^6Du<-LWYq^keP**O_4#!P*g0j@!|hl3_Q$?KxZ%uGT1YS zEW4B?U3~DFu+WvMGM@#LpQxtp+ZFXD)74?)Q-w_-OXV+}+VSg?!s={V53|O-aJN mJA!6tn6q{kRVF+!>dZgf)VDmS=aoY5z6wcs_O=#+*1b_a3c>-Bw{Bd$c<%p6IREU0^XE<-od)M4GXMy?WS0N{ literal 0 HcmV?d00001 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 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!63?wyl`GbL!Lb6AYF9SoB8UsT^3j@P1pisjL z28L1t28LG&3=CE?7#PG0=IjczVPIf98sHP+3RK|Nd%&x0mrdb3^NeXgE;8`zKjPE1 z-=lS#bM0E&l7*Hz)6CK)KokT`Jofjl*UzhVK(0|jJCOW))eK0QW=sN-Z_a^CHp`qW zZ3toj!K1^je{VVh8E%c6%+ewSu^cS{_bjjjNq`0qVGWL+OG*#Ek0`S+6kuj7&jreo3K z(Y(c_ZmmPba-Xg}|NsAYtzYk0xx%hwkyZX|v&<=;t=ruiHabHjtJ|Ce>W+IPA)ZgQ?!ZC|$Z|8gnk%7jDF#&p$6AgEMe;uIFrk^Al-!^-G$=KGCmYgs2(3;7>L0);>u7?Zr+ zU80zGF9pTWS5Ftm5Q)pl2@Fhb%Qk%AND~V)W7Fv9>FVq3?M_c`nE~Gk@#z{`)21Y8cxw21 z>3R1~7n?p&cm0Yf$tzZ^TRAm6KqTZ*K-kLFYr<0lPM-<7e&I@1Mo!kc?98iSNf$ab zuUyT}&d+)EA}90hrRNC-25-!=UcY?%`o)|4x37%M5)Dj^w=;awUArbXC-<$z8%Y_N zxpS(2tNF|^+1Y*g&{5GxRogj(m-%&HD*ImWLj-nwqKP5A*5>tbL0Yt;W_if978W=oX{an^LB{Ts5zOa#I literal 0 HcmV?d00001 diff --git a/Lib/test/imghdrdata/python.ppm b/Lib/test/imghdrdata/python.ppm new file mode 100644 index 0000000000000000000000000000000000000000..7d9cdb3215877b4604de4c177fc81ebb645b8290 GIT binary patch literal 781 zcmWGA<1#c;Ff`*bGBxF5KmvZf2fW&Lc{Fcvsaxw%vD~I`o_WSJBzbhEPuHIR|Npzz zuXn6mVOO%qDu1?F<`i^ANPN%M?QRVlovK&al`gg}m}{Oj4Xb*;{v$qJ`#oB>IoGbW zEm>%pGtDe*fO9I=FWsDhN%DlDD>}Lub)@# zfNlxsKkU=F$D?H{#MwaeAHjtJ|Ce=v>b=@`x;Jieu32qgw$vkbAW8H;-vqNBmUp_ z`+pmvSRbMosQ%C_A)p#$@c%hf@r&cOuaBC%KBV<}pW^G?5H49eJC|cLygR|t7(RqR+G|1 z8);jZU5(OAXX%HeBr~nZNTCbM4;67pIQ)B_xycaof#29U=l#9^=Q)>cwJDb*63JXI zK@!Py`YV~nKND4hwYYUS{>)>TPThw-uO6|7>xAfoRb`4K+Z+=9D|$DBrKlNPhGPxT zWZ!@~^BN-4Dj-k3xPsR39+MR|?2o>g0l zp4OQ18|}K>`Z`r+jZ>LwbISLeM?`8lyNrq4fN}nlO{E=3EN+9juxWJWT%NfWs*GwV z_h0t8J|7s|EQlZDPyX#y&!)Ywv7{ZAv&}G_elRvO<@;v#`Hi7F3v|3517_g8W?1Z% zhwyR2xfbutxBX_W^vx`UCzY|ANNWp!yfl!j1>P!vp4BS$;|m@k?!;Z_j^0Gfq3ezE zsgceLMsoF|GdtybORaRS_SfeFg7`fJ z__7Na+5&t^06tKz$6Z5ih#~(ij?pDgRQm)Tk%em|_M_gB=VYGfG5lloA;RNN5 zxz2B$Du?@dn8WQ_>2S9MJKRkHOm;_wl(U0~sl+T|9w8~O{^ zVlgq%yDW$U+%viTn2Qm_{T)-x1rN^pMbxLy_G6F-f|PIYRc(6(ZU`96~>T0F5h z=V!OAHmNRuLeGKfxp4mM&*S{GCcMRYPb}e?s_@74T!}l(X-toQ9~~LEmNPSbesW?w zril&0vDgfu%u8EnGVq|B{WyD&rNba`|5g1p|4Cd+9kpJWnLhRX*s+n3E=_OV;I*%M zY*9vr30g^Al%I|KsQx2=1C!aqq&Vz(WaMm&5w+LVRMRySz`|!@G4`&A^&Z$yl_=u0 zt+1HQ%!HUy73byfyv|lplT)^v|4QTRrbupVsLh(0tH4G@?9=|9@~X-g)~|8emRrp1 ze#Dw4Ie-2zOk+%@P)V`FN;eV)&JrTu-&p@5UHtvBwheY##10Mg?WR|@qDWEhatq5u zEQEuOdi&zA#aSsemz6pTt(iIsTuMkB^UWdu8?X6R>PR_pZgPU15uvXRef;5G7eX#M zcNu#GAsr77Z}hlJ<(J}cmC)O3o+>FSSe;|d&Z0lmI_+&uZ*8lq-s-E^L_bXZ$|m4y zKX&A^Xz%`x$es|nigMU8#8UAPhu4PTu~X=2kJ}~N^CYX1MO^DEw6jHdv!S+1@+q6> z!W8=jp3sS-Ukvv5bV(5tC(V Y#PP3(1`fX8p^CuZbqNQ(CtZT;AGbYjjQ{`u literal 0 HcmV?d00001 diff --git a/Lib/test/imghdrdata/python.tiff b/Lib/test/imghdrdata/python.tiff new file mode 100644 index 0000000000000000000000000000000000000000..39d0bfcec02533ade8934a8796aebb8990c9d389 GIT binary patch literal 1326 zcmZ{kYe(N;1=o3>C6iK^NjBVVur+_Bv`_wuaafkG@48y9bf^^$>oj7DD%5;{sDlOE7sh%6W8^ zHdQL~>Z@h?%35i9sZpXmZwyK=V=V6HA{sU;lGCVYLVIt4=!0B-{U`qtBV%3@DGgv|wwpgepN!VX?X zuM16{8&xT|rbbb}n^B6)`JN@ai;Xy^`8kqNWB#&- zgHJmF#34StwlYyJCWfFy(}0;bbRJ7kRA&T=p1%qegj$B8b?b1$8thpa^KedR6Wd`- ze-K0XWzWx3xlVjc$L3-)?<%wt%)z;p*q58of$d3FBAXm{KBj#a@Ua2og?v64if!`O zVoW}CP;#Loi$%D97{*RGJBs;9e7+@*+OvEFE(6tc^oEg~Ep!#U=uxxlrSfvR8njyW fXuf-=%w_kUu8C|tS<3P=v-PRE3}1bkRx9`m*;LyH literal 0 HcmV?d00001 diff --git a/Lib/test/imghdrdata/python.xbm b/Lib/test/imghdrdata/python.xbm new file mode 100644 index 00000000000..cfbee2e9806 --- /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 00000000000..0ad4343f520 --- /dev/null +++ b/Lib/test/test_imghdr.py @@ -0,0 +1,131 @@ +import imghdr +import io +import os +import unittest +import warnings +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): + @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) + 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) + self.assertEqual(imghdr.what(None, bytearray(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 self.assertRaises(AttributeError): + imghdr.what(os.fsencode(self.testfile)) + 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_string_data(self): + with warnings.catch_warnings(): + warnings.simplefilter("ignore", BytesWarning) + for filename, _ in TEST_FILES: + filename = findfile(filename, subdir='imghdrdata') + with open(filename, 'rb') as stream: + data = stream.read().decode('latin1') + with self.assertRaises(TypeError): + imghdr.what(io.StringIO(data)) + with self.assertRaises(TypeError): + imghdr.what(None, data) + + def test_missing_file(self): + with self.assertRaises(FileNotFoundError): + 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(OSError) as cm: + imghdr.what(stream) + +if __name__ == '__main__': + unittest.main() diff --git a/Lib/test/test_sundry.py b/Lib/test/test_sundry.py index c6cca269b89..8808c47edde 100644 --- a/Lib/test/test_sundry.py +++ b/Lib/test/test_sundry.py @@ -42,7 +42,6 @@ class TestUntestedModules(unittest.TestCase): import encodings import formatter import html.entities - import imghdr import keyword import mailcap import nturl2path diff --git a/Misc/NEWS b/Misc/NEWS index 89610c94e71..10cb08e54b2 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -292,6 +292,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.