Get tracked objects (for gc.get_objects) If generation is None, returns all tracked objects. If generation is Some(n), returns objects in generation n only.
(&self, generation: Option<i32>)
| 328 | /// If generation is None, returns all tracked objects. |
| 329 | /// If generation is Some(n), returns objects in generation n only. |
| 330 | pub fn get_objects(&self, generation: Option<i32>) -> Vec<PyObjectRef> { |
| 331 | fn collect_from_list( |
| 332 | list: &LinkedList<GcLink, PyObject>, |
| 333 | ) -> impl Iterator<Item = PyObjectRef> + '_ { |
| 334 | list.iter().filter_map(|obj| obj.try_to_owned()) |
| 335 | } |
| 336 | |
| 337 | match generation { |
| 338 | None => { |
| 339 | // Return all tracked objects from all generations + permanent |
| 340 | let mut result = Vec::new(); |
| 341 | for gen_list in &self.generation_lists { |
| 342 | result.extend(collect_from_list(&gen_list.read())); |
| 343 | } |
| 344 | result.extend(collect_from_list(&self.permanent_list.read())); |
| 345 | result |
| 346 | } |
| 347 | Some(g) if (0..=2).contains(&g) => { |
| 348 | let guard = self.generation_lists[g as usize].read(); |
| 349 | collect_from_list(&guard).collect() |
| 350 | } |
| 351 | _ => Vec::new(), |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | /// Check if automatic GC should run and run it if needed. |
| 356 | /// Called after object allocation. |