Skip to content

Commit 091c602

Browse files
Remove PyUnicodeWriter compat shim
1 parent 0919850 commit 091c602

File tree

4 files changed

+16
-159
lines changed

4 files changed

+16
-159
lines changed

pyo3-ffi/src/compat/py_3_14.rs

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -24,88 +24,3 @@ compat_function!(
2424
}
2525
}
2626
);
27-
28-
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
29-
compat_function!(
30-
originally_defined_for(all(Py_3_14, not(Py_LIMITED_API)));
31-
32-
pub unsafe fn PyUnicodeWriter_Create(length: crate::Py_ssize_t) -> *mut crate::PyUnicodeWriter {
33-
if length < 0 {
34-
crate::PyErr_SetString(
35-
crate::PyExc_ValueError,
36-
c_str!("length must be positive").as_ptr(),
37-
);
38-
return std::ptr::null_mut();
39-
}
40-
41-
let size = std::mem::size_of::<crate::_PyUnicodeWriter>();
42-
let writer: *mut crate::_PyUnicodeWriter = crate::PyMem_Malloc(size).cast();
43-
crate::_PyUnicodeWriter_Init(writer);
44-
if crate::_PyUnicodeWriter_Prepare(writer, length, 127) < 0 {
45-
PyUnicodeWriter_Discard(writer.cast());
46-
return std::ptr::null_mut();
47-
}
48-
(*writer).overallocate = 1;
49-
writer.cast()
50-
}
51-
);
52-
53-
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
54-
compat_function!(
55-
originally_defined_for(all(Py_3_14, not(Py_LIMITED_API)));
56-
57-
pub unsafe fn PyUnicodeWriter_Finish(writer: *mut crate::PyUnicodeWriter) -> *mut crate::PyObject {
58-
let str = crate::_PyUnicodeWriter_Finish(writer.cast());
59-
crate::PyMem_Free(writer.cast());
60-
str
61-
}
62-
);
63-
64-
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
65-
compat_function!(
66-
originally_defined_for(all(Py_3_14, not(Py_LIMITED_API)));
67-
68-
pub unsafe fn PyUnicodeWriter_Discard(writer: *mut crate::PyUnicodeWriter) -> () {
69-
crate::_PyUnicodeWriter_Dealloc(writer.cast());
70-
crate::PyMem_Free(writer.cast())
71-
}
72-
);
73-
74-
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
75-
compat_function!(
76-
originally_defined_for(all(Py_3_14, not(Py_LIMITED_API)));
77-
78-
pub unsafe fn PyUnicodeWriter_WriteChar(writer: *mut crate::PyUnicodeWriter, ch: crate::Py_UCS4) -> std::os::raw::c_int {
79-
if ch > 0x10ffff {
80-
crate::PyErr_SetString(
81-
crate::PyExc_ValueError,
82-
c_str!("character must be in range(0x110000)").as_ptr(),
83-
);
84-
return -1;
85-
}
86-
87-
crate::_PyUnicodeWriter_WriteChar(writer.cast(), ch)
88-
}
89-
);
90-
91-
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
92-
compat_function!(
93-
originally_defined_for(all(Py_3_14, not(Py_LIMITED_API)));
94-
95-
pub unsafe fn PyUnicodeWriter_WriteUTF8(writer: *mut crate::PyUnicodeWriter,str: *const std::os::raw::c_char, size: crate::Py_ssize_t) -> std::os::raw::c_int {
96-
let size = if size < 0 {
97-
libc::strlen(str) as isize
98-
} else {
99-
size
100-
};
101-
102-
let py_str = crate::PyUnicode_FromStringAndSize(str, size);
103-
if py_str.is_null() {
104-
return -1;
105-
}
106-
107-
let result = crate::_PyUnicodeWriter_WriteStr(writer.cast(), py_str);
108-
crate::Py_DECREF(py_str);
109-
result
110-
}
111-
);

pyo3-ffi/src/cpython/unicodeobject.rs

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -689,53 +689,15 @@ extern "C" {
689689
#[cfg(Py_3_14)]
690690
opaque_struct!(pub PyUnicodeWriter);
691691

692-
#[cfg(not(Py_3_14))]
693-
pub type PyUnicodeWriter = _PyUnicodeWriter;
694-
695-
#[cfg(not(Py_3_14))]
696-
#[doc(hidden)]
697-
#[repr(C)]
698-
pub struct _PyUnicodeWriter {
699-
buffer: *mut PyObject,
700-
data: *mut c_void,
701-
kind: c_int,
702-
pub(crate) maxchar: Py_UCS4,
703-
pub(crate) size: Py_ssize_t,
704-
pub(crate) pos: Py_ssize_t,
705-
min_length: Py_ssize_t,
706-
min_char: Py_UCS4,
707-
pub(crate) overallocate: c_char,
708-
readonly: c_char,
709-
}
710-
711692
extern "C" {
712693
#[cfg(Py_3_14)]
713694
pub fn PyUnicodeWriter_Create(length: Py_ssize_t) -> *mut PyUnicodeWriter;
714695
#[cfg(Py_3_14)]
715696
pub fn PyUnicodeWriter_Finish(writer: *mut PyUnicodeWriter) -> *mut PyObject;
716-
#[cfg(not(Py_3_14))]
717-
pub(crate) fn _PyUnicodeWriter_Finish(writer: *mut _PyUnicodeWriter) -> *mut PyObject;
718697
#[cfg(Py_3_14)]
719698
pub fn PyUnicodeWriter_Discard(writer: *mut PyUnicodeWriter);
720-
#[cfg(not(Py_3_14))]
721-
pub(crate) fn _PyUnicodeWriter_Dealloc(writer: *mut _PyUnicodeWriter);
722-
#[cfg(not(Py_3_14))]
723-
pub(crate) fn _PyUnicodeWriter_Init(writer: *mut _PyUnicodeWriter);
724-
#[cfg(not(Py_3_14))]
725-
pub(crate) fn _PyUnicodeWriter_PrepareInternal(
726-
writer: *mut _PyUnicodeWriter,
727-
length: Py_ssize_t,
728-
maxchars: Py_UCS4,
729-
) -> c_int;
730699
#[cfg(Py_3_14)]
731700
pub fn PyUnicodeWriter_WriteChar(writer: *mut PyUnicodeWriter, ch: Py_UCS4) -> c_int;
732-
#[cfg(not(Py_3_14))]
733-
pub(crate) fn _PyUnicodeWriter_WriteChar(writer: *mut _PyUnicodeWriter, ch: Py_UCS4) -> c_int;
734-
#[cfg(not(Py_3_14))]
735-
pub(crate) fn _PyUnicodeWriter_WriteStr(
736-
writer: *mut _PyUnicodeWriter,
737-
str: *mut PyObject,
738-
) -> c_int;
739701
#[cfg(Py_3_14)]
740702
pub fn PyUnicodeWriter_WriteUTF8(
741703
writer: *mut PyUnicodeWriter,
@@ -744,24 +706,6 @@ extern "C" {
744706
) -> c_int;
745707
}
746708

747-
#[cfg(not(Py_3_14))]
748-
#[inline(always)]
749-
pub(crate) unsafe fn _PyUnicodeWriter_Prepare(
750-
writer: *mut _PyUnicodeWriter,
751-
length: Py_ssize_t,
752-
maxchars: Py_UCS4,
753-
) -> c_int {
754-
if maxchars <= (*writer).maxchar && length <= (*writer).size - (*writer).pos {
755-
return 0;
756-
}
757-
758-
if length == 0 {
759-
return 0;
760-
}
761-
762-
_PyUnicodeWriter_PrepareInternal(writer, length, maxchars)
763-
}
764-
765709
// skipped _PyUnicodeWriter
766710
// skipped _PyUnicodeWriter_Init
767711
// skipped _PyUnicodeWriter_Prepare

src/fmt.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
//! constructing Python strings using Rust's `fmt::Write` trait.
33
//! It allows for incremental string construction, without the need for repeated allocations, and
44
//! is particularly useful for building strings in a performance-sensitive context.
5-
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
5+
#[cfg(Py_3_14)]
66
use {
7-
crate::ffi::compat::{
7+
crate::ffi::{
88
PyUnicodeWriter_Create, PyUnicodeWriter_Discard, PyUnicodeWriter_Finish,
99
PyUnicodeWriter_WriteChar, PyUnicodeWriter_WriteUTF8,
1010
},
@@ -23,8 +23,7 @@ use {
2323
#[macro_export]
2424
macro_rules! py_format {
2525
($py: expr, $($arg:tt)*) => {{
26-
let format_args = format_args!($($arg)*);
27-
if let Some(static_string) = format_args.as_str() {
26+
if let Some(static_string) = format_args!($($arg)*).as_str() {
2827
static INTERNED: $crate::sync::PyOnceLock<$crate::Py<$crate::types::PyString>> = $crate::sync::PyOnceLock::new();
2928
Ok(
3029
INTERNED
@@ -33,20 +32,20 @@ macro_rules! py_format {
3332
.to_owned()
3433
)
3534
} else {
36-
$crate::types::PyString::from_fmt($py, format_args)
35+
$crate::types::PyString::from_fmt($py, format_args!($($arg)*))
3736
}
3837
}}
3938
}
4039

41-
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
40+
#[cfg(Py_3_14)]
4241
/// The `PyUnicodeWriter` is a utility for efficiently constructing Python strings
4342
pub struct PyUnicodeWriter<'py> {
4443
python: Python<'py>,
4544
writer: NonNull<ffi::PyUnicodeWriter>,
4645
last_error: Option<PyErr>,
4746
}
4847

49-
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
48+
#[cfg(Py_3_14)]
5049
impl<'py> PyUnicodeWriter<'py> {
5150
/// Creates a new `PyUnicodeWriter`.
5251
pub fn new(py: Python<'py>) -> PyResult<Self> {
@@ -93,7 +92,7 @@ impl<'py> PyUnicodeWriter<'py> {
9392
}
9493
}
9594

96-
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
95+
#[cfg(Py_3_14)]
9796
impl fmt::Write for PyUnicodeWriter<'_> {
9897
fn write_str(&mut self, s: &str) -> fmt::Result {
9998
let result = unsafe {
@@ -118,7 +117,7 @@ impl fmt::Write for PyUnicodeWriter<'_> {
118117
}
119118
}
120119

121-
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
120+
#[cfg(Py_3_14)]
122121
impl Drop for PyUnicodeWriter<'_> {
123122
fn drop(&mut self) {
124123
unsafe {
@@ -127,7 +126,7 @@ impl Drop for PyUnicodeWriter<'_> {
127126
}
128127
}
129128

130-
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
129+
#[cfg(Py_3_14)]
131130
impl<'py> IntoPyObject<'py> for PyUnicodeWriter<'py> {
132131
type Target = PyString;
133132
type Output = Bound<'py, Self::Target>;
@@ -140,14 +139,14 @@ impl<'py> IntoPyObject<'py> for PyUnicodeWriter<'py> {
140139

141140
#[cfg(test)]
142141
mod tests {
143-
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
142+
#[cfg(Py_3_14)]
144143
use super::*;
145144
use crate::types::PyStringMethods;
146145
use crate::{IntoPyObject, Python};
147146

148147
#[test]
149148
#[allow(clippy::write_literal)]
150-
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
149+
#[cfg(Py_3_14)]
151150
fn unicode_writer_test() {
152151
use std::fmt::Write;
153152
Python::attach(|py| {

src/types/string.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
#[cfg(not(Py_LIMITED_API))]
22
use crate::exceptions::PyUnicodeDecodeError;
33
use crate::ffi_ptr_ext::FfiPtrExt;
4-
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
5-
use crate::fmt::PyUnicodeWriter;
64
use crate::instance::Borrowed;
75
use crate::py_result_ext::PyResultExt;
86
use crate::types::bytes::PyBytesMethods;
97
use crate::types::PyBytes;
108
use crate::{ffi, Bound, Py, PyAny, PyResult, Python};
119
use std::borrow::Cow;
1210
use std::ffi::{CStr, CString};
13-
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
14-
use std::fmt::Write as _;
1511
use std::{fmt, str};
1612

1713
/// Represents raw data backing a Python `str`.
@@ -264,16 +260,19 @@ impl PyString {
264260
return Ok(PyString::new(py, static_string));
265261
};
266262

267-
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
263+
#[cfg(Py_3_14)]
268264
{
265+
use crate::fmt::PyUnicodeWriter;
266+
use std::fmt::Write as _;
267+
269268
let mut writer = PyUnicodeWriter::new(py)?;
270269
writer
271270
.write_fmt(args)
272271
.map_err(|_| writer.take_error().expect("expected error"))?;
273272
writer.into_py_string()
274273
}
275274

276-
#[cfg(any(Py_LIMITED_API, PyPy))]
275+
#[cfg(not(Py_3_14))]
277276
{
278277
Ok(PyString::new(py, &format!("{args}")))
279278
}

0 commit comments

Comments
 (0)