Skip to content

Commit 617d086

Browse files
author
ornariece
committed
garbage control for context object
1 parent c40aa4b commit 617d086

File tree

4 files changed

+92
-3
lines changed

4 files changed

+92
-3
lines changed

src/serializers/extra.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,11 @@ pub(crate) struct ExtraOwned {
183183
config: SerializationConfig,
184184
rec_guard: SerRecursionState,
185185
check: SerCheck,
186-
model: Option<PyObject>,
186+
pub model: Option<PyObject>,
187187
field_name: Option<String>,
188188
serialize_unknown: bool,
189-
fallback: Option<PyObject>,
190-
context: Option<PyObject>,
189+
pub fallback: Option<PyObject>,
190+
pub context: Option<PyObject>,
191191
}
192192

193193
impl ExtraOwned {

src/serializers/type_serializers/function.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use std::borrow::Cow;
22

33
use pyo3::exceptions::{PyAttributeError, PyRecursionError, PyRuntimeError};
4+
use pyo3::gc::PyVisit;
45
use pyo3::intern;
56
use pyo3::prelude::*;
67
use pyo3::types::PyDict;
8+
use pyo3::PyTraverseError;
79

810
use pyo3::types::PyString;
911

@@ -440,6 +442,33 @@ impl SerializationCallable {
440442
exclude: exclude.map(|v| v.into_py(py)),
441443
}
442444
}
445+
446+
fn __traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError> {
447+
if let Some(include) = &self.include {
448+
visit.call(include)?;
449+
}
450+
if let Some(exclude) = &self.exclude {
451+
visit.call(exclude)?;
452+
}
453+
if let Some(model) = &self.extra_owned.model {
454+
visit.call(model)?;
455+
}
456+
if let Some(fallback) = &self.extra_owned.fallback {
457+
visit.call(fallback)?;
458+
}
459+
if let Some(context) = &self.extra_owned.context {
460+
visit.call(context)?;
461+
}
462+
Ok(())
463+
}
464+
465+
fn __clear__(&mut self) {
466+
self.include = None;
467+
self.exclude = None;
468+
self.extra_owned.model = None;
469+
self.extra_owned.fallback = None;
470+
self.extra_owned.context = None;
471+
}
443472
}
444473

445474
#[pymethods]
@@ -545,6 +574,25 @@ impl SerializationInfo {
545574
})
546575
}
547576
}
577+
578+
fn __traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError> {
579+
if let Some(include) = &self.include {
580+
visit.call(include)?;
581+
}
582+
if let Some(exclude) = &self.exclude {
583+
visit.call(exclude)?;
584+
}
585+
if let Some(context) = &self.context {
586+
visit.call(context)?;
587+
}
588+
Ok(())
589+
}
590+
591+
fn __clear__(&mut self) {
592+
self.include = None;
593+
self.exclude = None;
594+
self.context = None;
595+
}
548596
}
549597

550598
#[pymethods]

src/serializers/type_serializers/generator.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use std::borrow::Cow;
22

3+
use pyo3::gc::PyVisit;
34
use pyo3::intern;
45
use pyo3::prelude::*;
56
use pyo3::types::{PyDict, PyIterator};
7+
use pyo3::PyTraverseError;
68

79
use serde::ser::SerializeSeq;
810

@@ -173,6 +175,33 @@ impl SerializationIterator {
173175
exclude: exclude.map(|v| v.into_py(py)),
174176
}
175177
}
178+
179+
fn __traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError> {
180+
if let Some(include) = &self.include {
181+
visit.call(include)?;
182+
}
183+
if let Some(exclude) = &self.exclude {
184+
visit.call(exclude)?;
185+
}
186+
if let Some(model) = &self.extra_owned.model {
187+
visit.call(model)?;
188+
}
189+
if let Some(fallback) = &self.extra_owned.fallback {
190+
visit.call(fallback)?;
191+
}
192+
if let Some(context) = &self.extra_owned.context {
193+
visit.call(context)?;
194+
}
195+
Ok(())
196+
}
197+
198+
fn __clear__(&mut self) {
199+
self.include = None;
200+
self.exclude = None;
201+
self.extra_owned.model = None;
202+
self.extra_owned.fallback = None;
203+
self.extra_owned.context = None;
204+
}
176205
}
177206

178207
#[pymethods]

src/validators/function.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,18 @@ impl ValidationInfo {
480480
mode: extra.input_type,
481481
}
482482
}
483+
484+
fn __traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError> {
485+
visit.call(&self.config)?;
486+
if let Some(context) = &self.context {
487+
visit.call(context)?;
488+
}
489+
Ok(())
490+
}
491+
492+
fn __clear__(&mut self) {
493+
self.context = None;
494+
}
483495
}
484496

485497
#[pymethods]

0 commit comments

Comments
 (0)