| 103 | } |
| 104 | |
| 105 | fn clear(&mut self, out: &mut Vec<crate::PyObjectRef>) { |
| 106 | // Pop closure if present (equivalent to Py_CLEAR(func_closure)) |
| 107 | if let Some(closure) = self.closure.take() { |
| 108 | out.push(closure.into()); |
| 109 | } |
| 110 | |
| 111 | // Pop defaults and kwdefaults |
| 112 | if let Some(mut guard) = self.defaults_and_kwdefaults.try_lock() { |
| 113 | if let Some(defaults) = guard.0.take() { |
| 114 | out.push(defaults.into()); |
| 115 | } |
| 116 | if let Some(kwdefaults) = guard.1.take() { |
| 117 | out.push(kwdefaults.into()); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Clear annotations and annotate (Py_CLEAR) |
| 122 | if let Some(mut guard) = self.annotations.try_lock() |
| 123 | && let Some(annotations) = guard.take() |
| 124 | { |
| 125 | out.push(annotations.into()); |
| 126 | } |
| 127 | if let Some(mut guard) = self.annotate.try_lock() |
| 128 | && let Some(annotate) = guard.take() |
| 129 | { |
| 130 | out.push(annotate); |
| 131 | } |
| 132 | |
| 133 | // Clear module, doc, and type_params (Py_CLEAR) |
| 134 | if let Some(mut guard) = self.module.try_lock() { |
| 135 | let old_module = |
| 136 | core::mem::replace(&mut *guard, Context::genesis().none.to_owned().into()); |
| 137 | out.push(old_module); |
| 138 | } |
| 139 | if let Some(mut guard) = self.doc.try_lock() { |
| 140 | let old_doc = |
| 141 | core::mem::replace(&mut *guard, Context::genesis().none.to_owned().into()); |
| 142 | out.push(old_doc); |
| 143 | } |
| 144 | if let Some(mut guard) = self.type_params.try_lock() { |
| 145 | let old_type_params = |
| 146 | core::mem::replace(&mut *guard, Context::genesis().empty_tuple.to_owned()); |
| 147 | out.push(old_type_params.into()); |
| 148 | } |
| 149 | |
| 150 | // Replace name and qualname with empty string to break potential str subclass cycles |
| 151 | // name and qualname could be str subclasses, so they could have reference cycles |
| 152 | if let Some(mut guard) = self.name.try_lock() { |
| 153 | let old_name = core::mem::replace(&mut *guard, Context::genesis().empty_str.to_owned()); |
| 154 | out.push(old_name.into()); |
| 155 | } |
| 156 | if let Some(mut guard) = self.qualname.try_lock() { |
| 157 | let old_qualname = |
| 158 | core::mem::replace(&mut *guard, Context::genesis().empty_str.to_owned()); |
| 159 | out.push(old_qualname.into()); |
| 160 | } |
| 161 | |
| 162 | // Note: globals, builtins, code are NOT cleared (required to be non-NULL) |