@@ -245,6 +245,56 @@ class MyGeneric:
245245 self .assertEndsWith (repr (MyGeneric [[]]), 'MyGeneric[[]]' )
246246 self .assertEndsWith (repr (MyGeneric [[int , str ]]), 'MyGeneric[[int, str]]' )
247247
248+ def test_evil_repr1 (self ):
249+ # gh-143635
250+ class Zap :
251+ def __init__ (self , container ):
252+ self .container = container
253+ def __getattr__ (self , name ):
254+ if name == "__origin__" :
255+ self .container .clear ()
256+ return None
257+ if name == "__args__" :
258+ return ()
259+ raise AttributeError
260+
261+ params = []
262+ params .append (Zap (params ))
263+ alias = GenericAlias (list , (params ,))
264+ repr_str = repr (alias )
265+ self .assertTrue (repr_str .startswith ("list[[" ), repr_str )
266+
267+ def test_evil_repr2 (self ):
268+ class Zap :
269+ def __init__ (self , container ):
270+ self .container = container
271+ def __getattr__ (self , name ):
272+ if name == "__qualname__" :
273+ self .container .clear ()
274+ return "abcd"
275+ if name == "__module__" :
276+ return None
277+ raise AttributeError
278+
279+ params = []
280+ params .append (Zap (params ))
281+ alias = GenericAlias (list , (params ,))
282+ repr_str = repr (alias )
283+ self .assertTrue (repr_str .startswith ("list[[" ), repr_str )
284+
285+ def test_evil_repr3 (self ):
286+ # gh-143823
287+ lst = []
288+ class X :
289+ def __repr__ (self ):
290+ lst .clear ()
291+ return "x"
292+
293+ lst += [X (), 1 ]
294+ ga = GenericAlias (int , lst )
295+ with self .assertRaises (IndexError ):
296+ repr (ga )
297+
248298 def test_exposed_type (self ):
249299 import types
250300 a = types .GenericAlias (list , int )
0 commit comments