Created
June 2, 2020 00:56
-
-
Save AndreLouisCaron/a18ae434e2eaf2866ffa4a24f693b2e8 to your computer and use it in GitHub Desktop.
Demonstrate leaks in ujson upgrade
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ujson | |
import sys | |
def test_does_not_leak_dictionary_values(): | |
import gc | |
gc.collect() | |
value = ["abc"] | |
data = {"1": value} | |
ref_count = sys.getrefcount(value) | |
ujson.dumps(data) | |
assert ref_count == sys.getrefcount(value) | |
def test_does_not_leak_dictionary_keys(): | |
import gc | |
gc.collect() | |
key1 = "1" | |
key2 = "1" | |
value1 = ["abc"] | |
value2 = [1,2,3] | |
data = {key1: value1, key2: value2} | |
ref_count1 = sys.getrefcount(key1) | |
ref_count2 = sys.getrefcount(key2) | |
ujson.dumps(data) | |
assert ref_count1 == sys.getrefcount(key1) | |
assert ref_count2 == sys.getrefcount(key2) | |
def test_does_not_leak_dictionary_string_key(): | |
import gc | |
gc.collect() | |
key1 = "1" | |
value1 = 1 | |
data = {key1: value1} | |
ref_count1 = sys.getrefcount(key1) | |
ujson.dumps(data) | |
assert ref_count1 == sys.getrefcount(key1) | |
def test_does_not_leak_dictionary_tuple_key(): | |
import gc | |
gc.collect() | |
key1 = ("a",) | |
value1 = 1 | |
data = {key1: value1} | |
ref_count1 = sys.getrefcount(key1) | |
ujson.dumps(data) | |
assert ref_count1 == sys.getrefcount(key1) | |
def test_does_not_leak_dictionary_bytes_key(): | |
import gc | |
gc.collect() | |
key1 = b"1" | |
value1 = 1 | |
data = {key1: value1} | |
ref_count1 = sys.getrefcount(key1) | |
ujson.dumps(data) | |
assert ref_count1 == sys.getrefcount(key1) | |
def test_does_not_leak_dictionary_None_key(): | |
import gc | |
gc.collect() | |
key1 = None | |
value1 = 1 | |
data = {key1: value1} | |
ref_count1 = sys.getrefcount(key1) | |
ujson.dumps(data) | |
assert ref_count1 == sys.getrefcount(key1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
[tox] | |
skipsdist = true | |
envlist = | |
a | |
b | |
c | |
[testenv] | |
basepython = python2.7 | |
commands = | |
pytest -s -vv test_leaks.py | |
[testenv:a] | |
deps = | |
pytest | |
ujson==1.35 | |
[testenv:b] | |
deps = | |
pytest | |
ujson==2.0.1 | |
[testenv:c] | |
deps = | |
pytest | |
ujson==2.0.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment