From 1bde97f77d1c099542e0118ccaa81fb454a0fefd Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Sat, 14 Oct 2017 12:18:16 +0300 Subject: [PATCH 01/17] init commit --- Modules/_asynciomodule.c | 4 ++-- Modules/_bz2module.c | 2 +- Modules/_hashopenssl.c | 4 ++-- Modules/_lzmamodule.c | 2 +- Objects/descrobject.c | 8 ++++---- Objects/funcobject.c | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 2c64c55e7e3476..9258d1fcabf73e 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -1336,12 +1336,12 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop) return -1; } - self->task_fut_waiter = NULL; + Py_CLEAR(self->task_fut_waiter); self->task_must_cancel = 0; self->task_log_destroy_pending = 1; Py_INCREF(coro); - self->task_coro = coro; + Py_XSETREF(self->task_coro, coro); if (task_call_step_soon(self, NULL)) { return -1; diff --git a/Modules/_bz2module.c b/Modules/_bz2module.c index dba0e19384ab98..0789b6179e52be 100644 --- a/Modules/_bz2module.c +++ b/Modules/_bz2module.c @@ -644,7 +644,7 @@ _bz2_BZ2Decompressor___init___impl(BZ2Decompressor *self) self->bzs_avail_in_real = 0; self->input_buffer = NULL; self->input_buffer_size = 0; - self->unused_data = PyBytes_FromStringAndSize(NULL, 0); + Py_XSETREF(self->unused_data, PyBytes_FromStringAndSize(NULL, 0)); if (self->unused_data == NULL) goto error; diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index 01f1671a8f5056..8ec2ebc7f4e7dc 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -368,8 +368,8 @@ EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds) return -1; } - self->name = name_obj; - Py_INCREF(self->name); + Py_INCREF(name_obj); + Py_XSETREF(self->name, name_obj); if (data_obj) { if (view.len >= HASHLIB_GIL_MINSIZE) { diff --git a/Modules/_lzmamodule.c b/Modules/_lzmamodule.c index fd3bbb80bce14e..5bcd088d772188 100644 --- a/Modules/_lzmamodule.c +++ b/Modules/_lzmamodule.c @@ -1173,7 +1173,7 @@ _lzma_LZMADecompressor___init___impl(Decompressor *self, int format, self->needs_input = 1; self->input_buffer = NULL; self->input_buffer_size = 0; - self->unused_data = PyBytes_FromStringAndSize(NULL, 0); + Py_XSETREF(self->unused_data, PyBytes_FromStringAndSize(NULL, 0)); if (self->unused_data == NULL) goto error; diff --git a/Objects/descrobject.c b/Objects/descrobject.c index 5dc27ef67278a4..19e339520b248f 100644 --- a/Objects/descrobject.c +++ b/Objects/descrobject.c @@ -1521,10 +1521,10 @@ property_init_impl(propertyobject *self, PyObject *fget, PyObject *fset, Py_XINCREF(fdel); Py_XINCREF(doc); - self->prop_get = fget; - self->prop_set = fset; - self->prop_del = fdel; - self->prop_doc = doc; + Py_XSETREF(self->prop_get, fget); + Py_XSETREF(self->prop_set, fset); + Py_XSETREF(self->prop_del, fdel); + Py_XSETREF(self->prop_doc, doc); self->getter_doc = 0; /* if no docstring given and the getter has one, use that one */ diff --git a/Objects/funcobject.c b/Objects/funcobject.c index d376f9cab90be5..241685d5b7bbf7 100644 --- a/Objects/funcobject.c +++ b/Objects/funcobject.c @@ -709,7 +709,7 @@ cm_init(PyObject *self, PyObject *args, PyObject *kwds) if (!PyArg_UnpackTuple(args, "classmethod", 1, 1, &callable)) return -1; Py_INCREF(callable); - cm->cm_callable = callable; + Py_XSETREF(cm->cm_callable, callable); return 0; } @@ -890,7 +890,7 @@ sm_init(PyObject *self, PyObject *args, PyObject *kwds) if (!PyArg_UnpackTuple(args, "staticmethod", 1, 1, &callable)) return -1; Py_INCREF(callable); - sm->sm_callable = callable; + Py_XSETREF(sm->sm_callable, callable); return 0; } From 1935a91bddb8864f0050a27f16cdb654dadf9024 Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Fri, 20 Oct 2017 13:40:04 +0300 Subject: [PATCH 02/17] add tests for bz2 and lzma --- Lib/test/test_bz2.py | 11 +++++++++++ Lib/test/test_lzma.py | 11 +++++++++++ 2 files changed, 22 insertions(+) diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py index 58dbf96d3c80e9..5e6f8b7434ff9d 100644 --- a/Lib/test/test_bz2.py +++ b/Lib/test/test_bz2.py @@ -13,6 +13,7 @@ import threading from test.support import unlink import _compression +import sys # Skip tests if the bz2 module doesn't exist. @@ -816,6 +817,16 @@ def test_failure(self): # Previously, a second call could crash due to internal inconsistency self.assertRaises(Exception, bzd.decompress, self.BAD_DATA * 30) + @support.refcount_test + def test_refleaks_in___init__(self): + gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount') + bzd = BZ2Decompressor.__new__(BZ2Decompressor) + refs_before = gettotalrefcount() + for i in range(100): + bzd.__init__() + self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10) + + class CompressDecompressTest(BaseTest): def testCompress(self): data = bz2.compress(self.TEXT) diff --git a/Lib/test/test_lzma.py b/Lib/test/test_lzma.py index d7a8576d512340..75a1bd4a32345a 100644 --- a/Lib/test/test_lzma.py +++ b/Lib/test/test_lzma.py @@ -4,6 +4,8 @@ import pathlib import pickle import random +import sys +from test import support import unittest from test.support import ( @@ -364,6 +366,15 @@ def test_pickle(self): with self.assertRaises(TypeError): pickle.dumps(LZMADecompressor(), proto) + @support.refcount_test + def test_refleaks_in___init__(self): + gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount') + lzd = LZMADecompressor.__new__(LZMADecompressor) + refs_before = gettotalrefcount() + for i in range(100): + lzd.__init__() + self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10) + class CompressDecompressFunctionTestCase(unittest.TestCase): From baa612cde9fbb0275b48b6d5a1fb61aa142be6cb Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Fri, 20 Oct 2017 13:54:32 +0300 Subject: [PATCH 03/17] added a test for property --- Lib/test/test_property.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Lib/test/test_property.py b/Lib/test/test_property.py index 26b7d5283a2220..f6f8f5ed0e45ee 100644 --- a/Lib/test/test_property.py +++ b/Lib/test/test_property.py @@ -3,6 +3,7 @@ import sys import unittest +from test import support class PropertyBase(Exception): pass @@ -173,6 +174,16 @@ def spam(self): sub.__class__.spam.__doc__ = 'Spam' self.assertEqual(sub.__class__.spam.__doc__, 'Spam') + @support.refcount_test + def test_refleaks_in___init__(self): + gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount') + fake_prop = property('fget', 'fset', 'fdel', 'doc') + refs_before = gettotalrefcount() + for i in range(100): + fake_prop.__init__('fget', 'fset', 'fdel', 'doc') + self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10) + + # Issue 5890: subclasses of property do not preserve method __doc__ strings class PropertySub(property): """This is a subclass of property""" From 2553833a469976d9884e34251a8a886ac455fdc7 Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Fri, 20 Oct 2017 14:12:41 +0300 Subject: [PATCH 04/17] added tests for classmethod and staticmethod --- Lib/test/test_descr.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index ced25f3fc44244..a7137885919521 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -1559,6 +1559,15 @@ def f(cls, arg): return (cls, arg) del cm.x self.assertNotHasAttr(cm, "x") + @support.refcount_test + def test_refleaks_in_classmethod___init__(self): + gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount') + cm = classmethod(None) + refs_before = gettotalrefcount() + for i in range(100): + cm.__init__(None) + self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10) + @support.impl_detail("the module 'xxsubtype' is internal") def test_classmethods_in_c(self): # Testing C-based class methods... @@ -1614,6 +1623,15 @@ class D(C): del sm.x self.assertNotHasAttr(sm, "x") + @support.refcount_test + def test_refleaks_in_staticmethod___init__(self): + gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount') + sm = staticmethod(None) + refs_before = gettotalrefcount() + for i in range(100): + sm.__init__(None) + self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10) + @support.impl_detail("the module 'xxsubtype' is internal") def test_staticmethods_in_c(self): # Testing C-based static methods... From 7d428834a7f1abacb2b0d7f45531b30147bc304e Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Fri, 20 Oct 2017 14:21:06 +0300 Subject: [PATCH 05/17] simplified the lzma and bz2 tests --- Lib/test/test_bz2.py | 2 +- Lib/test/test_lzma.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_bz2.py b/Lib/test/test_bz2.py index 5e6f8b7434ff9d..7e6d20a5d1e54c 100644 --- a/Lib/test/test_bz2.py +++ b/Lib/test/test_bz2.py @@ -820,7 +820,7 @@ def test_failure(self): @support.refcount_test def test_refleaks_in___init__(self): gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount') - bzd = BZ2Decompressor.__new__(BZ2Decompressor) + bzd = BZ2Decompressor() refs_before = gettotalrefcount() for i in range(100): bzd.__init__() diff --git a/Lib/test/test_lzma.py b/Lib/test/test_lzma.py index 75a1bd4a32345a..7209e94c47fd2e 100644 --- a/Lib/test/test_lzma.py +++ b/Lib/test/test_lzma.py @@ -369,7 +369,7 @@ def test_pickle(self): @support.refcount_test def test_refleaks_in___init__(self): gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount') - lzd = LZMADecompressor.__new__(LZMADecompressor) + lzd = LZMADecompressor() refs_before = gettotalrefcount() for i in range(100): lzd.__init__() From b4cf5d0d4886ce32e42e2edfb1841874dc8668b3 Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Fri, 20 Oct 2017 17:05:17 +0300 Subject: [PATCH 06/17] added a test for hash --- Lib/test/test_hashlib.py | 9 +++++++++ Lib/test/test_lzma.py | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py index 90e6a563a72c72..46f074b0564416 100644 --- a/Lib/test/test_hashlib.py +++ b/Lib/test/test_hashlib.py @@ -161,6 +161,15 @@ def hash_constructors(self): constructors = self.constructors_to_test.values() return itertools.chain.from_iterable(constructors) + @support.refcount_test + def test_refleaks_in_hash___init__(self): + gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount') + sha1_hash = c_hashlib.new('sha1') + refs_before = gettotalrefcount() + for i in range(100): + sha1_hash.__init__(name='sha1') + self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10) + def test_hash_array(self): a = array.array("b", range(10)) for cons in self.hash_constructors: diff --git a/Lib/test/test_lzma.py b/Lib/test/test_lzma.py index 7209e94c47fd2e..3dc2c1e7e3b779 100644 --- a/Lib/test/test_lzma.py +++ b/Lib/test/test_lzma.py @@ -367,7 +367,7 @@ def test_pickle(self): pickle.dumps(LZMADecompressor(), proto) @support.refcount_test - def test_refleaks_in___init__(self): + def test_refleaks_in_decompressor___init__(self): gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount') lzd = LZMADecompressor() refs_before = gettotalrefcount() From 9575b4427853880e9f7613a0c0598be3ac51f097 Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Fri, 20 Oct 2017 17:09:14 +0300 Subject: [PATCH 07/17] add a comment to the hash test --- Lib/test/test_hashlib.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py index 46f074b0564416..4585215ee5893f 100644 --- a/Lib/test/test_hashlib.py +++ b/Lib/test/test_hashlib.py @@ -163,6 +163,8 @@ def hash_constructors(self): @support.refcount_test def test_refleaks_in_hash___init__(self): + # This test is relevant only in case of compiling with + # HASH_OBJ_CONSTRUCTOR. gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount') sha1_hash = c_hashlib.new('sha1') refs_before = gettotalrefcount() From 15c8f416b47d469329f1c142cbb8bc7fce4a49a2 Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Fri, 20 Oct 2017 17:59:14 +0300 Subject: [PATCH 08/17] remove the comment in the hash test --- Lib/test/test_hashlib.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py index 4585215ee5893f..46f074b0564416 100644 --- a/Lib/test/test_hashlib.py +++ b/Lib/test/test_hashlib.py @@ -163,8 +163,6 @@ def hash_constructors(self): @support.refcount_test def test_refleaks_in_hash___init__(self): - # This test is relevant only in case of compiling with - # HASH_OBJ_CONSTRUCTOR. gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount') sha1_hash = c_hashlib.new('sha1') refs_before = gettotalrefcount() From 004bfa5fc71f57c2cc1001ac234bfb250905ba85 Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Fri, 20 Oct 2017 18:41:29 +0300 Subject: [PATCH 09/17] added a test for asyncio task --- Lib/test/test_asyncio/test_tasks.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index e23963a62215f5..7c6af75dac15eb 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -2196,6 +2196,20 @@ class CTask_CFuture_Tests(BaseTaskTests, test_utils.TestCase): Task = getattr(tasks, '_CTask', None) Future = getattr(futures, '_CFuture', None) + @support.refcount_test + def test_refleaks_in_task___init__(self): + gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount') + @asyncio.coroutine + def coro(): + pass + with support.swap_attr(self.loop, 'call_soon', lambda _step: None): + task = self.new_task(self.loop, coro()) + refs_before = gettotalrefcount() + for i in range(100): + task.__init__(coro(), loop=self.loop) + self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, + delta=10) + @unittest.skipUnless(hasattr(futures, '_CFuture'), 'requires the C _asyncio module') From 921f49da727c9a842ef7218b8184d9f34a8e732d Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Fri, 20 Oct 2017 23:26:48 +0300 Subject: [PATCH 10/17] prevent destroying the task while it is pending --- Lib/test/test_asyncio/test_tasks.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 7c6af75dac15eb..92ca68709f8ffe 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -2209,6 +2209,9 @@ def coro(): task.__init__(coro(), loop=self.loop) self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10) + # Prevent destroying the task while it is pending. + self.loop.call_soon(task._step) + self.loop.run_until_complete(task) @unittest.skipUnless(hasattr(futures, '_CFuture'), From 1608ab68ddf125beb8b48806e3bd0efe08468801 Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Fri, 20 Oct 2017 23:32:53 +0300 Subject: [PATCH 11/17] slightly simplify the test --- Lib/test/test_hashlib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py index 46f074b0564416..50c5a4383eff81 100644 --- a/Lib/test/test_hashlib.py +++ b/Lib/test/test_hashlib.py @@ -167,7 +167,7 @@ def test_refleaks_in_hash___init__(self): sha1_hash = c_hashlib.new('sha1') refs_before = gettotalrefcount() for i in range(100): - sha1_hash.__init__(name='sha1') + sha1_hash.__init__('sha1') self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10) def test_hash_array(self): From 6a00fc2fda89b1ef4690a6dbca3663a6940a0981 Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Fri, 20 Oct 2017 23:42:30 +0300 Subject: [PATCH 12/17] add a comment to clarify the asyncio test --- Lib/test/test_asyncio/test_tasks.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 92ca68709f8ffe..e88afe1b14f72c 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -2202,6 +2202,8 @@ def test_refleaks_in_task___init__(self): @asyncio.coroutine def coro(): pass + # Overwrite call_soon() to prevent increasing refcounts that are + # irrelevant to the test. with support.swap_attr(self.loop, 'call_soon', lambda _step: None): task = self.new_task(self.loop, coro()) refs_before = gettotalrefcount() From 20bef1bdf5b37218dc8e8ec3960b8c4fed1aef3c Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Fri, 9 Feb 2018 21:43:48 +0900 Subject: [PATCH 13/17] Add NEWS entry --- .../next/Library/2018-02-09-21-41-56.bpo-31787.owSZ2t.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2018-02-09-21-41-56.bpo-31787.owSZ2t.rst diff --git a/Misc/NEWS.d/next/Library/2018-02-09-21-41-56.bpo-31787.owSZ2t.rst b/Misc/NEWS.d/next/Library/2018-02-09-21-41-56.bpo-31787.owSZ2t.rst new file mode 100644 index 00000000000000..f0cde59d740f06 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-02-09-21-41-56.bpo-31787.owSZ2t.rst @@ -0,0 +1,2 @@ +Fixed refleaks of ``__init__()`` methods in various modules. +(Contributed by Oren Milman) From b3e10edf34402269f1f543ac98c6e0208c027818 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Fri, 9 Feb 2018 22:18:25 +0900 Subject: [PATCH 14/17] Fix test --- Lib/test/test_asyncio/test_tasks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index fcb1ae07e00906..f33749b80f3236 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -2381,7 +2381,7 @@ def coro(): pass # Overwrite call_soon() to prevent increasing refcounts that are # irrelevant to the test. - with support.swap_attr(self.loop, 'call_soon', lambda _step: None): + with support.swap_attr(self.loop, 'call_soon', lambda _step, context=None: None): task = self.new_task(self.loop, coro()) refs_before = gettotalrefcount() for i in range(100): From f3f344ae444ee0470121f0a009639534236c8e27 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Sat, 10 Feb 2018 00:12:54 +0900 Subject: [PATCH 15/17] Fix more refleaks --- Modules/_asynciomodule.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 641f87dc5a92e4..99576d6b4cae4c 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -492,10 +492,9 @@ future_init(FutureObj *fut, PyObject *loop) } } - fut->fut_callback0 = NULL; - fut->fut_context0 = NULL; - fut->fut_callbacks = NULL; - + Py_CLEAR(fut->fut_callback0); + Py_CLEAR(fut->fut_context0); + Py_CLEAR(fut->fut_callbacks); return 0; } From be14ca26f8fa1a7c4246b503a9ba80ee44196b39 Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 13 Feb 2018 15:09:29 +0900 Subject: [PATCH 16/17] Overriding loop.call_soon() is not allowed --- Lib/test/test_asyncio/test_tasks.py | 17 ++++++----------- Modules/_asynciomodule.c | 16 ++++++++++------ 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index f33749b80f3236..e5334c6ff914c6 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -2379,18 +2379,13 @@ def test_refleaks_in_task___init__(self): @asyncio.coroutine def coro(): pass - # Overwrite call_soon() to prevent increasing refcounts that are - # irrelevant to the test. - with support.swap_attr(self.loop, 'call_soon', lambda _step, context=None: None): - task = self.new_task(self.loop, coro()) - refs_before = gettotalrefcount() - for i in range(100): - task.__init__(coro(), loop=self.loop) - self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, - delta=10) - # Prevent destroying the task while it is pending. - self.loop.call_soon(task._step) + task = self.new_task(self.loop, coro()) self.loop.run_until_complete(task) + refs_before = gettotalrefcount() + for i in range(100): + task.__init__(coro(), loop=self.loop) + self.loop.run_until_complete(task) + self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10) @unittest.skipUnless(hasattr(futures, '_CFuture') and diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 99576d6b4cae4c..0919f29a701358 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -458,6 +458,8 @@ future_schedule_callbacks(FutureObj *fut) return 0; } +static int FutureObj_clear(FutureObj *fut); + static int future_init(FutureObj *fut, PyObject *loop) { @@ -465,6 +467,11 @@ future_init(FutureObj *fut, PyObject *loop) int is_true; _Py_IDENTIFIER(get_debug); + (void)FutureObj_clear(fut); + fut->fut_state = STATE_PENDING; + fut->fut_log_tb = 0; + fut->fut_blocking = 0; + if (loop == Py_None) { loop = get_event_loop(); if (loop == NULL) { @@ -474,7 +481,7 @@ future_init(FutureObj *fut, PyObject *loop) else { Py_INCREF(loop); } - Py_XSETREF(fut->fut_loop, loop); + fut->fut_loop = loop; res = _PyObject_CallMethodId(fut->fut_loop, &PyId_get_debug, NULL); if (res == NULL) { @@ -486,15 +493,12 @@ future_init(FutureObj *fut, PyObject *loop) return -1; } if (is_true) { - Py_XSETREF(fut->fut_source_tb, _PyObject_CallNoArg(traceback_extract_stack)); + fut->fut_source_tb = _PyObject_CallNoArg(traceback_extract_stack); if (fut->fut_source_tb == NULL) { return -1; } } - Py_CLEAR(fut->fut_callback0); - Py_CLEAR(fut->fut_context0); - Py_CLEAR(fut->fut_callbacks); return 0; } @@ -1937,7 +1941,7 @@ _asyncio_Task___init___impl(TaskObj *self, PyObject *coro, PyObject *loop) return -1; } - self->task_context = PyContext_CopyCurrent(); + Py_XSETREF(self->task_context, PyContext_CopyCurrent()); if (self->task_context == NULL) { return -1; } From 7ac37a445b3281bc1f1b585b83c624e01ab14b5d Mon Sep 17 00:00:00 2001 From: INADA Naoki Date: Tue, 13 Feb 2018 18:45:45 +0900 Subject: [PATCH 17/17] Fix test_asyncio.test_tasks fail --- Modules/_asynciomodule.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index 0919f29a701358..d66cc4cf0a5f5f 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -458,7 +458,6 @@ future_schedule_callbacks(FutureObj *fut) return 0; } -static int FutureObj_clear(FutureObj *fut); static int future_init(FutureObj *fut, PyObject *loop) @@ -467,7 +466,15 @@ future_init(FutureObj *fut, PyObject *loop) int is_true; _Py_IDENTIFIER(get_debug); - (void)FutureObj_clear(fut); + // Same to FutureObj_clear() but not clearing fut->dict + Py_CLEAR(fut->fut_loop); + Py_CLEAR(fut->fut_callback0); + Py_CLEAR(fut->fut_context0); + Py_CLEAR(fut->fut_callbacks); + Py_CLEAR(fut->fut_result); + Py_CLEAR(fut->fut_exception); + Py_CLEAR(fut->fut_source_tb); + fut->fut_state = STATE_PENDING; fut->fut_log_tb = 0; fut->fut_blocking = 0;