Skip to content

replace PyString::from_object with PyString::from_encoded_object #5017

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions newsfragments/5017.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `PyString::from_encoded_object`.
1 change: 1 addition & 0 deletions newsfragments/5017.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate `PyString::from_object` in favour of `PyString::from_encoded_object`.
55 changes: 43 additions & 12 deletions src/types/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::types::PyBytes;
use crate::IntoPy;
use crate::{ffi, Bound, Py, PyAny, PyResult, Python};
use std::borrow::Cow;
use std::ffi::CString;
use std::ffi::{CStr, CString};
use std::str;

/// Deprecated alias for [`PyString`].
Expand Down Expand Up @@ -212,32 +212,48 @@ impl PyString {
/// Attempts to create a Python string from a Python [bytes-like object].
///
/// [bytes-like object]: (https://docs.python.org/3/glossary.html#term-bytes-like-object).
pub fn from_encoded_object<'py>(
src: &Bound<'py, PyAny>,
encoding: &CStr,
errors: &CStr,
Comment on lines +217 to +218
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be Options to "have the interface use the default values" or do we want to explicitly not provide that option?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah good spot. I'll have a think about this, I'm not sure which I prefer 🤔

) -> PyResult<Bound<'py, PyString>> {
unsafe {
ffi::PyUnicode_FromEncodedObject(src.as_ptr(), encoding.as_ptr(), errors.as_ptr())
.assume_owned_or_err(src.py())
.downcast_into_unchecked()
}
}

/// Deprecated form of `PyString::from_encoded_object`.
///
/// This version took `&str` arguments for `encoding` and `errors`, which required a runtime
/// conversion to `CString` internally.
#[deprecated(
since = "0.25.0",
note = "replaced with to `PyString::from_encoded_object`"
)]
pub fn from_object<'py>(
src: &Bound<'py, PyAny>,
encoding: &str,
errors: &str,
) -> PyResult<Bound<'py, PyString>> {
let encoding = CString::new(encoding)?;
let errors = CString::new(errors)?;
unsafe {
ffi::PyUnicode_FromEncodedObject(
src.as_ptr(),
encoding.as_ptr().cast(),
errors.as_ptr().cast(),
)
.assume_owned_or_err(src.py())
.downcast_into_unchecked()
}
PyString::from_encoded_object(src, &encoding, &errors)
}

/// Deprecated name for [`PyString::from_object`].
#[deprecated(since = "0.23.0", note = "renamed to `PyString::from_object`")]
#[deprecated(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this was deprecated already, we can probably just remove it already, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, just a race with the other prs, think this function is deleted now 🙌

since = "0.23.0",
note = "replcaed with `PyString::from_encoded_object`"
)]
#[inline]
pub fn from_object_bound<'py>(
src: &Bound<'py, PyAny>,
encoding: &str,
errors: &str,
) -> PyResult<Bound<'py, PyString>> {
#[allow(deprecated)]
Self::from_object(src, encoding, errors)
}
}
Expand Down Expand Up @@ -586,6 +602,8 @@ impl PartialEq<Borrowed<'_, '_, PyString>> for &'_ str {

#[cfg(test)]
mod tests {
use pyo3_ffi::c_str;

use super::*;
use crate::{IntoPyObject, PyObject};

Expand Down Expand Up @@ -678,6 +696,14 @@ mod tests {
Python::with_gil(|py| {
let py_bytes = PyBytes::new(py, b"ab\xFFcd");

let py_string =
PyString::from_encoded_object(&py_bytes, c_str!("utf-8"), c_str!("ignore"))
.unwrap();

let result = py_string.to_cow().unwrap();
assert_eq!(result, "abcd");

#[allow(deprecated)]
let py_string = PyString::from_object(&py_bytes, "utf-8", "ignore").unwrap();

let result = py_string.to_cow().unwrap();
Expand All @@ -686,13 +712,18 @@ mod tests {
}

#[test]
fn test_string_from_obect_with_invalid_encoding_errors() {
fn test_string_from_object_with_invalid_encoding_errors() {
Python::with_gil(|py| {
let py_bytes = PyBytes::new(py, b"abcd");

let result = PyString::from_encoded_object(&py_bytes, c_str!("wat"), c_str!("ignore"));
assert!(result.is_err());

#[allow(deprecated)]
let result = PyString::from_object(&py_bytes, "utf\0-8", "ignore");
assert!(result.is_err());

#[allow(deprecated)]
let result = PyString::from_object(&py_bytes, "utf-8", "ign\0ore");
assert!(result.is_err());
});
Expand Down
Loading