Skip to content

Commit 7242fb4

Browse files
committed
Complete test_discard()
Some cleanups.
1 parent f3fd520 commit 7242fb4

3 files changed

Lines changed: 19 additions & 7 deletions

File tree

‎Doc/c-api/set.rst‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ See also the :c:func:`PyFrozenSet_New` function.
245245
246246
Create a :c:type:`PyFrozenSetWriter`.
247247
248-
If *size* is greater than zero, preallocate *size* items.
248+
If *size* is greater than zero, allocate an internal table big enough to
249+
store *size* items.
249250
250251
On error, set an exception and return ``NULL``.
251252

‎Lib/test/test_capi/test_set.py‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def check_add_method(self, add_method):
323323
items = self.create_items(self.PySet_MINSIZE * 2 - 1)
324324
table_size = self.compute_table_size(len(items))
325325
for prealloc in (0, len(items) // 2, len(items), len(items) * 10):
326-
with self.subTest(size=prealloc):
326+
with self.subTest(prealloc=prealloc):
327327
writer = self.create_writer()
328328
add = getattr(writer, add_method)
329329
for item in items:
@@ -356,7 +356,7 @@ def check_update_method(self, update_method):
356356
empty = ()
357357

358358
for prealloc in (0, total // 2, total, total * 10):
359-
with self.subTest(size=prealloc):
359+
with self.subTest(prealloc=prealloc):
360360
writer = self.create_writer(prealloc)
361361
update = getattr(writer, update_method)
362362
update(items + common)
@@ -412,10 +412,21 @@ class MyObject:
412412

413413
def test_discard(self):
414414
# Test PyFrozenSetWriter_Discard(): must not leak memory
415-
writer = self.create_writer()
416-
writer.update([2, 5, 7])
415+
416+
writer = self.create_writer(0)
417417
writer.discard()
418418

419+
for prealloc in (0, 3, 100):
420+
for items in (
421+
(), # empty
422+
[2, 5, 7], # use the small table
423+
range(100), # allocate a new table
424+
):
425+
with self.subTest(prealloc=prealloc, items=items):
426+
writer = self.create_writer(prealloc)
427+
writer.update(items)
428+
writer.discard()
429+
419430
@threading_helper.requires_working_threading()
420431
def test_thread(self):
421432
# PyFrozenSetWriter can be used by multiple threads: it's up to the

‎Objects/setobject.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3287,13 +3287,13 @@ PyObject*
32873287
PyFrozenSetWriter_Finish(PyFrozenSetWriter *writer)
32883288
{
32893289
assert(frozensetwriter_check_consistency(writer));
3290-
PySetObject *frozenset = _PySet_CAST(writer);
3290+
PyObject *frozenset = _PyObject_CAST(writer);
32913291

32923292
// Now that the frozenset is fully initialized, it's safe to track it
32933293
// in the garbage collector.
32943294
_PyObject_GC_TRACK(frozenset);
32953295

3296-
return _PyObject_CAST(frozenset);
3296+
return frozenset;
32973297
}
32983298

32993299

0 commit comments

Comments
 (0)