()
| 679 | |
| 680 | #[test] |
| 681 | fn test_bytes_buffer() { |
| 682 | let gil = Python::acquire_gil(); |
| 683 | let py = gil.python(); |
| 684 | let bytes = py.eval("b'abcde'", None, None).unwrap(); |
| 685 | let buffer = PyBuffer::get(py, &bytes).unwrap(); |
| 686 | assert_eq!(buffer.dimensions(), 1); |
| 687 | assert_eq!(buffer.item_count(), 5); |
| 688 | assert_eq!(buffer.format().to_str().unwrap(), "B"); |
| 689 | assert_eq!(buffer.shape(), [5]); |
| 690 | // single-dimensional buffer is always contiguous |
| 691 | assert!(buffer.is_c_contiguous()); |
| 692 | assert!(buffer.is_fortran_contiguous()); |
| 693 | |
| 694 | assert!(buffer.as_slice::<f64>(py).is_none()); |
| 695 | assert!(buffer.as_slice::<i8>(py).is_none()); |
| 696 | |
| 697 | let slice = buffer.as_slice::<u8>(py).unwrap(); |
| 698 | assert_eq!(slice.len(), 5); |
| 699 | assert_eq!(slice[0].get(), b'a'); |
| 700 | assert_eq!(slice[2].get(), b'c'); |
| 701 | |
| 702 | assert!(buffer.as_mut_slice::<u8>(py).is_none()); |
| 703 | |
| 704 | assert!(buffer.copy_to_slice(py, &mut [0u8]).is_err()); |
| 705 | let mut arr = [0; 5]; |
| 706 | buffer.copy_to_slice(py, &mut arr).unwrap(); |
| 707 | assert_eq!(arr, b"abcde" as &[u8]); |
| 708 | |
| 709 | assert!(buffer.copy_from_slice(py, &[0u8; 5]).is_err()); |
| 710 | |
| 711 | assert!(buffer.to_vec::<i8>(py).is_err()); |
| 712 | assert!(buffer.to_vec::<u16>(py).is_err()); |
| 713 | assert_eq!(buffer.to_vec::<u8>(py).unwrap(), b"abcde"); |
| 714 | } |
| 715 | |
| 716 | #[test] |
| 717 | #[cfg(feature = "python3-sys")] // array.array doesn't implement the buffer protocol in python 2.7 |
nothing calls this directly
no test coverage detected