Skip to content

Conversation

@kngwyu
Copy link
Member

@kngwyu kngwyu commented Nov 19, 2020

Resolve #1281. Support char and PyString conversion.
I'm sorry this PR includes some unrelated cleanups. Since recently I don't have much time for PyO3, I think it would be better to do some cleanups when I find the necessity.

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

👍 looks great, thanks! I made some minor nitpicks, only one I'd definitely like to see is the error message formatting.

Also CHANGELOG entry 😉

where
T: ToPyObject,
{
impl<'a, T: ?Sized + ToPyObject> ToPyObject for &'a T {
Copy link
Member

Choose a reason for hiding this comment

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

👍 nice! I think the lifetime can maybe also be anonymous

Suggested change
impl<'a, T: ?Sized + ToPyObject> ToPyObject for &'a T {
impl<T: ?Sized + ToPyObject> ToPyObject for &'_ T {

Copy link
Member Author

Choose a reason for hiding this comment

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

👍

Comment on lines 188 to 189
impl<'source> FromPyObject<'source> for char {
fn extract(obj: &'source PyAny) -> PyResult<Self> {
Copy link
Member

Choose a reason for hiding this comment

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

Similarly can skip the lifetime here:

Suggested change
impl<'source> FromPyObject<'source> for char {
fn extract(obj: &'source PyAny) -> PyResult<Self> {
impl FromPyObject<'_> for char {
fn extract(obj: &PyAny) -> PyResult<Self> {

Ok(ch)
} else {
Err(crate::exceptions::PyValueError::new_err(format!(
"Expected a sting of length 1",
Copy link
Member

Choose a reason for hiding this comment

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

typo sting -> string, and I think the recommendation in #1212 (comment) was that it's better for us to make errors start with lowercase

Suggested change
"Expected a sting of length 1",
"expected a string of length 1",

#[test]
fn test_extract_char() {
Python::with_gil(|py| {
let ch = '😃';
Copy link
Member

Choose a reason for hiding this comment

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

😃

@davidhewitt
Copy link
Member

I'm working on a PR to fix the clippy errors with new Rust 1.48.

let ch = '😃';
let py_string = ch.to_object(py);
let ch2: char = FromPyObject::extract(py_string.as_ref(py)).unwrap();
assert_eq!(ch, ch2);
Copy link
Member

Choose a reason for hiding this comment

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

Is there a test for the error case (string with len > 1)?

Copy link
Member

Choose a reason for hiding this comment

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

👍 good idea

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks!

@davidhewitt
Copy link
Member

Rebase on master to fix CI (see #1284).

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

💯

@kngwyu kngwyu merged commit c621b70 into master Nov 20, 2020
@messense messense deleted the char-support branch March 18, 2021 02:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for Rust char type

4 participants