Whole file
mina86/pygtrie
The author described this change as “Fix StringTrie.copy ignoring separator and improve PrefixSet.copy”. It counts as a record because the check below fails on the code as it stood at fdb6991dd and passes on e29637950, with nothing else changed between the two runs.
Projectmina86/pygtrie
Fix saved2019-05-27
Sharing licenceApache-2.0 · LICENSE
Change size+9 −4
What the code was meant to do, written into the code itself as a save note
Fix StringTrie.copy ignoring separator and improve PrefixSet.copy
The change
| 1448 | 1448 | trie[key] = value | |
| 1449 | 1449 | return trie | |
| 1450 | 1450 | ||
| 1451 | + | def copy(self): | |
| 1452 | + | return self.__class__(self, separator=self._separator) | |
| 1453 | + | ||
| 1451 | 1454 | def _path_from_key(self, key): | |
| 1452 | 1455 | return key.split(self._separator) | |
| 1453 | 1456 | ||
| ⋯ | |||
| 1468 | 1471 | behaviour for element deletion. | |
| 1469 | 1472 | """ | |
| 1470 | 1473 | ||
| 1471 | - | def __init__(self, iterable=None, factory=Trie, **kwargs): | |
| 1474 | + | def __init__(self, iterable=(), factory=Trie, **kwargs): | |
| 1472 | 1475 | """Initialises the prefix set. | |
| 1473 | 1476 | ||
| 1474 | 1477 | Args: | |
| ⋯ | |||
| 1478 | 1481 | kwargs: Additional keyword arguments passed to the factory function. | |
| 1479 | 1482 | """ | |
| 1480 | 1483 | trie = factory(**kwargs) | |
| 1481 | - | if iterable: | |
| 1482 | - | trie.update((key, True) for key in iterable) | |
| 1484 | + | for key in iterable: | |
| 1485 | + | trie[key:] = True | |
| 1483 | 1486 | self._trie = trie | |
| 1484 | 1487 | ||
| 1485 | 1488 | def copy(self): | |
| 1486 | 1489 | """Returns a copy of the prefix set.""" | |
| 1487 | - | return self.__class__(self._trie, factory=self._trie.__class__) | |
| 1490 | + | cpy = super(PrefixSet, self).__new__(type(self)) | |
| 1491 | + | cpy._trie = self._trie.copy() # pylint: disable=protected-access | |
| 1492 | + | return cpy | |
| 1488 | 1493 | ||
| 1489 | 1494 | def clear(self): | |
| 1490 | 1495 | """Removes all keys from the set.""" | |
The check that tells the two apart
fail→pass·test.py::StringTrieTestCase::test_basics_CopyOfTrie
Check file test.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.
Origin and history
The code before itfdb6991dd4ae8a15ba9fbc0266ea0dfdb8a6d75a
Broken version dated2019-05-27
Modulepygtrie
Units changedPrefixSet, StringTrie
Fingerprintb5e4b707b43e65b4
Checked2026-08-18 by goldset/0.1
Every field above is generated by our program. None of it is written by hand.
Other bugs found in mina86/pygtrie
- 2020-02-26_NoChildren
- 2019-07-17PrefixSet
- 2018-08-10PrefixSet