Skip to content

Commit 94e8e22

Browse files
committed
clean up GC traversal for some top-level types
1 parent 191187a commit 94e8e22

File tree

7 files changed

+55
-49
lines changed

7 files changed

+55
-49
lines changed

src/py_gc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ macro_rules! impl_py_gc_traverse {
6464
}
6565
}
6666
};
67-
($name:ty { $($fields:ident),* }) => {
67+
($name:ty { $($fields:ident),* $(,)? }) => {
6868
impl crate::py_gc::PyGcTraverse for $name {
6969
fn py_gc_traverse(&self, visit: &pyo3::PyVisit<'_>) -> Result<(), pyo3::PyTraverseError> {
7070
$(self.$fields.py_gc_traverse(visit)?;)*

src/serializers/extra.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,14 @@ pub(crate) struct ExtraOwned {
261261
exclude: Option<Py<PyAny>>,
262262
}
263263

264+
impl_py_gc_traverse!(ExtraOwned {
265+
model,
266+
fallback,
267+
context,
268+
include,
269+
exclude,
270+
});
271+
264272
#[derive(Clone)]
265273
enum FieldNameOwned {
266274
Root,

src/serializers/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ pub struct SchemaSerializer {
4848
py_config: Option<Py<PyDict>>,
4949
}
5050

51+
impl_py_gc_traverse!(SchemaSerializer {
52+
serializer,
53+
definitions,
54+
py_schema,
55+
py_config,
56+
});
57+
5158
#[pymethods]
5259
impl SchemaSerializer {
5360
#[new]
@@ -186,13 +193,7 @@ impl SchemaSerializer {
186193
}
187194

188195
fn __traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError> {
189-
visit.call(&self.py_schema)?;
190-
if let Some(ref py_config) = self.py_config {
191-
visit.call(py_config)?;
192-
}
193-
self.serializer.py_gc_traverse(&visit)?;
194-
self.definitions.py_gc_traverse(&visit)?;
195-
Ok(())
196+
self.py_gc_traverse(&visit)
196197
}
197198
}
198199

src/serializers/type_serializers/function.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use pyo3::PyTraverseError;
1111
use pyo3::types::PyString;
1212

1313
use crate::definitions::DefinitionsBuilder;
14+
use crate::py_gc::PyGcTraverse;
1415
use crate::serializers::SerializationState;
1516
use crate::tools::SchemaDict;
1617
use crate::tools::{function_name, py_err, py_error_type};
@@ -437,6 +438,11 @@ pub(crate) struct SerializationCallable {
437438
filter: AnyFilter,
438439
}
439440

441+
impl_py_gc_traverse!(SerializationCallable {
442+
serializer,
443+
extra_owned
444+
});
445+
440446
impl SerializationCallable {
441447
pub fn new(serializer: &Arc<CombinedSerializer>, state: &SerializationState<'_, '_>) -> Self {
442448
Self {
@@ -447,16 +453,7 @@ impl SerializationCallable {
447453
}
448454

449455
fn __traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError> {
450-
if let Some(model) = &self.extra_owned.model {
451-
visit.call(model)?;
452-
}
453-
if let Some(fallback) = &self.extra_owned.fallback {
454-
visit.call(fallback)?;
455-
}
456-
if let Some(context) = &self.extra_owned.context {
457-
visit.call(context)?;
458-
}
459-
Ok(())
456+
self.py_gc_traverse(&visit)
460457
}
461458

462459
fn __clear__(&mut self) {
@@ -542,6 +539,12 @@ struct SerializationInfo {
542539
serialize_as_any: bool,
543540
}
544541

542+
impl_py_gc_traverse!(SerializationInfo {
543+
include,
544+
exclude,
545+
context
546+
});
547+
545548
impl SerializationInfo {
546549
fn new(state: &SerializationState<'_, '_>, is_field_serializer: bool) -> PyResult<Self> {
547550
let extra = &state.extra;
@@ -584,16 +587,7 @@ impl SerializationInfo {
584587
}
585588

586589
fn __traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError> {
587-
if let Some(include) = &self.include {
588-
visit.call(include)?;
589-
}
590-
if let Some(exclude) = &self.exclude {
591-
visit.call(exclude)?;
592-
}
593-
if let Some(context) = &self.context {
594-
visit.call(context)?;
595-
}
596-
Ok(())
590+
self.py_gc_traverse(&visit)
597591
}
598592

599593
fn __clear__(&mut self) {

src/serializers/type_serializers/generator.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use pyo3::PyTraverseError;
1111
use serde::ser::SerializeSeq;
1212

1313
use crate::definitions::DefinitionsBuilder;
14+
use crate::py_gc::PyGcTraverse;
1415
use crate::serializers::SerializationState;
1516
use crate::tools::SchemaDict;
1617

@@ -144,6 +145,12 @@ pub(crate) struct SerializationIterator {
144145
filter: SchemaFilter<usize>,
145146
}
146147

148+
impl_py_gc_traverse!(SerializationIterator {
149+
iterator,
150+
item_serializer,
151+
extra_owned,
152+
});
153+
147154
impl SerializationIterator {
148155
pub fn new(
149156
py_iter: &Bound<'_, PyIterator>,
@@ -161,16 +168,7 @@ impl SerializationIterator {
161168
}
162169

163170
fn __traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError> {
164-
if let Some(model) = &self.extra_owned.model {
165-
visit.call(model)?;
166-
}
167-
if let Some(fallback) = &self.extra_owned.fallback {
168-
visit.call(fallback)?;
169-
}
170-
if let Some(context) = &self.extra_owned.context {
171-
visit.call(context)?;
172-
}
173-
Ok(())
171+
self.py_gc_traverse(&visit)
174172
}
175173

176174
fn __clear__(&mut self) {

src/validators/function.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,13 @@ pub struct ValidationInfo {
536536
mode: InputType,
537537
}
538538

539+
impl_py_gc_traverse!(ValidationInfo {
540+
config,
541+
context,
542+
data,
543+
field_name
544+
});
545+
539546
impl ValidationInfo {
540547
fn new(py: Python, extra: &Extra<'_, '_>, config: &Py<PyAny>, field_name: Option<Py<PyString>>) -> Self {
541548
Self {
@@ -548,11 +555,7 @@ impl ValidationInfo {
548555
}
549556

550557
fn __traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError> {
551-
visit.call(&self.config)?;
552-
if let Some(context) = &self.context {
553-
visit.call(context)?;
554-
}
555-
Ok(())
558+
self.py_gc_traverse(&visit)
556559
}
557560

558561
fn __clear__(&mut self) {

src/validators/mod.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ pub struct SchemaValidator {
121121
cache_str: StringCacheMode,
122122
}
123123

124+
impl_py_gc_traverse!(SchemaValidator {
125+
validator,
126+
definitions,
127+
py_schema,
128+
py_config,
129+
});
130+
124131
#[pymethods]
125132
impl SchemaValidator {
126133
#[new]
@@ -403,12 +410,7 @@ impl SchemaValidator {
403410
}
404411

405412
fn __traverse__(&self, visit: PyVisit<'_>) -> Result<(), PyTraverseError> {
406-
self.validator.py_gc_traverse(&visit)?;
407-
visit.call(&self.py_schema)?;
408-
if let Some(ref py_config) = self.py_config {
409-
visit.call(py_config)?;
410-
}
411-
Ok(())
413+
self.py_gc_traverse(&visit)
412414
}
413415
}
414416

0 commit comments

Comments
 (0)