|
| 1 | +use crate::PyObject; |
| 2 | +use crate::object::define_py_check; |
| 3 | +use crate::pystate::with_vm; |
| 4 | +use core::ffi::{CStr, c_char, c_int}; |
| 5 | +use core::ptr::NonNull; |
| 6 | +use core::slice; |
| 7 | +use core::str; |
| 8 | +use rustpython_vm::PyObjectRef; |
| 9 | +use rustpython_vm::builtins::PyStr; |
| 10 | + |
| 11 | +define_py_check!(fn PyUnicode_Check, types.str_type); |
| 12 | +define_py_check!(exact fn PyUnicode_CheckExact, types.str_type); |
| 13 | + |
| 14 | +#[unsafe(no_mangle)] |
| 15 | +pub unsafe extern "C" fn PyUnicode_FromStringAndSize( |
| 16 | + s: *const c_char, |
| 17 | + len: isize, |
| 18 | +) -> *mut PyObject { |
| 19 | + with_vm(|vm| { |
| 20 | + let len: usize = len |
| 21 | + .try_into() |
| 22 | + .map_err(|_| vm.new_system_error("length must be non-negative"))?; |
| 23 | + |
| 24 | + let text = if s.is_null() { |
| 25 | + if len != 0 { |
| 26 | + return Err(vm.new_system_error( |
| 27 | + "PyUnicode_FromStringAndSize called with null data and non-zero len", |
| 28 | + )); |
| 29 | + } |
| 30 | + "" |
| 31 | + } else { |
| 32 | + let bytes = unsafe { slice::from_raw_parts(s.cast::<u8>(), len) }; |
| 33 | + str::from_utf8(bytes).expect("PyUnicode_FromStringAndSize got non-UTF8 data") |
| 34 | + }; |
| 35 | + |
| 36 | + Ok(vm.ctx.new_str(text)) |
| 37 | + }) |
| 38 | +} |
| 39 | + |
| 40 | +#[unsafe(no_mangle)] |
| 41 | +pub unsafe extern "C" fn PyUnicode_AsUTF8AndSize( |
| 42 | + obj: *mut PyObject, |
| 43 | + size: *mut isize, |
| 44 | +) -> *const c_char { |
| 45 | + with_vm(|vm| { |
| 46 | + let unicode = unsafe { &*obj }.try_downcast_ref::<PyStr>(vm)?; |
| 47 | + |
| 48 | + let str = unicode.to_str().ok_or_else(|| { |
| 49 | + vm.new_system_error("PyUnicode_AsUTF8AndSize only supports UTF-8 or ASCII strings") |
| 50 | + })?; |
| 51 | + |
| 52 | + if size.is_null() { |
| 53 | + // We do not support null size arguments because the returned string is not NULL terminated. |
| 54 | + return Err( |
| 55 | + vm.new_system_error("size argument to PyUnicode_AsUTF8AndSize cannot be null") |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + unsafe { *size = str.len() as isize }; |
| 60 | + Ok(str.as_ptr()) |
| 61 | + }) |
| 62 | +} |
| 63 | + |
| 64 | +#[unsafe(no_mangle)] |
| 65 | +pub unsafe extern "C" fn PyUnicode_AsEncodedString( |
| 66 | + unicode: *mut PyObject, |
| 67 | + encoding: *const c_char, |
| 68 | + errors: *const c_char, |
| 69 | +) -> *mut PyObject { |
| 70 | + with_vm(|vm| { |
| 71 | + let unicode = unsafe { &*unicode } |
| 72 | + .try_downcast_ref::<PyStr>(vm)? |
| 73 | + .to_owned(); |
| 74 | + let encoding = if encoding.is_null() { |
| 75 | + "utf-8" |
| 76 | + } else { |
| 77 | + unsafe { CStr::from_ptr(encoding) } |
| 78 | + .to_str() |
| 79 | + .expect("encoding must be valid UTF-8") |
| 80 | + }; |
| 81 | + let errors = if errors.is_null() { |
| 82 | + None |
| 83 | + } else { |
| 84 | + let errors = unsafe { CStr::from_ptr(errors) } |
| 85 | + .to_str() |
| 86 | + .expect("errors must be valid UTF-8"); |
| 87 | + Some(vm.ctx.new_utf8_str(errors)) |
| 88 | + }; |
| 89 | + vm.state |
| 90 | + .codec_registry |
| 91 | + .encode_text(unicode, encoding, errors, vm) |
| 92 | + }) |
| 93 | +} |
| 94 | + |
| 95 | +#[unsafe(no_mangle)] |
| 96 | +pub unsafe extern "C" fn PyUnicode_InternInPlace(string: *mut *mut PyObject) { |
| 97 | + with_vm(|vm| { |
| 98 | + let old_str = unsafe { PyObjectRef::from_raw(NonNull::new_unchecked(*string)) } |
| 99 | + .downcast_exact::<PyStr>(vm) |
| 100 | + .expect("PyUnicode_InternInPlace called with non-string object"); |
| 101 | + |
| 102 | + let interned: PyObjectRef = vm.ctx.intern_str(old_str).to_owned().into(); |
| 103 | + |
| 104 | + unsafe { *string = interned.into_raw().as_ptr() } |
| 105 | + }) |
| 106 | +} |
| 107 | + |
| 108 | +#[unsafe(no_mangle)] |
| 109 | +pub unsafe extern "C" fn PyUnicode_EqualToUTF8AndSize( |
| 110 | + unicode: *mut PyObject, |
| 111 | + string: *const c_char, |
| 112 | + size: isize, |
| 113 | +) -> c_int { |
| 114 | + with_vm(|vm| { |
| 115 | + let size = size.try_into().map_err(|_| { |
| 116 | + vm.new_system_error("Negative size passed to PyUnicode_EqualToUTF8AndSize") |
| 117 | + })?; |
| 118 | + |
| 119 | + let unicode = unsafe { &*unicode }.try_downcast_ref::<PyStr>(vm)?; |
| 120 | + let result = unsafe { |
| 121 | + let slice = slice::from_raw_parts(string as _, size); |
| 122 | + str::from_utf8(slice) |
| 123 | + } |
| 124 | + .ok() |
| 125 | + .and_then(|other| Some(unicode.to_str()? == other)) |
| 126 | + .unwrap_or(false); |
| 127 | + |
| 128 | + Ok(result) |
| 129 | + }) |
| 130 | +} |
| 131 | + |
| 132 | +#[cfg(false)] |
| 133 | +mod tests { |
| 134 | + use pyo3::intern; |
| 135 | + use pyo3::prelude::*; |
| 136 | + use pyo3::types::PyString; |
| 137 | + |
| 138 | + #[test] |
| 139 | + fn test_unicode() { |
| 140 | + Python::attach(|py| { |
| 141 | + let string = PyString::new(py, "Hello, World!"); |
| 142 | + assert!(string.is_instance_of::<PyString>()); |
| 143 | + assert_eq!(string.to_str().unwrap(), "Hello, World!"); |
| 144 | + assert_eq!(string, "Hello, World!"); |
| 145 | + }) |
| 146 | + } |
| 147 | + |
| 148 | + #[test] |
| 149 | + fn test_intern_str() { |
| 150 | + Python::attach(|py| { |
| 151 | + let _string = intern!(py, "Hello, World!"); |
| 152 | + }) |
| 153 | + } |
| 154 | +} |
0 commit comments