File tree Expand file tree Collapse file tree 3 files changed +26
-1
lines changed Expand file tree Collapse file tree 3 files changed +26
-1
lines changed Original file line number Diff line number Diff line change 11Added ` _PyLong_Sign `
2+ Add ` PyBytes_AS_STRING `
Original file line number Diff line number Diff line change 11use crate :: object:: * ;
22use crate :: Py_ssize_t ;
3- #[ cfg( not( any ( PyPy , GraalPy , Py_LIMITED_API ) ) ) ]
3+ #[ cfg( not( Py_LIMITED_API ) ) ]
44use std:: os:: raw:: c_char;
55use std:: os:: raw:: c_int;
66
@@ -23,3 +23,12 @@ extern "C" {
2323 #[ cfg_attr( PyPy , link_name = "_PyPyBytes_Resize" ) ]
2424 pub fn _PyBytes_Resize ( bytes : * mut * mut PyObject , newsize : Py_ssize_t ) -> c_int ;
2525}
26+
27+ #[ cfg( not( Py_LIMITED_API ) ) ]
28+ #[ inline]
29+ pub unsafe fn PyBytes_AS_STRING ( op : * mut PyObject ) -> * const c_char {
30+ #[ cfg( not( any( PyPy , GraalPy ) ) ) ]
31+ return & ( * op. cast :: < PyBytesObject > ( ) ) . ob_sval as * const c_char ;
32+ #[ cfg( any( PyPy , GraalPy , Py_LIMITED_API ) ) ]
33+ return crate :: PyBytes_AsString ( op) ;
34+ }
Original file line number Diff line number Diff line change @@ -365,4 +365,19 @@ mod tests {
365365 assert_eq ! ( * b, py_string) ;
366366 } )
367367 }
368+
369+ #[ test]
370+ #[ cfg( not( Py_LIMITED_API ) ) ]
371+ fn test_as_string ( ) {
372+ Python :: with_gil ( |py| {
373+ let b = b"hello, world" . as_slice ( ) ;
374+ let py_bytes = PyBytes :: new ( py, b) ;
375+ unsafe {
376+ assert_eq ! (
377+ ffi:: PyBytes_AsString ( py_bytes. as_ptr( ) ) as * const i8 ,
378+ ffi:: PyBytes_AS_STRING ( py_bytes. as_ptr( ) )
379+ ) ;
380+ }
381+ } )
382+ }
368383}
You can’t perform that action at this time.
0 commit comments