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 000000000..675f95191 Binary files /dev/null and b/tests/snippets/imghdrdata/python.bmp differ diff --git a/tests/snippets/imghdrdata/python.exr b/tests/snippets/imghdrdata/python.exr new file mode 100644 index 000000000..773c81ee1 Binary files /dev/null and b/tests/snippets/imghdrdata/python.exr differ diff --git a/tests/snippets/imghdrdata/python.gif b/tests/snippets/imghdrdata/python.gif new file mode 100644 index 000000000..96fd9fef7 Binary files /dev/null and b/tests/snippets/imghdrdata/python.gif differ diff --git a/tests/snippets/imghdrdata/python.jpg b/tests/snippets/imghdrdata/python.jpg new file mode 100644 index 000000000..21222c09f Binary files /dev/null and b/tests/snippets/imghdrdata/python.jpg differ diff --git a/tests/snippets/imghdrdata/python.pbm b/tests/snippets/imghdrdata/python.pbm new file mode 100644 index 000000000..1848ba7ff --- /dev/null +++ b/tests/snippets/imghdrdata/python.pbm @@ -0,0 +1,3 @@ +P4 +16 16 +ûñ¿úßÕ­±[ñ¥a_ÁX°°ðððð?ÿÿ \ No newline at end of file diff --git a/tests/snippets/imghdrdata/python.pgm b/tests/snippets/imghdrdata/python.pgm new file mode 100644 index 000000000..8349f2a53 Binary files /dev/null and b/tests/snippets/imghdrdata/python.pgm differ diff --git a/tests/snippets/imghdrdata/python.png b/tests/snippets/imghdrdata/python.png new file mode 100644 index 000000000..1a987f79f Binary files /dev/null and b/tests/snippets/imghdrdata/python.png differ diff --git a/tests/snippets/imghdrdata/python.ppm b/tests/snippets/imghdrdata/python.ppm new file mode 100644 index 000000000..7d9cdb321 Binary files /dev/null and b/tests/snippets/imghdrdata/python.ppm differ diff --git a/tests/snippets/imghdrdata/python.ras b/tests/snippets/imghdrdata/python.ras new file mode 100644 index 000000000..130e96f81 Binary files /dev/null and b/tests/snippets/imghdrdata/python.ras differ diff --git a/tests/snippets/imghdrdata/python.sgi b/tests/snippets/imghdrdata/python.sgi new file mode 100644 index 000000000..ffe9081c7 Binary files /dev/null and b/tests/snippets/imghdrdata/python.sgi differ diff --git a/tests/snippets/imghdrdata/python.tiff b/tests/snippets/imghdrdata/python.tiff new file mode 100644 index 000000000..39d0bfcec Binary files /dev/null and b/tests/snippets/imghdrdata/python.tiff differ diff --git a/tests/snippets/imghdrdata/python.webp b/tests/snippets/imghdrdata/python.webp new file mode 100644 index 000000000..e824ec7fb Binary files /dev/null and b/tests/snippets/imghdrdata/python.webp differ 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