1010
1111from PIL import Image
1212
13- from .helper import assert_image_equal , hopper , is_win32
13+ from .helper import assert_image_equal , hopper , is_win32 , modes
1414
1515# CFFI imports pycparser which doesn't support PYTHONOPTIMIZE=2
1616# https://github.com/eliben/pycparser/pull/198#issuecomment-317001670
3333
3434
3535class AccessTest :
36- # initial value
36+ # Initial value
3737 _init_cffi_access = Image .USE_CFFI_ACCESS
3838 _need_cffi_access = False
3939
@@ -138,8 +138,8 @@ def color(mode: str) -> int | tuple[int, ...]:
138138 if bands == 1 :
139139 return 1
140140 if mode in ("BGR;15" , "BGR;16" ):
141- # These modes have less than 8 bits per band
142- # So (1, 2, 3) cannot be roundtripped
141+ # These modes have less than 8 bits per band,
142+ # so (1, 2, 3) cannot be roundtripped.
143143 return (16 , 32 , 49 )
144144 return tuple (range (1 , bands + 1 ))
145145
@@ -151,7 +151,7 @@ def check(self, mode: str, expected_color_int: int | None = None) -> None:
151151 self .color (mode ) if expected_color_int is None else expected_color_int
152152 )
153153
154- # check putpixel
154+ # Check putpixel
155155 im = Image .new (mode , (1 , 1 ), None )
156156 im .putpixel ((0 , 0 ), expected_color )
157157 actual_color = im .getpixel ((0 , 0 ))
@@ -160,74 +160,52 @@ def check(self, mode: str, expected_color_int: int | None = None) -> None:
160160 f"expected { expected_color } got { actual_color } "
161161 )
162162
163- # check putpixel negative index
163+ # Check putpixel negative index
164164 im .putpixel ((- 1 , - 1 ), expected_color )
165165 actual_color = im .getpixel ((- 1 , - 1 ))
166166 assert actual_color == expected_color , (
167167 f"put/getpixel roundtrip negative index failed for mode { mode } , "
168168 f"expected { expected_color } got { actual_color } "
169169 )
170170
171- # Check 0
171+ # Check 0x0 image with None initial color
172172 im = Image .new (mode , (0 , 0 ), None )
173173 assert im .load () is not None
174-
175174 error = ValueError if self ._need_cffi_access else IndexError
176175 with pytest .raises (error ):
177176 im .putpixel ((0 , 0 ), expected_color )
178177 with pytest .raises (error ):
179178 im .getpixel ((0 , 0 ))
180- # Check 0 negative index
179+ # Check negative index
181180 with pytest .raises (error ):
182181 im .putpixel ((- 1 , - 1 ), expected_color )
183182 with pytest .raises (error ):
184183 im .getpixel ((- 1 , - 1 ))
185184
186- # check initial color
185+ # Check initial color
187186 im = Image .new (mode , (1 , 1 ), expected_color )
188187 actual_color = im .getpixel ((0 , 0 ))
189188 assert actual_color == expected_color , (
190189 f"initial color failed for mode { mode } , "
191190 f"expected { expected_color } got { actual_color } "
192191 )
193192
194- # check initial color negative index
193+ # Check initial color negative index
195194 actual_color = im .getpixel ((- 1 , - 1 ))
196195 assert actual_color == expected_color , (
197196 f"initial color failed with negative index for mode { mode } , "
198197 f"expected { expected_color } got { actual_color } "
199198 )
200199
201- # Check 0
200+ # Check 0x0 image with initial color
202201 im = Image .new (mode , (0 , 0 ), expected_color )
203202 with pytest .raises (error ):
204203 im .getpixel ((0 , 0 ))
205- # Check 0 negative index
204+ # Check negative index
206205 with pytest .raises (error ):
207206 im .getpixel ((- 1 , - 1 ))
208207
209- @pytest .mark .parametrize (
210- "mode" ,
211- (
212- "1" ,
213- "L" ,
214- "LA" ,
215- "I" ,
216- "I;16" ,
217- "I;16B" ,
218- "F" ,
219- "P" ,
220- "PA" ,
221- "BGR;15" ,
222- "BGR;16" ,
223- "BGR;24" ,
224- "RGB" ,
225- "RGBA" ,
226- "RGBX" ,
227- "CMYK" ,
228- "YCbCr" ,
229- ),
230- )
208+ @pytest .mark .parametrize ("mode" , modes )
231209 def test_basic (self , mode : str ) -> None :
232210 self .check (mode )
233211
@@ -238,7 +216,7 @@ def test_list(self) -> None:
238216 @pytest .mark .parametrize ("mode" , ("I;16" , "I;16B" ))
239217 @pytest .mark .parametrize ("expected_color" , (2 ** 15 - 1 , 2 ** 15 , 2 ** 15 + 1 , 2 ** 16 - 1 ))
240218 def test_signedness (self , mode : str , expected_color : int ) -> None :
241- # see https://github.com/python-pillow/Pillow/issues/452
219+ # See https://github.com/python-pillow/Pillow/issues/452
242220 # pixelaccess is using signed int* instead of uint*
243221 self .check (mode , expected_color )
244222
@@ -298,13 +276,6 @@ def test_get_vs_c(self) -> None:
298276 im = Image .new (mode , (10 , 10 ), 40000 )
299277 self ._test_get_access (im )
300278
301- # These don't actually appear to be modes that I can actually make,
302- # as unpack sets them directly into the I mode.
303- # im = Image.new('I;32L', (10, 10), -2**10)
304- # self._test_get_access(im)
305- # im = Image.new('I;32B', (10, 10), 2**10)
306- # self._test_get_access(im)
307-
308279 def _test_set_access (self , im : Image .Image , color : tuple [int , ...] | float ) -> None :
309280 """Are we writing the correct bits into the image?
310281
@@ -336,23 +307,18 @@ def test_set_vs_c(self) -> None:
336307 self ._test_set_access (hopper ("LA" ), (128 , 128 ))
337308 self ._test_set_access (hopper ("1" ), 255 )
338309 self ._test_set_access (hopper ("P" ), 128 )
339- # self._test_set_access(i , (128, 128)) #PA -- undone how to make
310+ self ._test_set_access (hopper ( "PA" ) , (128 , 128 ))
340311 self ._test_set_access (hopper ("F" ), 1024.0 )
341312
342313 for mode in ("I;16" , "I;16L" , "I;16B" , "I;16N" , "I" ):
343314 im = Image .new (mode , (10 , 10 ), 40000 )
344315 self ._test_set_access (im , 45000 )
345316
346- # im = Image.new('I;32L', (10, 10), -(2**10))
347- # self._test_set_access(im, -(2**13)+1)
348- # im = Image.new('I;32B', (10, 10), 2**10)
349- # self._test_set_access(im, 2**13-1)
350-
351317 @pytest .mark .filterwarnings ("ignore::DeprecationWarning" )
352318 def test_not_implemented (self ) -> None :
353319 assert PyAccess .new (hopper ("BGR;15" )) is None
354320
355- # ref https://github.com/python-pillow/Pillow/pull/2009
321+ # Ref https://github.com/python-pillow/Pillow/pull/2009
356322 def test_reference_counting (self ) -> None :
357323 size = 10
358324
@@ -361,7 +327,7 @@ def test_reference_counting(self) -> None:
361327 with pytest .warns (DeprecationWarning ):
362328 px = Image .new ("L" , (size , 1 ), 0 ).load ()
363329 for i in range (size ):
364- # pixels can contain garbage if image is released
330+ # Pixels can contain garbage if image is released
365331 assert px [i , 0 ] == 0
366332
367333 @pytest .mark .parametrize ("mode" , ("P" , "PA" ))
@@ -478,7 +444,7 @@ def test_embeddable(self) -> None:
478444 env = os .environ .copy ()
479445 env ["PATH" ] = sys .prefix + ";" + env ["PATH" ]
480446
481- # do not display the Windows Error Reporting dialog
447+ # Do not display the Windows Error Reporting dialog
482448 getattr (ctypes , "windll" ).kernel32 .SetErrorMode (0x0002 )
483449
484450 process = subprocess .Popen (["embed_pil.exe" ], env = env )
0 commit comments