Skip to content

Instantly share code, notes, and snippets.

@AndreLouisCaron
Created June 2, 2020 00:56
Show Gist options
  • Save AndreLouisCaron/a18ae434e2eaf2866ffa4a24f693b2e8 to your computer and use it in GitHub Desktop.
Save AndreLouisCaron/a18ae434e2eaf2866ffa4a24f693b2e8 to your computer and use it in GitHub Desktop.
Demonstrate leaks in ujson upgrade
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)
# -*- 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