bpo-30773: Fix ag_running; prohibit running athrow/asend/aclose in pa… · python/cpython@fc4a044 (original) (raw)

`@@ -133,24 +133,6 @@ def async_iterate(g):

`

133

133

`break

`

134

134

`return res

`

135

135

``

136

``

`-

def async_iterate(g):

`

137

``

`-

res = []

`

138

``

`-

while True:

`

139

``

`-

try:

`

140

``

`-

g.anext().next()

`

141

``

`-

except StopAsyncIteration:

`

142

``

`-

res.append('STOP')

`

143

``

`-

break

`

144

``

`-

except StopIteration as ex:

`

145

``

`-

if ex.args:

`

146

``

`-

res.append(ex.args[0])

`

147

``

`-

else:

`

148

``

`-

res.append('EMPTY StopIteration')

`

149

``

`-

break

`

150

``

`-

except Exception as ex:

`

151

``

`-

res.append(str(type(ex)))

`

152

``

`-

return res

`

153

``

-

154

136

`sync_gen_result = sync_iterate(sync_gen)

`

155

137

`async_gen_result = async_iterate(async_gen)

`

156

138

`self.assertEqual(sync_gen_result, async_gen_result)

`

`@@ -176,19 +158,22 @@ async def gen():

`

176

158

``

177

159

`g = gen()

`

178

160

`ai = g.aiter()

`

179

``

`-

self.assertEqual(ai.anext().next(), ('result',))

`

``

161

+

``

162

`+

an = ai.anext()

`

``

163

`+

self.assertEqual(an.next(), ('result',))

`

180

164

``

181

165

`try:

`

182

``

`-

ai.anext().next()

`

``

166

`+

an.next()

`

183

167

`except StopIteration as ex:

`

184

168

`self.assertEqual(ex.args[0], 123)

`

185

169

`else:

`

186

170

`self.fail('StopIteration was not raised')

`

187

171

``

188

``

`-

self.assertEqual(ai.anext().next(), ('result',))

`

``

172

`+

an = ai.anext()

`

``

173

`+

self.assertEqual(an.next(), ('result',))

`

189

174

``

190

175

`try:

`

191

``

`-

ai.anext().next()

`

``

176

`+

an.next()

`

192

177

`except StopAsyncIteration as ex:

`

193

178

`self.assertFalse(ex.args)

`

194

179

`else:

`

`@@ -212,10 +197,11 @@ async def gen():

`

212

197

``

213

198

`g = gen()

`

214

199

`ai = g.aiter()

`

215

``

`-

self.assertEqual(ai.anext().next(), ('result',))

`

``

200

`+

an = ai.anext()

`

``

201

`+

self.assertEqual(an.next(), ('result',))

`

216

202

``

217

203

`try:

`

218

``

`-

ai.anext().next()

`

``

204

`+

an.next()

`

219

205

`except StopIteration as ex:

`

220

206

`self.assertEqual(ex.args[0], 123)

`

221

207

`else:

`

`@@ -646,17 +632,13 @@ async def run():

`

646

632

`gen = foo()

`

647

633

`it = gen.aiter()

`

648

634

`self.assertEqual(await it.anext(), 1)

`

649

``

`-

t = self.loop.create_task(it.anext())

`

650

``

`-

await asyncio.sleep(0.01)

`

651

635

`await gen.aclose()

`

652

``

`-

return t

`

653

636

``

654

``

`-

t = self.loop.run_until_complete(run())

`

``

637

`+

self.loop.run_until_complete(run())

`

655

638

`self.assertEqual(DONE, 1)

`

656

639

``

657

640

`# Silence ResourceWarnings

`

658

641

`fut.cancel()

`

659

``

`-

t.cancel()

`

660

642

`self.loop.run_until_complete(asyncio.sleep(0.01))

`

661

643

``

662

644

`def test_async_gen_asyncio_gc_aclose_09(self):

`

`@@ -1053,46 +1035,18 @@ async def wait():

`

1053

1035

``

1054

1036

`self.loop.run_until_complete(asyncio.sleep(0.1))

`

1055

1037

``

1056

``

`-

self.loop.run_until_complete(self.loop.shutdown_asyncgens())

`

1057

``

`-

self.assertEqual(finalized, 2)

`

1058

``

-

1059

1038

`# Silence warnings

`

1060

1039

`t1.cancel()

`

1061

1040

`t2.cancel()

`

1062

``

`-

self.loop.run_until_complete(asyncio.sleep(0.1))

`

1063

1041

``

1064

``

`-

def test_async_gen_asyncio_shutdown_02(self):

`

1065

``

`-

logged = 0

`

1066

``

-

1067

``

`-

def logger(loop, context):

`

1068

``

`-

nonlocal logged

`

1069

``

`-

self.assertIn('asyncgen', context)

`

1070

``

`-

expected = 'an error occurred during closing of asynchronous'

`

1071

``

`-

if expected in context['message']:

`

1072

``

`-

logged += 1

`

1073

``

-

1074

``

`-

async def waiter(timeout):

`

1075

``

`-

try:

`

1076

``

`-

await asyncio.sleep(timeout)

`

1077

``

`-

yield 1

`

1078

``

`-

finally:

`

1079

``

`-

1 / 0

`

1080

``

-

1081

``

`-

async def wait():

`

1082

``

`-

async for _ in waiter(1):

`

1083

``

`-

pass

`

1084

``

-

1085

``

`-

t = self.loop.create_task(wait())

`

1086

``

`-

self.loop.run_until_complete(asyncio.sleep(0.1))

`

``

1042

`+

with self.assertRaises(asyncio.CancelledError):

`

``

1043

`+

self.loop.run_until_complete(t1)

`

``

1044

`+

with self.assertRaises(asyncio.CancelledError):

`

``

1045

`+

self.loop.run_until_complete(t2)

`

1087

1046

``

1088

``

`-

self.loop.set_exception_handler(logger)

`

1089

1047

`self.loop.run_until_complete(self.loop.shutdown_asyncgens())

`

1090

1048

``

1091

``

`-

self.assertEqual(logged, 1)

`

1092

``

-

1093

``

`-

Silence warnings

`

1094

``

`-

t.cancel()

`

1095

``

`-

self.loop.run_until_complete(asyncio.sleep(0.1))

`

``

1049

`+

self.assertEqual(finalized, 2)

`

1096

1050

``

1097

1051

`def test_async_gen_expression_01(self):

`

1098

1052

`async def arange(n):

`