forked from Qiskit/qiskit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ecf73d6
commit b30d2c6
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// This code is part of Qiskit. | ||
// | ||
// (C) Copyright IBM 2023 | ||
// | ||
// This code is licensed under the Apache License, Version 2.0. You may | ||
// obtain a copy of this license in the LICENSE.txt file in the root directory | ||
// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// Any modifications or derivative works of this code must retain this | ||
// copyright notice, and modified files need to carry a notice indicating | ||
// that they have been altered from the originals. | ||
use pyo3::ffi::Py_ssize_t; | ||
use pyo3::prelude::*; | ||
use pyo3::types::{PyList, PyTuple}; | ||
use pyo3::{ffi, AsPyPointer, PyNativeType}; | ||
|
||
pub fn tuple_new(py: Python<'_>, items: Vec<PyObject>) -> Py<PyTuple> { | ||
unsafe { | ||
let ptr = ffi::PyTuple_New(items.len() as Py_ssize_t); | ||
let tup: Py<PyTuple> = Py::from_owned_ptr(py, ptr); | ||
for (i, obj) in items.into_iter().enumerate() { | ||
ffi::PyTuple_SetItem(ptr, i as Py_ssize_t, obj.into_ptr()); | ||
} | ||
tup | ||
} | ||
} | ||
|
||
pub fn tuple_new_empty(py: Python<'_>) -> Py<PyTuple> { | ||
unsafe { Py::from_owned_ptr(py, ffi::PyTuple_New(0)) } | ||
} | ||
|
||
pub fn tuple_from_list(list: &PyList) -> Py<PyTuple> { | ||
unsafe { Py::from_owned_ptr(list.py(), ffi::PyList_AsTuple(list.as_ptr())) } | ||
} |