@@ -232,6 +232,56 @@ class MyGeneric:
232232 self .assertTrue (repr (MyGeneric [[]]).endswith ('MyGeneric[[]]' ))
233233 self .assertTrue (repr (MyGeneric [[int , str ]]).endswith ('MyGeneric[[int, str]]' ))
234234
235+ def test_evil_repr1 (self ):
236+ # gh-143635
237+ class Zap :
238+ def __init__ (self , container ):
239+ self .container = container
240+ def __getattr__ (self , name ):
241+ if name == "__origin__" :
242+ self .container .clear ()
243+ return None
244+ if name == "__args__" :
245+ return ()
246+ raise AttributeError
247+
248+ params = []
249+ params .append (Zap (params ))
250+ alias = GenericAlias (list , (params ,))
251+ repr_str = repr (alias )
252+ self .assertTrue (repr_str .startswith ("list[[" ), repr_str )
253+
254+ def test_evil_repr2 (self ):
255+ class Zap :
256+ def __init__ (self , container ):
257+ self .container = container
258+ def __getattr__ (self , name ):
259+ if name == "__qualname__" :
260+ self .container .clear ()
261+ return "abcd"
262+ if name == "__module__" :
263+ return None
264+ raise AttributeError
265+
266+ params = []
267+ params .append (Zap (params ))
268+ alias = GenericAlias (list , (params ,))
269+ repr_str = repr (alias )
270+ self .assertTrue (repr_str .startswith ("list[[" ), repr_str )
271+
272+ def test_evil_repr3 (self ):
273+ # gh-143823
274+ lst = []
275+ class X :
276+ def __repr__ (self ):
277+ lst .clear ()
278+ return "x"
279+
280+ lst += [X (), 1 ]
281+ ga = GenericAlias (int , lst )
282+ with self .assertRaises (IndexError ):
283+ repr (ga )
284+
235285 def test_exposed_type (self ):
236286 import types
237287 a = types .GenericAlias (list , int )
0 commit comments