@@ -311,41 +311,50 @@ def register_array(info: TypeInfo, context: Optional[AdaptContext] = None) -> No
311311 if not info .array_oid :
312312 raise ValueError (f"the type info { info } doesn't describe an array" )
313313
314- base : Type [Any ]
315314 adapters = context .adapters if context else postgres .adapters
316315
317- base = getattr (_psycopg , "ArrayLoader" , ArrayLoader )
318- name = f"{ info .name .title ()} { base .__name__ } "
319- attribs = {
320- "base_oid" : info .oid ,
321- "delimiter" : info .delimiter .encode (),
322- }
323- loader = type (name , (base ,), attribs )
316+ loader = _make_loader (info .name , info .oid , info .delimiter )
324317 adapters .register_loader (info .array_oid , loader )
325318
319+ # No need to make a new loader because the binary datum has all the info.
326320 loader = getattr (_psycopg , "ArrayBinaryLoader" , ArrayBinaryLoader )
327321 adapters .register_loader (info .array_oid , loader )
328322
329- base = ListDumper
330- name = f"{ info .name .title ()} { base .__name__ } "
331- attribs = {
332- "oid" : info .array_oid ,
333- "element_oid" : info .oid ,
334- "delimiter" : info .delimiter .encode (),
335- }
336- dumper = type (name , (base ,), attribs )
323+ dumper = _make_dumper (info .name , info .oid , info .array_oid , info .delimiter )
337324 adapters .register_dumper (None , dumper )
338325
339- base = ListBinaryDumper
340- name = f"{ info .name .title ()} { base .__name__ } "
341- attribs = {
342- "oid" : info .array_oid ,
343- "element_oid" : info .oid ,
344- }
345- dumper = type (name , (base ,), attribs )
326+ dumper = _make_binary_dumper (info .name , info .oid , info .array_oid )
346327 adapters .register_dumper (None , dumper )
347328
348329
330+ # Cache all dynamically-generated types to avoid leaks in case the types
331+ # cannot be GC'd.
332+
333+
334+ @cache
335+ def _make_loader (name : str , oid : int , delimiter : str ) -> Type [Loader ]:
336+ # Note: caching this function is really needed because, if the C extension
337+ # is available, the resulting type cannot be GC'd, so calling
338+ # register_array() in a loop results in a leak. See #647.
339+ base = getattr (_psycopg , "ArrayLoader" , ArrayLoader )
340+ attribs = {"base_oid" : oid , "delimiter" : delimiter .encode ()}
341+ return type (f"{ name .title ()} { base .__name__ } " , (base ,), attribs )
342+
343+
344+ @cache
345+ def _make_dumper (
346+ name : str , oid : int , array_oid : int , delimiter : str
347+ ) -> Type [BaseListDumper ]:
348+ attribs = {"oid" : array_oid , "element_oid" : oid , "delimiter" : delimiter .encode ()}
349+ return type (f"{ name .title ()} ListDumper" , (ListDumper ,), attribs )
350+
351+
352+ @cache
353+ def _make_binary_dumper (name : str , oid : int , array_oid : int ) -> Type [BaseListDumper ]:
354+ attribs = {"oid" : array_oid , "element_oid" : oid }
355+ return type (f"{ name .title ()} ListBinaryDumper" , (ListBinaryDumper ,), attribs )
356+
357+
349358def register_default_adapters (context : AdaptContext ) -> None :
350359 # The text dumper is more flexible as it can handle lists of mixed type,
351360 # so register it later.
0 commit comments