From 0c53d507f6e5a3de331f16706e7fa43f0bf796ae Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Fri, 16 Aug 2019 15:15:33 +0900 Subject: [PATCH] Modify imghdr.py and write unittest --- Lib/imghdr.py | 9 ++++++--- tests/snippets/imghdrdata/python.bmp | Bin 0 -> 1162 bytes tests/snippets/imghdrdata/python.exr | Bin 0 -> 2635 bytes tests/snippets/imghdrdata/python.gif | Bin 0 -> 610 bytes tests/snippets/imghdrdata/python.jpg | Bin 0 -> 543 bytes tests/snippets/imghdrdata/python.pbm | 3 +++ tests/snippets/imghdrdata/python.pgm | Bin 0 -> 269 bytes tests/snippets/imghdrdata/python.png | Bin 0 -> 1020 bytes tests/snippets/imghdrdata/python.ppm | Bin 0 -> 781 bytes tests/snippets/imghdrdata/python.ras | Bin 0 -> 1056 bytes tests/snippets/imghdrdata/python.sgi | Bin 0 -> 1967 bytes tests/snippets/imghdrdata/python.tiff | Bin 0 -> 1326 bytes tests/snippets/imghdrdata/python.webp | Bin 0 -> 432 bytes tests/snippets/imghdrdata/python.xbm | 6 ++++++ tests/snippets/test_imghdr.py | 24 ++++++++++++++++++++++++ 15 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 tests/snippets/imghdrdata/python.bmp create mode 100644 tests/snippets/imghdrdata/python.exr create mode 100644 tests/snippets/imghdrdata/python.gif create mode 100644 tests/snippets/imghdrdata/python.jpg create mode 100644 tests/snippets/imghdrdata/python.pbm create mode 100644 tests/snippets/imghdrdata/python.pgm create mode 100644 tests/snippets/imghdrdata/python.png create mode 100644 tests/snippets/imghdrdata/python.ppm create mode 100644 tests/snippets/imghdrdata/python.ras create mode 100644 tests/snippets/imghdrdata/python.sgi create mode 100644 tests/snippets/imghdrdata/python.tiff create mode 100644 tests/snippets/imghdrdata/python.webp create mode 100644 tests/snippets/imghdrdata/python.xbm create mode 100644 tests/snippets/test_imghdr.py diff --git a/Lib/imghdr.py b/Lib/imghdr.py index 76e8abb2d..c4a30094c 100644 --- a/Lib/imghdr.py +++ b/Lib/imghdr.py @@ -1,9 +1,11 @@ """Recognize image file formats based on their first few bytes.""" -from os import PathLike +#from os import PathLike __all__ = ["what"] +# should replace using FileIO into file +from io import FileIO #-------------------------# # Recognize image headers # #-------------------------# @@ -12,8 +14,9 @@ def what(file, h=None): f = None try: if h is None: - if isinstance(file, (str, PathLike)): - f = open(file, 'rb') + # if isinstance(file, (str, PathLike)) + if isinstance(file, str): # FIXME(corona10): RustPython doesn't support PathLike yet. + f = FileIO(file, 'rb') h = f.read(32) else: location = file.tell() diff --git a/tests/snippets/imghdrdata/python.bmp b/tests/snippets/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/tests/snippets/imghdrdata/python.exr b/tests/snippets/imghdrdata/python.exr new file mode 100644 index 0000000000000000000000000000000000000000..773c81ee1fb850cdbb6cccd3ae5edabb80146481 GIT binary patch literal 2635 zcmcJROK2NM7{@2=N@|-p=|Qy*&7%~@59(Nt9qrD{DhX+Ai$Y5vmGPwyR7-LMiexOs zO(7|QBFmSQLUHU%A5d)h)E0`IT#^`yB17rp;>J*toManH+guzM3T?lco%Qb8Dm`_U z-I?7P&G(!C_n$$NfzZCk5JIEp6S-U}TYyG3Qz)XB;oBLs51zd6n%^((`4Zdn<=mbx zXf%Ied?HmSWb!#zTTGu!6cZzvTr&R-d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ccIw1(M<*tumnzd%e%Nq;p;v*Om5IR`0Io9gE&u=k literal 0 HcmV?d00001 diff --git a/tests/snippets/imghdrdata/python.jpg b/tests/snippets/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/tests/snippets/imghdrdata/python.png b/tests/snippets/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/tests/snippets/imghdrdata/python.ppm b/tests/snippets/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/tests/snippets/imghdrdata/python.tiff b/tests/snippets/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/tests/snippets/imghdrdata/python.webp b/tests/snippets/imghdrdata/python.webp new file mode 100644 index 0000000000000000000000000000000000000000..e824ec7fb1c7fa85716d27454e9d1715a896d4b4 GIT binary patch literal 432 zcmV;h0Z;x?Nk&Gf0RRA3MM6+kP&il$0000G0000F000jF06|PpNW%aC00AeWsF5V) zdgmBl=ie%sh?xI$agE?6Fyi5hcQVMq5nO{SFzB#&6^?jOHNglB0#A~lYOqxq9M-mN zMlVjrc09K2cl)m?t|6lTNzwg}koUSoNhBLOC6P4q6m1U2)8!T*^a~Wekl};38Zw?X zf`1(;eol4>`-q;ik?)yvM(8qv+xW~#viC(WeIWfd7mOh?RxPF0Ya_f}@)trH( z*>09~iWo5uimEJ7H2N@t-@0p1QYp19&*@YY3IPBB09H^qAie+q0FVLzodGHk01yBJ z05m1A0(3A>2!#xM104t~{pbMx{dA!H0-GO-_d|S~H}=vDuhaiEXyWdty~%GI^s2>U zuDYL>gx^7;JK9_1@A%8{y?^1a`ak6f?DV^4U;nBp*?-Aw=Z5C2m$~G|^phiuU)_&6 zRUm(rPy9Lf|8LT31L&W;gP3M*vDFj*b{FQc{#}aXpGUv5`oI1i9rF^8{#?(&`uP~c az0c1dkvX6N{h9$wF)ZG^*?X$20000=+R1SM literal 0 HcmV?d00001 diff --git a/tests/snippets/imghdrdata/python.xbm b/tests/snippets/imghdrdata/python.xbm new file mode 100644 index 000000000..cfbee2e98 --- /dev/null +++ b/tests/snippets/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/tests/snippets/test_imghdr.py b/tests/snippets/test_imghdr.py new file mode 100644 index 000000000..56dcfbb01 --- /dev/null +++ b/tests/snippets/test_imghdr.py @@ -0,0 +1,24 @@ +# unittest for modified imghdr.py +# Should be replace it into https://github.com/python/cpython/blob/master/Lib/test/test_imghdr.py + +import imghdr + +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'), + ('python.webp', 'webp'), + ('python.exr', 'exr'), +) + +for fname, expected in TEST_FILES: + res = imghdr.what('tests/snippets/imghdrdata/'+fname) + assert res == expected \ No newline at end of file