FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Refactor PyUtf8Str by youknowone · Pull Request #6047 · RustPython/RustPython · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .rs  (28) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
29 changes: 14 additions & 15 deletions stdlib/src/sqlite.rs
This file contains hidden or 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
Expand Up @@ -59,6 +59,7 @@ mod _sqlite {
builtins::{
PyBaseException, PyBaseExceptionRef, PyByteArray, PyBytes, PyDict, PyDictRef, PyFloat,
PyInt, PyIntRef, PySlice, PyStr, PyStrRef, PyTuple, PyTupleRef, PyType, PyTypeRef,
PyUtf8Str, PyUtf8StrRef,
},
convert::IntoObject,
function::{
Expand Down Expand Up @@ -851,7 +852,7 @@ mod _sqlite {
}

impl Callable for Connection {
type Args = (PyStrRef,);
type Args = (PyUtf8StrRef,);

fn call(zelf: &Py<Self>, args: Self::Args, vm: &VirtualMachine) -> PyResult {
if let Some(stmt) = Statement::new(zelf, args.0, vm)? {
Expand Down Expand Up @@ -986,7 +987,7 @@ mod _sqlite {
#[pymethod]
fn execute(
zelf: PyRef<Self>,
sql: PyStrRef,
sql: PyUtf8StrRef,
parameters: OptionalArg<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<PyRef<Cursor>> {
Expand All @@ -998,7 +999,7 @@ mod _sqlite {
#[pymethod]
fn executemany(
zelf: PyRef<Self>,
sql: PyStrRef,
sql: PyUtf8StrRef,
seq_of_params: ArgIterable,
vm: &VirtualMachine,
) -> PyResult<PyRef<Cursor>> {
Expand All @@ -1010,7 +1011,7 @@ mod _sqlite {
#[pymethod]
fn executescript(
zelf: PyRef<Self>,
script: PyStrRef,
script: PyUtf8StrRef,
vm: &VirtualMachine,
) -> PyResult<PyRef<Cursor>> {
let row_factory = zelf.row_factory.to_owned();
Expand Down Expand Up @@ -1159,11 +1160,10 @@ mod _sqlite {
#[pymethod]
fn create_collation(
&self,
name: PyStrRef,
name: PyUtf8StrRef,
callable: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<()> {
name.ensure_valid_utf8(vm)?;
let name = name.to_cstring(vm)?;
let db = self.db_lock(vm)?;
let Some(data) = CallbackData::new(callable.clone(), vm) else {
Expand Down Expand Up @@ -1491,7 +1491,7 @@ mod _sqlite {
#[pymethod]
fn execute(
zelf: PyRef<Self>,
sql: PyStrRef,
sql: PyUtf8StrRef,
parameters: OptionalArg<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<PyRef<Self>> {
Expand Down Expand Up @@ -1563,7 +1563,7 @@ mod _sqlite {
#[pymethod]
fn executemany(
zelf: PyRef<Self>,
sql: PyStrRef,
sql: PyUtf8StrRef,
seq_of_params: ArgIterable,
vm: &VirtualMachine,
) -> PyResult<PyRef<Self>> {
Expand Down Expand Up @@ -1637,11 +1637,9 @@ mod _sqlite {
#[pymethod]
fn executescript(
zelf: PyRef<Self>,
script: PyStrRef,
script: PyUtf8StrRef,
vm: &VirtualMachine,
) -> PyResult<PyRef<Self>> {
script.ensure_valid_utf8(vm)?;

let db = zelf.connection.db_lock(vm)?;

db.sql_limit(script.byte_len(), vm)?;
Expand Down Expand Up @@ -2375,10 +2373,9 @@ mod _sqlite {
impl Statement {
fn new(
connection: &Connection,
sql: PyStrRef,
sql: PyUtf8StrRef,
vm: &VirtualMachine,
) -> PyResult<Option<Self>> {
let sql = sql.try_into_utf8(vm)?;
if sql.as_str().contains('\0') {
return Err(new_programming_error(
vm,
Expand Down Expand Up @@ -2731,6 +2728,7 @@ mod _sqlite {
let val = val.to_f64();
unsafe { sqlite3_bind_double(self.st, pos, val) }
} else if let Some(val) = obj.downcast_ref::<PyStr>() {
let val = val.try_as_utf8(vm)?;
let (ptr, len) = str_to_ptr_len(val, vm)?;
unsafe { sqlite3_bind_text(self.st, pos, ptr, len, SQLITE_TRANSIENT()) }
} else if let Ok(buffer) = PyBuffer::try_from_borrowed_object(vm, obj) {
Expand Down Expand Up @@ -2990,6 +2988,7 @@ mod _sqlite {
} else if let Some(val) = val.downcast_ref::<PyFloat>() {
sqlite3_result_double(self.ctx, val.to_f64())
} else if let Some(val) = val.downcast_ref::<PyStr>() {
let val = val.try_as_utf8(vm)?;
let (ptr, len) = str_to_ptr_len(val, vm)?;
sqlite3_result_text(self.ctx, ptr, len, SQLITE_TRANSIENT())
} else if let Ok(buffer) = PyBuffer::try_from_borrowed_object(vm, val) {
Expand Down Expand Up @@ -3070,8 +3069,8 @@ mod _sqlite {
}
}

fn str_to_ptr_len(s: &PyStr, vm: &VirtualMachine) -> PyResult<(*const libc::c_char, i32)> {
let s_str = s.try_to_str(vm)?;
fn str_to_ptr_len(s: &PyUtf8Str, vm: &VirtualMachine) -> PyResult<(*const libc::c_char, i32)> {
let s_str = s.as_str();
let len = c_int::try_from(s_str.len())
.map_err(|_| vm.new_overflow_error("TEXT longer than INT_MAX bytes"))?;
let ptr = s_str.as_ptr().cast();
Expand Down
3 changes: 2 additions & 1 deletion vm/src/builtins/builtin_func.rs
This file contains hidden or 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
Expand Up @@ -2,6 +2,7 @@ use super::{PyStrInterned, PyStrRef, PyType, type_};
use crate::{
AsObject, Context, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
class::PyClassImpl,
common::wtf8::Wtf8,
convert::TryFromObject,
function::{FuncArgs, PyComparisonValue, PyMethodDef, PyMethodFlags, PyNativeFn},
types::{Callable, Comparable, PyComparisonOp, Representable, Unconstructible},
Expand All @@ -27,7 +28,7 @@ impl fmt::Debug for PyNativeFunction {
write!(
f,
"builtin function {}.{} ({:?}) self as instance of {:?}",
self.module.map_or("<unknown>", |m| m.as_str()),
self.module.map_or(Wtf8::new("<unknown>"), |m| m.as_wtf8()),
self.value.name,
self.value.flags,
self.zelf.as_ref().map(|z| z.class().name().to_owned())
Expand Down
2 changes: 1 addition & 1 deletion vm/src/builtins/mod.rs
This file contains hidden or 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
Expand Up @@ -59,7 +59,7 @@ pub(crate) mod bool_;
pub use bool_::PyBool;
#[path = "str.rs"]
pub(crate) mod pystr;
pub use pystr::{PyStr, PyStrInterned, PyStrRef};
pub use pystr::{PyStr, PyStrInterned, PyStrRef, PyUtf8Str, PyUtf8StrRef};
#[path = "super.rs"]
pub(crate) mod super_;
pub use super_::PySuper;
Expand Down
4 changes: 2 additions & 2 deletions vm/src/builtins/namespace.rs
This file contains hidden or 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
Expand Up @@ -89,8 +89,8 @@ impl Representable for PyNamespace {
let dict = zelf.as_object().dict().unwrap();
let mut parts = Vec::with_capacity(dict.__len__());
for (key, value) in dict {
let k = &key.repr(vm)?;
let key_str = k.as_str();
let k = key.repr(vm)?;
let key_str = k.as_wtf8();
let value_repr = value.repr(vm)?;
parts.push(format!("{}={}", &key_str[1..key_str.len() - 1], value_repr));
}
Expand Down
9 changes: 2 additions & 7 deletions vm/src/builtins/slice.rs
This file contains hidden or 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
Expand Up @@ -292,15 +292,10 @@ impl Representable for PySlice {
#[inline]
fn repr_str(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
let start_repr = zelf.start_ref(vm).repr(vm)?;
let stop_repr = &zelf.stop.repr(vm)?;
let stop_repr = zelf.stop.repr(vm)?;
let step_repr = zelf.step_ref(vm).repr(vm)?;

Ok(format!(
"slice({}, {}, {})",
start_repr.as_str(),
stop_repr.as_str(),
step_repr.as_str()
))
Ok(format!("slice({start_repr}, {stop_repr}, {step_repr})"))
}
}

Expand Down
Loading
Loading

Back | FazBrowse Home | New Git URL