##// END OF EJS Templates
rust-cpython: mark all PyLeaked methods as unsafe...
Yuya Nishihara -
r44668:71457ff6 default draft
parent child Browse files
Show More
@@ -1,633 +1,664 b''
1 1 // ref_sharing.rs
2 2 //
3 3 // Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
4 4 //
5 5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 6 // of this software and associated documentation files (the "Software"), to
7 7 // deal in the Software without restriction, including without limitation the
8 8 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 9 // sell copies of the Software, and to permit persons to whom the Software is
10 10 // furnished to do so, subject to the following conditions:
11 11 //
12 12 // The above copyright notice and this permission notice shall be included in
13 13 // all copies or substantial portions of the Software.
14 14 //
15 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 21 // IN THE SOFTWARE.
22 22
23 23 //! Macros for use in the `hg-cpython` bridge library.
24 24
25 25 use cpython::{exc, PyClone, PyErr, PyObject, PyResult, Python};
26 26 use std::cell::{BorrowMutError, Ref, RefCell, RefMut};
27 27 use std::ops::{Deref, DerefMut};
28 28 use std::result;
29 29 use std::sync::atomic::{AtomicUsize, Ordering};
30 30
31 31 /// Manages the shared state between Python and Rust
32 32 ///
33 33 /// `PySharedState` is owned by `PySharedRefCell`, and is shared across its
34 34 /// derived references. The consistency of these references are guaranteed
35 35 /// as follows:
36 36 ///
37 37 /// - The immutability of `py_class!` object fields. Any mutation of
38 38 /// `PySharedRefCell` is allowed only through its `borrow_mut()`.
39 39 /// - The `py: Python<'_>` token, which makes sure that any data access is
40 40 /// synchronized by the GIL.
41 41 /// - The underlying `RefCell`, which prevents `PySharedRefCell` data from
42 42 /// being directly borrowed or leaked while it is mutably borrowed.
43 43 /// - The `borrow_count`, which is the number of references borrowed from
44 44 /// `PyLeaked`. Just like `RefCell`, mutation is prohibited while `PyLeaked`
45 45 /// is borrowed.
46 46 /// - The `generation` counter, which increments on `borrow_mut()`. `PyLeaked`
47 47 /// reference is valid only if the `current_generation()` equals to the
48 48 /// `generation` at the time of `leak_immutable()`.
49 49 #[derive(Debug, Default)]
50 50 struct PySharedState {
51 51 // The counter variable could be Cell<usize> since any operation on
52 52 // PySharedState is synchronized by the GIL, but being "atomic" makes
53 53 // PySharedState inherently Sync. The ordering requirement doesn't
54 54 // matter thanks to the GIL.
55 55 borrow_count: AtomicUsize,
56 56 generation: AtomicUsize,
57 57 }
58 58
59 59 impl PySharedState {
60 60 fn current_borrow_count(&self, _py: Python) -> usize {
61 61 self.borrow_count.load(Ordering::Relaxed)
62 62 }
63 63
64 64 fn increase_borrow_count(&self, _py: Python) {
65 65 // Note that this wraps around if there are more than usize::MAX
66 66 // borrowed references, which shouldn't happen due to memory limit.
67 67 self.borrow_count.fetch_add(1, Ordering::Relaxed);
68 68 }
69 69
70 70 fn decrease_borrow_count(&self, _py: Python) {
71 71 let prev_count = self.borrow_count.fetch_sub(1, Ordering::Relaxed);
72 72 assert!(prev_count > 0);
73 73 }
74 74
75 75 fn current_generation(&self, _py: Python) -> usize {
76 76 self.generation.load(Ordering::Relaxed)
77 77 }
78 78
79 79 fn increment_generation(&self, py: Python) {
80 80 assert_eq!(self.current_borrow_count(py), 0);
81 81 // Note that this wraps around to the same value if mutably
82 82 // borrowed more than usize::MAX times, which wouldn't happen
83 83 // in practice.
84 84 self.generation.fetch_add(1, Ordering::Relaxed);
85 85 }
86 86 }
87 87
88 88 /// Helper to keep the borrow count updated while the shared object is
89 89 /// immutably borrowed without using the `RefCell` interface.
90 90 struct BorrowPyShared<'a> {
91 91 py: Python<'a>,
92 92 py_shared_state: &'a PySharedState,
93 93 }
94 94
95 95 impl<'a> BorrowPyShared<'a> {
96 96 fn new(
97 97 py: Python<'a>,
98 98 py_shared_state: &'a PySharedState,
99 99 ) -> BorrowPyShared<'a> {
100 100 py_shared_state.increase_borrow_count(py);
101 101 BorrowPyShared {
102 102 py,
103 103 py_shared_state,
104 104 }
105 105 }
106 106 }
107 107
108 108 impl Drop for BorrowPyShared<'_> {
109 109 fn drop(&mut self) {
110 110 self.py_shared_state.decrease_borrow_count(self.py);
111 111 }
112 112 }
113 113
114 114 /// `RefCell` wrapper to be safely used in conjunction with `PySharedState`.
115 115 ///
116 116 /// This object can be stored in a `py_class!` object as a data field. Any
117 117 /// operation is allowed through the `PySharedRef` interface.
118 118 #[derive(Debug)]
119 119 pub struct PySharedRefCell<T> {
120 120 inner: RefCell<T>,
121 121 py_shared_state: PySharedState,
122 122 }
123 123
124 124 impl<T> PySharedRefCell<T> {
125 125 pub fn new(value: T) -> PySharedRefCell<T> {
126 126 Self {
127 127 inner: RefCell::new(value),
128 128 py_shared_state: PySharedState::default(),
129 129 }
130 130 }
131 131
132 132 fn borrow<'a>(&'a self, _py: Python<'a>) -> Ref<'a, T> {
133 133 // py_shared_state isn't involved since
134 134 // - inner.borrow() would fail if self is mutably borrowed,
135 135 // - and inner.try_borrow_mut() would fail while self is borrowed.
136 136 self.inner.borrow()
137 137 }
138 138
139 139 fn try_borrow_mut<'a>(
140 140 &'a self,
141 141 py: Python<'a>,
142 142 ) -> result::Result<RefMut<'a, T>, BorrowMutError> {
143 143 if self.py_shared_state.current_borrow_count(py) > 0 {
144 144 // propagate borrow-by-leaked state to inner to get BorrowMutError
145 145 let _dummy = self.inner.borrow();
146 146 self.inner.try_borrow_mut()?;
147 147 unreachable!("BorrowMutError must be returned");
148 148 }
149 149 let inner_ref = self.inner.try_borrow_mut()?;
150 150 self.py_shared_state.increment_generation(py);
151 151 Ok(inner_ref)
152 152 }
153 153 }
154 154
155 155 /// Sharable data member of type `T` borrowed from the `PyObject`.
156 156 pub struct PySharedRef<'a, T> {
157 157 py: Python<'a>,
158 158 owner: &'a PyObject,
159 159 data: &'a PySharedRefCell<T>,
160 160 }
161 161
162 162 impl<'a, T> PySharedRef<'a, T> {
163 163 /// # Safety
164 164 ///
165 165 /// The `data` must be owned by the `owner`. Otherwise, the leak count
166 166 /// would get wrong.
167 167 pub unsafe fn new(
168 168 py: Python<'a>,
169 169 owner: &'a PyObject,
170 170 data: &'a PySharedRefCell<T>,
171 171 ) -> Self {
172 172 Self { py, owner, data }
173 173 }
174 174
175 175 pub fn borrow(&self) -> Ref<'a, T> {
176 176 self.data.borrow(self.py)
177 177 }
178 178
179 179 /// Mutably borrows the wrapped value.
180 180 ///
181 181 /// # Panics
182 182 ///
183 183 /// Panics if the value is currently borrowed through `PySharedRef`
184 184 /// or `PyLeaked`.
185 185 pub fn borrow_mut(&self) -> RefMut<'a, T> {
186 186 self.try_borrow_mut().expect("already borrowed")
187 187 }
188 188
189 189 /// Mutably borrows the wrapped value, returning an error if the value
190 190 /// is currently borrowed.
191 191 pub fn try_borrow_mut(
192 192 &self,
193 193 ) -> result::Result<RefMut<'a, T>, BorrowMutError> {
194 194 self.data.try_borrow_mut(self.py)
195 195 }
196 196
197 197 /// Returns a leaked reference.
198 198 ///
199 199 /// # Panics
200 200 ///
201 201 /// Panics if this is mutably borrowed.
202 202 pub fn leak_immutable(&self) -> PyLeaked<&'static T> {
203 203 let state = &self.data.py_shared_state;
204 204 // make sure self.data isn't mutably borrowed; otherwise the
205 205 // generation number can't be trusted.
206 206 let data_ref = self.borrow();
207 207
208 208 // &'static cast is safe because data_ptr and state_ptr are owned
209 209 // by self.owner, and we do have the GIL for thread safety.
210 210 let data_ptr: *const T = &*data_ref;
211 211 let state_ptr: *const PySharedState = state;
212 212 PyLeaked::<&'static T> {
213 213 inner: self.owner.clone_ref(self.py),
214 214 data: unsafe { &*data_ptr },
215 215 py_shared_state: unsafe { &*state_ptr },
216 216 generation: state.current_generation(self.py),
217 217 }
218 218 }
219 219 }
220 220
221 221 /// Allows a `py_class!` generated struct to share references to one of its
222 222 /// data members with Python.
223 223 ///
224 224 /// # Parameters
225 225 ///
226 226 /// * `$name` is the same identifier used in for `py_class!` macro call.
227 227 /// * `$inner_struct` is the identifier of the underlying Rust struct
228 228 /// * `$data_member` is the identifier of the data member of `$inner_struct`
229 229 /// that will be shared.
230 230 /// * `$shared_accessor` is the function name to be generated, which allows
231 231 /// safe access to the data member.
232 232 ///
233 233 /// # Safety
234 234 ///
235 235 /// `$data_member` must persist while the `$name` object is alive. In other
236 236 /// words, it must be an accessor to a data field of the Python object.
237 237 ///
238 238 /// # Example
239 239 ///
240 240 /// ```
241 241 /// struct MyStruct {
242 242 /// inner: Vec<u32>;
243 243 /// }
244 244 ///
245 245 /// py_class!(pub class MyType |py| {
246 246 /// data inner: PySharedRefCell<MyStruct>;
247 247 /// });
248 248 ///
249 249 /// py_shared_ref!(MyType, MyStruct, inner, inner_shared);
250 250 /// ```
251 251 macro_rules! py_shared_ref {
252 252 (
253 253 $name: ident,
254 254 $inner_struct: ident,
255 255 $data_member: ident,
256 256 $shared_accessor: ident
257 257 ) => {
258 258 impl $name {
259 259 /// Returns a safe reference to the shared `$data_member`.
260 260 ///
261 261 /// This function guarantees that `PySharedRef` is created with
262 262 /// the valid `self` and `self.$data_member(py)` pair.
263 263 fn $shared_accessor<'a>(
264 264 &'a self,
265 265 py: Python<'a>,
266 266 ) -> $crate::ref_sharing::PySharedRef<'a, $inner_struct> {
267 267 use cpython::PythonObject;
268 268 use $crate::ref_sharing::PySharedRef;
269 269 let owner = self.as_object();
270 270 let data = self.$data_member(py);
271 271 unsafe { PySharedRef::new(py, owner, data) }
272 272 }
273 273 }
274 274 };
275 275 }
276 276
277 277 /// Manage immutable references to `PyObject` leaked into Python iterators.
278 278 ///
279 279 /// This reference will be invalidated once the original value is mutably
280 280 /// borrowed.
281 281 pub struct PyLeaked<T> {
282 282 inner: PyObject,
283 283 data: T,
284 284 py_shared_state: &'static PySharedState,
285 285 /// Generation counter of data `T` captured when PyLeaked is created.
286 286 generation: usize,
287 287 }
288 288
289 289 // DO NOT implement Deref for PyLeaked<T>! Dereferencing PyLeaked
290 290 // without taking Python GIL wouldn't be safe. Also, the underling reference
291 291 // is invalid if generation != py_shared_state.generation.
292 292
293 293 impl<T> PyLeaked<T> {
294 294 /// Immutably borrows the wrapped value.
295 295 ///
296 296 /// Borrowing fails if the underlying reference has been invalidated.
297 pub fn try_borrow<'a>(
297 ///
298 /// # Safety
299 ///
300 /// The lifetime of the innermost object is cheated. Do not obtain and
301 /// copy it out of the borrow scope. See the example of `try_borrow_mut()`
302 /// for details.
303 pub unsafe fn try_borrow<'a>(
298 304 &'a self,
299 305 py: Python<'a>,
300 306 ) -> PyResult<PyLeakedRef<'a, T>> {
301 307 self.validate_generation(py)?;
302 308 Ok(PyLeakedRef {
303 309 _borrow: BorrowPyShared::new(py, self.py_shared_state),
304 310 data: &self.data,
305 311 })
306 312 }
307 313
308 314 /// Mutably borrows the wrapped value.
309 315 ///
310 316 /// Borrowing fails if the underlying reference has been invalidated.
311 317 ///
312 318 /// Typically `T` is an iterator. If `T` is an immutable reference,
313 319 /// `get_mut()` is useless since the inner value can't be mutated.
314 pub fn try_borrow_mut<'a>(
320 ///
321 /// # Safety
322 ///
323 /// The lifetime of the innermost object is cheated. Do not obtain and
324 /// copy it out of the borrow scope. For example, the following code
325 /// is unsafe:
326 ///
327 /// ```compile_fail
328 /// let slice;
329 /// {
330 /// let iter = leaked.try_borrow_mut(py);
331 /// // slice can outlive since the iterator is of Iter<'static, T>
332 /// // type, but it shouldn't.
333 /// slice = iter.as_slice();
334 /// }
335 /// println!("{:?}", slice);
336 /// ```
337 pub unsafe fn try_borrow_mut<'a>(
315 338 &'a mut self,
316 339 py: Python<'a>,
317 340 ) -> PyResult<PyLeakedRefMut<'a, T>> {
318 341 self.validate_generation(py)?;
319 342 Ok(PyLeakedRefMut {
320 343 _borrow: BorrowPyShared::new(py, self.py_shared_state),
321 344 data: &mut self.data,
322 345 })
323 346 }
324 347
325 348 /// Converts the inner value by the given function.
326 349 ///
327 350 /// Typically `T` is a static reference to a container, and `U` is an
328 351 /// iterator of that container.
329 352 ///
330 353 /// # Panics
331 354 ///
332 355 /// Panics if the underlying reference has been invalidated.
333 356 ///
334 357 /// This is typically called immediately after the `PyLeaked` is obtained.
335 358 /// In which case, the reference must be valid and no panic would occur.
336 359 ///
337 360 /// # Safety
338 361 ///
339 362 /// The lifetime of the object passed in to the function `f` is cheated.
340 363 /// It's typically a static reference, but is valid only while the
341 364 /// corresponding `PyLeaked` is alive. Do not copy it out of the
342 365 /// function call.
343 366 pub unsafe fn map<U>(
344 367 self,
345 368 py: Python,
346 369 f: impl FnOnce(T) -> U,
347 370 ) -> PyLeaked<U> {
348 371 // Needs to test the generation value to make sure self.data reference
349 372 // is still intact.
350 373 self.validate_generation(py)
351 374 .expect("map() over invalidated leaked reference");
352 375
353 376 // f() could make the self.data outlive. That's why map() is unsafe.
354 377 // In order to make this function safe, maybe we'll need a way to
355 378 // temporarily restrict the lifetime of self.data and translate the
356 379 // returned object back to Something<'static>.
357 380 let new_data = f(self.data);
358 381 PyLeaked {
359 382 inner: self.inner,
360 383 data: new_data,
361 384 py_shared_state: self.py_shared_state,
362 385 generation: self.generation,
363 386 }
364 387 }
365 388
366 389 fn validate_generation(&self, py: Python) -> PyResult<()> {
367 390 if self.py_shared_state.current_generation(py) == self.generation {
368 391 Ok(())
369 392 } else {
370 393 Err(PyErr::new::<exc::RuntimeError, _>(
371 394 py,
372 395 "Cannot access to leaked reference after mutation",
373 396 ))
374 397 }
375 398 }
376 399 }
377 400
378 401 /// Immutably borrowed reference to a leaked value.
379 402 pub struct PyLeakedRef<'a, T> {
380 403 _borrow: BorrowPyShared<'a>,
381 404 data: &'a T,
382 405 }
383 406
384 407 impl<T> Deref for PyLeakedRef<'_, T> {
385 408 type Target = T;
386 409
387 410 fn deref(&self) -> &T {
388 411 self.data
389 412 }
390 413 }
391 414
392 415 /// Mutably borrowed reference to a leaked value.
393 416 pub struct PyLeakedRefMut<'a, T> {
394 417 _borrow: BorrowPyShared<'a>,
395 418 data: &'a mut T,
396 419 }
397 420
398 421 impl<T> Deref for PyLeakedRefMut<'_, T> {
399 422 type Target = T;
400 423
401 424 fn deref(&self) -> &T {
402 425 self.data
403 426 }
404 427 }
405 428
406 429 impl<T> DerefMut for PyLeakedRefMut<'_, T> {
407 430 fn deref_mut(&mut self) -> &mut T {
408 431 self.data
409 432 }
410 433 }
411 434
412 435 /// Defines a `py_class!` that acts as a Python iterator over a Rust iterator.
413 436 ///
414 437 /// TODO: this is a bit awkward to use, and a better (more complicated)
415 438 /// procedural macro would simplify the interface a lot.
416 439 ///
417 440 /// # Parameters
418 441 ///
419 442 /// * `$name` is the identifier to give to the resulting Rust struct.
420 443 /// * `$leaked` corresponds to `$leaked` in the matching `py_shared_ref!` call.
421 444 /// * `$iterator_type` is the type of the Rust iterator.
422 445 /// * `$success_func` is a function for processing the Rust `(key, value)`
423 446 /// tuple on iteration success, turning it into something Python understands.
424 447 /// * `$success_func` is the return type of `$success_func`
425 448 ///
449 /// # Safety
450 ///
451 /// `$success_func` may take a reference, but it's lifetime may be cheated.
452 /// Do not copy it out of the function call.
453 ///
426 454 /// # Example
427 455 ///
428 456 /// ```
429 457 /// struct MyStruct {
430 458 /// inner: HashMap<Vec<u8>, Vec<u8>>;
431 459 /// }
432 460 ///
433 461 /// py_class!(pub class MyType |py| {
434 462 /// data inner: PySharedRefCell<MyStruct>;
435 463 ///
436 464 /// def __iter__(&self) -> PyResult<MyTypeItemsIterator> {
437 465 /// let leaked_ref = self.inner_shared(py).leak_immutable();
438 466 /// MyTypeItemsIterator::from_inner(
439 467 /// py,
440 468 /// unsafe { leaked_ref.map(py, |o| o.iter()) },
441 469 /// )
442 470 /// }
443 471 /// });
444 472 ///
445 473 /// impl MyType {
446 474 /// fn translate_key_value(
447 475 /// py: Python,
448 476 /// res: (&Vec<u8>, &Vec<u8>),
449 477 /// ) -> PyResult<Option<(PyBytes, PyBytes)>> {
450 478 /// let (f, entry) = res;
451 479 /// Ok(Some((
452 480 /// PyBytes::new(py, f),
453 481 /// PyBytes::new(py, entry),
454 482 /// )))
455 483 /// }
456 484 /// }
457 485 ///
458 486 /// py_shared_ref!(MyType, MyStruct, inner, MyTypeLeakedRef);
459 487 ///
460 488 /// py_shared_iterator!(
461 489 /// MyTypeItemsIterator,
462 490 /// PyLeaked<HashMap<'static, Vec<u8>, Vec<u8>>>,
463 491 /// MyType::translate_key_value,
464 492 /// Option<(PyBytes, PyBytes)>
465 493 /// );
466 494 /// ```
467 495 macro_rules! py_shared_iterator {
468 496 (
469 497 $name: ident,
470 498 $leaked: ty,
471 499 $success_func: expr,
472 500 $success_type: ty
473 501 ) => {
474 502 py_class!(pub class $name |py| {
475 503 data inner: RefCell<$leaked>;
476 504
477 505 def __next__(&self) -> PyResult<$success_type> {
478 506 let mut leaked = self.inner(py).borrow_mut();
479 let mut iter = leaked.try_borrow_mut(py)?;
507 let mut iter = unsafe { leaked.try_borrow_mut(py)? };
480 508 match iter.next() {
481 509 None => Ok(None),
510 // res may be a reference of cheated 'static lifetime
482 511 Some(res) => $success_func(py, res),
483 512 }
484 513 }
485 514
486 515 def __iter__(&self) -> PyResult<Self> {
487 516 Ok(self.clone_ref(py))
488 517 }
489 518 });
490 519
491 520 impl $name {
492 521 pub fn from_inner(
493 522 py: Python,
494 523 leaked: $leaked,
495 524 ) -> PyResult<Self> {
496 525 Self::create_instance(
497 526 py,
498 527 RefCell::new(leaked),
499 528 )
500 529 }
501 530 }
502 531 };
503 532 }
504 533
505 534 #[cfg(test)]
506 535 #[cfg(any(feature = "python27-bin", feature = "python3-bin"))]
507 536 mod test {
508 537 use super::*;
509 538 use cpython::{GILGuard, Python};
510 539
511 540 py_class!(class Owner |py| {
512 541 data string: PySharedRefCell<String>;
513 542 });
514 543 py_shared_ref!(Owner, String, string, string_shared);
515 544
516 545 fn prepare_env() -> (GILGuard, Owner) {
517 546 let gil = Python::acquire_gil();
518 547 let py = gil.python();
519 548 let owner =
520 549 Owner::create_instance(py, PySharedRefCell::new("new".to_owned()))
521 550 .unwrap();
522 551 (gil, owner)
523 552 }
524 553
525 554 #[test]
526 555 fn test_leaked_borrow() {
527 556 let (gil, owner) = prepare_env();
528 557 let py = gil.python();
529 558 let leaked = owner.string_shared(py).leak_immutable();
530 let leaked_ref = leaked.try_borrow(py).unwrap();
559 let leaked_ref = unsafe { leaked.try_borrow(py) }.unwrap();
531 560 assert_eq!(*leaked_ref, "new");
532 561 }
533 562
534 563 #[test]
535 564 fn test_leaked_borrow_mut() {
536 565 let (gil, owner) = prepare_env();
537 566 let py = gil.python();
538 567 let leaked = owner.string_shared(py).leak_immutable();
539 568 let mut leaked_iter = unsafe { leaked.map(py, |s| s.chars()) };
540 let mut leaked_ref = leaked_iter.try_borrow_mut(py).unwrap();
569 let mut leaked_ref =
570 unsafe { leaked_iter.try_borrow_mut(py) }.unwrap();
541 571 assert_eq!(leaked_ref.next(), Some('n'));
542 572 assert_eq!(leaked_ref.next(), Some('e'));
543 573 assert_eq!(leaked_ref.next(), Some('w'));
544 574 assert_eq!(leaked_ref.next(), None);
545 575 }
546 576
547 577 #[test]
548 578 fn test_leaked_borrow_after_mut() {
549 579 let (gil, owner) = prepare_env();
550 580 let py = gil.python();
551 581 let leaked = owner.string_shared(py).leak_immutable();
552 582 owner.string_shared(py).borrow_mut().clear();
553 assert!(leaked.try_borrow(py).is_err());
583 assert!(unsafe { leaked.try_borrow(py) }.is_err());
554 584 }
555 585
556 586 #[test]
557 587 fn test_leaked_borrow_mut_after_mut() {
558 588 let (gil, owner) = prepare_env();
559 589 let py = gil.python();
560 590 let leaked = owner.string_shared(py).leak_immutable();
561 591 let mut leaked_iter = unsafe { leaked.map(py, |s| s.chars()) };
562 592 owner.string_shared(py).borrow_mut().clear();
563 assert!(leaked_iter.try_borrow_mut(py).is_err());
593 assert!(unsafe { leaked_iter.try_borrow_mut(py) }.is_err());
564 594 }
565 595
566 596 #[test]
567 597 #[should_panic(expected = "map() over invalidated leaked reference")]
568 598 fn test_leaked_map_after_mut() {
569 599 let (gil, owner) = prepare_env();
570 600 let py = gil.python();
571 601 let leaked = owner.string_shared(py).leak_immutable();
572 602 owner.string_shared(py).borrow_mut().clear();
573 603 let _leaked_iter = unsafe { leaked.map(py, |s| s.chars()) };
574 604 }
575 605
576 606 #[test]
577 607 fn test_try_borrow_mut_while_leaked_ref() {
578 608 let (gil, owner) = prepare_env();
579 609 let py = gil.python();
580 610 assert!(owner.string_shared(py).try_borrow_mut().is_ok());
581 611 let leaked = owner.string_shared(py).leak_immutable();
582 612 {
583 let _leaked_ref = leaked.try_borrow(py).unwrap();
613 let _leaked_ref = unsafe { leaked.try_borrow(py) }.unwrap();
584 614 assert!(owner.string_shared(py).try_borrow_mut().is_err());
585 615 {
586 let _leaked_ref2 = leaked.try_borrow(py).unwrap();
616 let _leaked_ref2 = unsafe { leaked.try_borrow(py) }.unwrap();
587 617 assert!(owner.string_shared(py).try_borrow_mut().is_err());
588 618 }
589 619 assert!(owner.string_shared(py).try_borrow_mut().is_err());
590 620 }
591 621 assert!(owner.string_shared(py).try_borrow_mut().is_ok());
592 622 }
593 623
594 624 #[test]
595 625 fn test_try_borrow_mut_while_leaked_ref_mut() {
596 626 let (gil, owner) = prepare_env();
597 627 let py = gil.python();
598 628 assert!(owner.string_shared(py).try_borrow_mut().is_ok());
599 629 let leaked = owner.string_shared(py).leak_immutable();
600 630 let mut leaked_iter = unsafe { leaked.map(py, |s| s.chars()) };
601 631 {
602 let _leaked_ref = leaked_iter.try_borrow_mut(py).unwrap();
632 let _leaked_ref =
633 unsafe { leaked_iter.try_borrow_mut(py) }.unwrap();
603 634 assert!(owner.string_shared(py).try_borrow_mut().is_err());
604 635 }
605 636 assert!(owner.string_shared(py).try_borrow_mut().is_ok());
606 637 }
607 638
608 639 #[test]
609 640 #[should_panic(expected = "mutably borrowed")]
610 641 fn test_leak_while_borrow_mut() {
611 642 let (gil, owner) = prepare_env();
612 643 let py = gil.python();
613 644 let _mut_ref = owner.string_shared(py).borrow_mut();
614 645 owner.string_shared(py).leak_immutable();
615 646 }
616 647
617 648 #[test]
618 649 fn test_try_borrow_mut_while_borrow() {
619 650 let (gil, owner) = prepare_env();
620 651 let py = gil.python();
621 652 let _ref = owner.string_shared(py).borrow();
622 653 assert!(owner.string_shared(py).try_borrow_mut().is_err());
623 654 }
624 655
625 656 #[test]
626 657 #[should_panic(expected = "already borrowed")]
627 658 fn test_borrow_mut_while_borrow() {
628 659 let (gil, owner) = prepare_env();
629 660 let py = gil.python();
630 661 let _ref = owner.string_shared(py).borrow();
631 662 owner.string_shared(py).borrow_mut();
632 663 }
633 664 }
General Comments 0
You need to be logged in to leave comments. Login now