Skip to content

Commit

Permalink
Py_DECREF for owned traceback and type objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadeo authored and ijl committed Jun 9, 2023
1 parent 18b6562 commit 4550226
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ fn raise_dumps_exception_dynamic(err: &String) -> *mut PyObject {

if !cause_tp.is_null() {
PyException_SetCause(val, cause_val);
Py_DECREF(cause_tp);
}
if !cause_traceback.is_null() {
Py_DECREF(cause_traceback);
}

PyErr_Restore(tp, val, traceback);
Expand Down
15 changes: 15 additions & 0 deletions test/test_default.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import sys
import uuid

import pytest
Expand Down Expand Up @@ -260,3 +261,17 @@ def default(obj):

with pytest.raises(orjson.JSONEncodeError):
orjson.dumps(ref, default=default)

def test_reference_cleanup_default(self):
"""
references to encoded objects are cleaned up
"""
ref = Custom()

def default(obj):
raise TypeError

with pytest.raises(orjson.JSONEncodeError):
orjson.dumps(ref, default=default)

assert sys.getrefcount(ref) == 2 # one for ref, one for default

0 comments on commit 4550226

Please sign in to comment.