12
12
import platform
13
13
import random
14
14
import sys
15
+ import textwrap
15
16
import time
16
17
import unittest
17
18
@@ -676,35 +677,41 @@ async def test_pool_handles_transaction_exit_in_asyncgen_1(self):
676
677
pool = await self .create_pool (database = 'postgres' ,
677
678
min_size = 1 , max_size = 1 )
678
679
679
- async def iterate (con ):
680
- async with con .transaction ():
681
- for record in await con .fetch ("SELECT 1" ):
682
- yield record
680
+ locals_ = {}
681
+ exec (textwrap .dedent ('''\
682
+ async def iterate(con):
683
+ async with con.transaction():
684
+ for record in await con.fetch("SELECT 1"):
685
+ yield record
686
+ ''' ), globals (), locals_ )
683
687
684
688
class MyException (Exception ):
685
689
pass
686
690
687
691
with self .assertRaises (MyException ):
688
692
async with pool .acquire () as con :
689
- async for _ in iterate (con ): # noqa
693
+ async for _ in locals_ [ ' iterate' ] (con ): # noqa
690
694
raise MyException ()
691
695
692
696
@unittest .skipIf (sys .version_info [:2 ] < (3 , 6 ), 'no asyncgen support' )
693
697
async def test_pool_handles_transaction_exit_in_asyncgen_2 (self ):
694
698
pool = await self .create_pool (database = 'postgres' ,
695
699
min_size = 1 , max_size = 1 )
696
700
697
- async def iterate (con ):
698
- async with con .transaction ():
699
- for record in await con .fetch ("SELECT 1" ):
700
- yield record
701
+ locals_ = {}
702
+ exec (textwrap .dedent ('''\
703
+ async def iterate(con):
704
+ async with con.transaction():
705
+ for record in await con.fetch("SELECT 1"):
706
+ yield record
707
+ ''' ), globals (), locals_ )
701
708
702
709
class MyException (Exception ):
703
710
pass
704
711
705
712
with self .assertRaises (MyException ):
706
713
async with pool .acquire () as con :
707
- iterator = iterate (con )
714
+ iterator = locals_ [ ' iterate' ] (con )
708
715
async for _ in iterator : # noqa
709
716
raise MyException ()
710
717
@@ -715,17 +722,20 @@ async def test_pool_handles_asyncgen_finalization(self):
715
722
pool = await self .create_pool (database = 'postgres' ,
716
723
min_size = 1 , max_size = 1 )
717
724
718
- async def iterate (con ):
719
- for record in await con .fetch ("SELECT 1" ):
720
- yield record
725
+ locals_ = {}
726
+ exec (textwrap .dedent ('''\
727
+ async def iterate(con):
728
+ for record in await con.fetch("SELECT 1"):
729
+ yield record
730
+ ''' ), globals (), locals_ )
721
731
722
732
class MyException (Exception ):
723
733
pass
724
734
725
735
with self .assertRaises (MyException ):
726
736
async with pool .acquire () as con :
727
737
async with con .transaction ():
728
- async for _ in iterate (con ): # noqa
738
+ async for _ in locals_ [ ' iterate' ] (con ): # noqa
729
739
raise MyException ()
730
740
731
741
0 commit comments