Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reintroduce PySet constructors #4492

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/conversions/hashbrown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ mod tests {
#[test]
fn test_extract_hashbrown_hashset() {
Python::with_gil(|py| {
let set = PySet::new_bound(py, &[1, 2, 3, 4, 5]).unwrap();
let set = PySet::new(py, &[1, 2, 3, 4, 5]).unwrap();
let hash_set: hashbrown::HashSet<usize> = set.extract().unwrap();
assert_eq!(hash_set, [1, 2, 3, 4, 5].iter().copied().collect());

Expand Down
4 changes: 2 additions & 2 deletions src/conversions/std/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ mod tests {
#[test]
fn test_extract_hashset() {
Python::with_gil(|py| {
let set = PySet::new_bound(py, &[1, 2, 3, 4, 5]).unwrap();
let set = PySet::new(py, &[1, 2, 3, 4, 5]).unwrap();
let hash_set: HashSet<usize> = set.extract().unwrap();
assert_eq!(hash_set, [1, 2, 3, 4, 5].iter().copied().collect());

Expand All @@ -225,7 +225,7 @@ mod tests {
#[test]
fn test_extract_btreeset() {
Python::with_gil(|py| {
let set = PySet::new_bound(py, &[1, 2, 3, 4, 5]).unwrap();
let set = PySet::new(py, &[1, 2, 3, 4, 5]).unwrap();
let hash_set: BTreeSet<usize> = set.extract().unwrap();
assert_eq!(hash_set, [1, 2, 3, 4, 5].iter().copied().collect());

Expand Down
47 changes: 32 additions & 15 deletions src/types/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,38 @@ impl PySet {
///
/// Returns an error if some element is not hashable.
#[inline]
pub fn new_bound<'a, 'p, T: ToPyObject + 'a>(
pub fn new<'a, 'p, T: ToPyObject + 'a>(
py: Python<'p>,
elements: impl IntoIterator<Item = &'a T>,
) -> PyResult<Bound<'p, PySet>> {
new_from_iter(py, elements)
}

/// Deprecated name for [`PySet::new`].
#[deprecated(since = "0.23.0", note = "renamed to `PySet::new`")]
#[inline]
pub fn new_bound<'a, 'p, T: ToPyObject + 'a>(
py: Python<'p>,
elements: impl IntoIterator<Item = &'a T>,
) -> PyResult<Bound<'p, PySet>> {
Self::new(py, elements)
}

/// Creates a new empty set.
pub fn empty_bound(py: Python<'_>) -> PyResult<Bound<'_, PySet>> {
pub fn empty(py: Python<'_>) -> PyResult<Bound<'_, PySet>> {
unsafe {
ffi::PySet_New(ptr::null_mut())
.assume_owned_or_err(py)
.downcast_into_unchecked()
}
}

/// Deprecated name for [`PySet::empty`].
#[deprecated(since = "0.23.0", note = "renamed to `PySet::empty`")]
#[inline]
pub fn empty_bound(py: Python<'_>) -> PyResult<Bound<'_, PySet>> {
Self::empty(py)
}
}

/// Implementation of functionality for [`PySet`].
Expand Down Expand Up @@ -286,18 +303,18 @@ mod tests {
#[test]
fn test_set_new() {
Python::with_gil(|py| {
let set = PySet::new_bound(py, &[1]).unwrap();
let set = PySet::new(py, &[1]).unwrap();
assert_eq!(1, set.len());

let v = vec![1];
assert!(PySet::new_bound(py, &[v]).is_err());
assert!(PySet::new(py, &[v]).is_err());
});
}

#[test]
fn test_set_empty() {
Python::with_gil(|py| {
let set = PySet::empty_bound(py).unwrap();
let set = PySet::empty(py).unwrap();
assert_eq!(0, set.len());
assert!(set.is_empty());
});
Expand All @@ -320,7 +337,7 @@ mod tests {
#[test]
fn test_set_clear() {
Python::with_gil(|py| {
let set = PySet::new_bound(py, &[1]).unwrap();
let set = PySet::new(py, &[1]).unwrap();
assert_eq!(1, set.len());
set.clear();
assert_eq!(0, set.len());
Expand All @@ -330,15 +347,15 @@ mod tests {
#[test]
fn test_set_contains() {
Python::with_gil(|py| {
let set = PySet::new_bound(py, &[1]).unwrap();
let set = PySet::new(py, &[1]).unwrap();
assert!(set.contains(1).unwrap());
});
}

#[test]
fn test_set_discard() {
Python::with_gil(|py| {
let set = PySet::new_bound(py, &[1]).unwrap();
let set = PySet::new(py, &[1]).unwrap();
assert!(!set.discard(2).unwrap());
assert_eq!(1, set.len());

Expand All @@ -353,7 +370,7 @@ mod tests {
#[test]
fn test_set_add() {
Python::with_gil(|py| {
let set = PySet::new_bound(py, &[1, 2]).unwrap();
let set = PySet::new(py, &[1, 2]).unwrap();
set.add(1).unwrap(); // Add a dupliated element
assert!(set.contains(1).unwrap());
});
Expand All @@ -362,7 +379,7 @@ mod tests {
#[test]
fn test_set_pop() {
Python::with_gil(|py| {
let set = PySet::new_bound(py, &[1]).unwrap();
let set = PySet::new(py, &[1]).unwrap();
let val = set.pop();
assert!(val.is_some());
let val2 = set.pop();
Expand All @@ -380,7 +397,7 @@ mod tests {
#[test]
fn test_set_iter() {
Python::with_gil(|py| {
let set = PySet::new_bound(py, &[1]).unwrap();
let set = PySet::new(py, &[1]).unwrap();

for el in set {
assert_eq!(1i32, el.extract::<'_, i32>().unwrap());
Expand All @@ -393,7 +410,7 @@ mod tests {
use crate::types::any::PyAnyMethods;

Python::with_gil(|py| {
let set = PySet::new_bound(py, &[1]).unwrap();
let set = PySet::new(py, &[1]).unwrap();

for el in &set {
assert_eq!(1i32, el.extract::<i32>().unwrap());
Expand All @@ -405,7 +422,7 @@ mod tests {
#[should_panic]
fn test_set_iter_mutation() {
Python::with_gil(|py| {
let set = PySet::new_bound(py, &[1, 2, 3, 4, 5]).unwrap();
let set = PySet::new(py, &[1, 2, 3, 4, 5]).unwrap();

for _ in &set {
let _ = set.add(42);
Expand All @@ -417,7 +434,7 @@ mod tests {
#[should_panic]
fn test_set_iter_mutation_same_len() {
Python::with_gil(|py| {
let set = PySet::new_bound(py, &[1, 2, 3, 4, 5]).unwrap();
let set = PySet::new(py, &[1, 2, 3, 4, 5]).unwrap();

for item in &set {
let item: i32 = item.extract().unwrap();
Expand All @@ -430,7 +447,7 @@ mod tests {
#[test]
fn test_set_iter_size_hint() {
Python::with_gil(|py| {
let set = PySet::new_bound(py, &[1]).unwrap();
let set = PySet::new(py, &[1]).unwrap();
let mut iter = set.iter();

// Exact size
Expand Down
Loading