@@ -11,9 +11,14 @@ async def test_transaction_regular(self):
1111 self .assertIsNone (self .con ._top_xact )
1212
1313 with self .assertRaises (ZeroDivisionError ):
14- async with tr :
14+ async with tr as with_tr :
1515 self .assertIs (self .con ._top_xact , tr )
1616
17+ # We don't return the transaction object from __aenter__,
18+ # to make it harder for people to use '.rollback()' and
19+ # '.commit()' from within an 'async with' block.
20+ self .assertIsNone (with_tr )
21+
1722 await self .con .execute ('''
1823 CREATE TABLE mytab (a int);
1924 ''' )
@@ -107,23 +112,23 @@ async def test_transaction_interface_errors(self):
107112
108113 tr = self .con .transaction ()
109114 with self .assertRaisesRegex (asyncpg .InterfaceError ,
110- 'cannot manually commit.*async def ' ):
115+ 'cannot manually commit.*async with ' ):
111116 async with tr :
112117 await tr .commit ()
113118
114119 self .assertIsNone (self .con ._top_xact )
115120
116121 tr = self .con .transaction ()
117122 with self .assertRaisesRegex (asyncpg .InterfaceError ,
118- 'cannot manually rollback.*async def ' ):
123+ 'cannot manually rollback.*async with ' ):
119124 async with tr :
120125 await tr .rollback ()
121126
122127 self .assertIsNone (self .con ._top_xact )
123128
124129 tr = self .con .transaction ()
125130 with self .assertRaisesRegex (asyncpg .InterfaceError ,
126- 'cannot enter context' ):
131+ 'cannot enter context:.*async with ' ):
127132 async with tr :
128133 async with tr :
129134 pass
0 commit comments