-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add str.encode for utf-8 #901
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ use crate::pyobject::{ | |
}; | ||
use crate::vm::VirtualMachine; | ||
|
||
use super::objbytes::PyBytes; | ||
use super::objdict::PyDict; | ||
use super::objint::{self, PyInt}; | ||
use super::objnone::PyNone; | ||
|
@@ -957,6 +958,31 @@ impl PyString { | |
} | ||
} | ||
} | ||
|
||
#[pymethod] | ||
fn encode( | ||
&self, | ||
encoding: OptionalArg<PyObjectRef>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use PyStringRef to avoid type checking for "encoding" and "errors" wich will always be string There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By trying it, I found I can't control the exception message with it. Do you have any idea about it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RustPython doesn't aim to have exactly the same error message as cpython. But it's better to let the type checking "automatic" in function type. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure about error messages. If we can keep them the same or similar with small cost, why not? Is there priority about this? @windelbouwman There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error messages to my opinion do not have to be similar to cpython. While I worked with rust, I find the error messages super useful in rust, maybe we could try something new in rustpython as well with regards to error messages. So in this case, I agree with @jgirardet to use the type checker with |
||
_errors: OptionalArg<PyObjectRef>, | ||
vm: &VirtualMachine, | ||
) -> PyResult { | ||
let encoding = encoding.map_or_else( | ||
|| Ok("utf-8".to_string()), | ||
|v| { | ||
if objtype::isinstance(&v, &vm.ctx.str_type()) { | ||
Ok(get_value(&v)) | ||
} else { | ||
Err(vm.new_type_error(format!( | ||
"encode() argument 1 must be str, not {}", | ||
v.class().name | ||
))) | ||
} | ||
}, | ||
)?; | ||
|
||
let encoded = PyBytes::from_string(&self.value, &encoding, vm)?; | ||
Ok(encoded.into_pyobject(vm)?) | ||
} | ||
} | ||
|
||
impl PyValue for PyString { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
u8
is in encoding.aliases so I think it is redundant here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looking at cpython code unicode.encode uses a getdefaultencoding function which return "utf-8" (which is a different behaviour than bytes.decode). You could do it add it and at the same time add the getdefaultencoding to the sys module
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
About
u8
, we don't have codec for now. So it is not redundant at this time. We can refactor it later once we have codec. About getdefaultencoding, it is good point, Thanks. I will go with it for next step.