bpo-30306: release arguments of contextmanager (GH-1500) · python/cpython@dd0e087 (original) (raw)

`@@ -8,6 +8,7 @@

`

8

8

`import unittest

`

9

9

`from contextlib import * # Tests all

`

10

10

`from test import support

`

``

11

`+

import weakref

`

11

12

``

12

13

``

13

14

`class TestAbstractContextManager(unittest.TestCase):

`

`@@ -219,6 +220,52 @@ def woohoo(self, func, args, kwds):

`

219

220

`with woohoo(self=11, func=22, args=33, kwds=44) as target:

`

220

221

`self.assertEqual(target, (11, 22, 33, 44))

`

221

222

``

``

223

`+

def test_nokeepref(self):

`

``

224

`+

class A:

`

``

225

`+

pass

`

``

226

+

``

227

`+

@contextmanager

`

``

228

`+

def woohoo(a, b):

`

``

229

`+

a = weakref.ref(a)

`

``

230

`+

b = weakref.ref(b)

`

``

231

`+

self.assertIsNone(a())

`

``

232

`+

self.assertIsNone(b())

`

``

233

`+

yield

`

``

234

+

``

235

`+

with woohoo(A(), b=A()):

`

``

236

`+

pass

`

``

237

+

``

238

`+

def test_param_errors(self):

`

``

239

`+

@contextmanager

`

``

240

`+

def woohoo(a, *, b):

`

``

241

`+

yield

`

``

242

+

``

243

`+

with self.assertRaises(TypeError):

`

``

244

`+

woohoo()

`

``

245

`+

with self.assertRaises(TypeError):

`

``

246

`+

woohoo(3, 5)

`

``

247

`+

with self.assertRaises(TypeError):

`

``

248

`+

woohoo(b=3)

`

``

249

+

``

250

`+

def test_recursive(self):

`

``

251

`+

depth = 0

`

``

252

`+

@contextmanager

`

``

253

`+

def woohoo():

`

``

254

`+

nonlocal depth

`

``

255

`+

before = depth

`

``

256

`+

depth += 1

`

``

257

`+

yield

`

``

258

`+

depth -= 1

`

``

259

`+

self.assertEqual(depth, before)

`

``

260

+

``

261

`+

@woohoo()

`

``

262

`+

def recursive():

`

``

263

`+

if depth < 10:

`

``

264

`+

recursive()

`

``

265

+

``

266

`+

recursive()

`

``

267

`+

self.assertEqual(depth, 0)

`

``

268

+

222

269

``

223

270

`class ClosingTestCase(unittest.TestCase):

`

224

271

``