@@ -5,7 +5,7 @@ use core::ffi::{CStr, c_char, c_int};
55use core:: ptr:: NonNull ;
66use core:: slice;
77use core:: str;
8- use rustpython_vm:: builtins:: { PyStr , PyStrRef } ;
8+ use rustpython_vm:: builtins:: { PyBytesRef , PyStr , PyStrRef , PyUtf8StrRef } ;
99use rustpython_vm:: common:: wtf8:: { CodePoint , Wtf8Buf } ;
1010use rustpython_vm:: convert:: ToPyObject ;
1111use rustpython_vm:: { AsObject , PyObjectRef , PyResult , VirtualMachine } ;
@@ -93,78 +93,50 @@ pub unsafe extern "C" fn PyUnicode_AsUTF8AndSize(
9393 } )
9494}
9595
96+ fn encode_unicode (
97+ vm : & VirtualMachine ,
98+ unicode : * mut PyObject ,
99+ encoding : & str ,
100+ errors : Option < PyUtf8StrRef > ,
101+ ) -> PyResult < PyBytesRef > {
102+ let unicode = unsafe { & * unicode }
103+ . try_downcast_ref :: < PyStr > ( vm) ?
104+ . to_owned ( ) ;
105+ vm. state
106+ . codec_registry
107+ . encode_text ( unicode, encoding, errors, vm)
108+ }
109+
96110#[ unsafe( no_mangle) ]
97111pub unsafe extern "C" fn PyUnicode_AsASCIIString ( unicode : * mut PyObject ) -> * mut PyObject {
98- with_vm ( |vm| {
99- let unicode = unsafe { & * unicode }
100- . try_downcast_ref :: < PyStr > ( vm) ?
101- . to_owned ( ) ;
102- vm. state
103- . codec_registry
104- . encode_text ( unicode, "ascii" , None , vm)
105- } )
112+ with_vm ( |vm| encode_unicode ( vm, unicode, "ascii" , None ) )
106113}
107114
108115#[ unsafe( no_mangle) ]
109116pub unsafe extern "C" fn PyUnicode_AsLatin1String ( unicode : * mut PyObject ) -> * mut PyObject {
110- with_vm ( |vm| {
111- let unicode = unsafe { & * unicode }
112- . try_downcast_ref :: < PyStr > ( vm) ?
113- . to_owned ( ) ;
114- vm. state
115- . codec_registry
116- . encode_text ( unicode, "latin-1" , None , vm)
117- } )
117+ with_vm ( |vm| encode_unicode ( vm, unicode, "latin-1" , None ) )
118118}
119119
120120#[ unsafe( no_mangle) ]
121121pub unsafe extern "C" fn PyUnicode_AsRawUnicodeEscapeString (
122122 unicode : * mut PyObject ,
123123) -> * mut PyObject {
124- with_vm ( |vm| {
125- let unicode = unsafe { & * unicode }
126- . try_downcast_ref :: < PyStr > ( vm) ?
127- . to_owned ( ) ;
128- vm. state
129- . codec_registry
130- . encode_text ( unicode, "raw-unicode-escape" , None , vm)
131- } )
124+ with_vm ( |vm| encode_unicode ( vm, unicode, "raw-unicode-escape" , None ) )
132125}
133126
134127#[ unsafe( no_mangle) ]
135128pub unsafe extern "C" fn PyUnicode_AsUTF16String ( unicode : * mut PyObject ) -> * mut PyObject {
136- with_vm ( |vm| {
137- let unicode = unsafe { & * unicode }
138- . try_downcast_ref :: < PyStr > ( vm) ?
139- . to_owned ( ) ;
140- vm. state
141- . codec_registry
142- . encode_text ( unicode, "utf-16" , None , vm)
143- } )
129+ with_vm ( |vm| encode_unicode ( vm, unicode, "utf-16" , None ) )
144130}
145131
146132#[ unsafe( no_mangle) ]
147133pub unsafe extern "C" fn PyUnicode_AsUTF32String ( unicode : * mut PyObject ) -> * mut PyObject {
148- with_vm ( |vm| {
149- let unicode = unsafe { & * unicode }
150- . try_downcast_ref :: < PyStr > ( vm) ?
151- . to_owned ( ) ;
152- vm. state
153- . codec_registry
154- . encode_text ( unicode, "utf-32" , None , vm)
155- } )
134+ with_vm ( |vm| encode_unicode ( vm, unicode, "utf-32" , None ) )
156135}
157136
158137#[ unsafe( no_mangle) ]
159138pub unsafe extern "C" fn PyUnicode_AsUnicodeEscapeString ( unicode : * mut PyObject ) -> * mut PyObject {
160- with_vm ( |vm| {
161- let unicode = unsafe { & * unicode }
162- . try_downcast_ref :: < PyStr > ( vm) ?
163- . to_owned ( ) ;
164- vm. state
165- . codec_registry
166- . encode_text ( unicode, "unicode-escape" , None , vm)
167- } )
139+ with_vm ( |vm| encode_unicode ( vm, unicode, "unicode-escape" , None ) )
168140}
169141
170142#[ unsafe( no_mangle) ]
@@ -174,28 +146,16 @@ pub unsafe extern "C" fn PyUnicode_AsEncodedString(
174146 errors : * const c_char ,
175147) -> * mut PyObject {
176148 with_vm ( |vm| {
177- let unicode = unsafe { & * unicode }
178- . try_downcast_ref :: < PyStr > ( vm) ?
179- . to_owned ( ) ;
180149 let encoding = unsafe { encoding. try_as_str_opt ( vm) } ?. unwrap_or ( "utf-8" ) ;
181150 let errors =
182151 unsafe { errors. try_as_str_opt ( vm) } ?. map ( |errors| vm. ctx . new_utf8_str ( errors) ) ;
183- vm. state
184- . codec_registry
185- . encode_text ( unicode, encoding, errors, vm)
152+ encode_unicode ( vm, unicode, encoding, errors)
186153 } )
187154}
188155
189156#[ unsafe( no_mangle) ]
190157pub unsafe extern "C" fn PyUnicode_AsUTF8String ( unicode : * mut PyObject ) -> * mut PyObject {
191- with_vm ( |vm| {
192- let unicode = unsafe { & * unicode }
193- . try_downcast_ref :: < PyStr > ( vm) ?
194- . to_owned ( ) ;
195- vm. state
196- . codec_registry
197- . encode_text ( unicode, "utf-8" , None , vm)
198- } )
158+ with_vm ( |vm| encode_unicode ( vm, unicode, "utf-8" , None ) )
199159}
200160
201161#[ unsafe( no_mangle) ]
@@ -414,14 +374,11 @@ pub(crate) fn decode_fsdefault_and_size(
414374#[ unsafe( no_mangle) ]
415375pub unsafe extern "C" fn PyUnicode_EncodeFSDefault ( unicode : * mut PyObject ) -> * mut PyObject {
416376 with_vm ( |vm| {
417- let unicode = unsafe { & * unicode }
418- . try_downcast_ref :: < PyStr > ( vm) ?
419- . to_owned ( ) ;
420- vm. state . codec_registry . encode_text (
377+ encode_unicode (
378+ vm,
421379 unicode,
422380 vm. fs_encoding ( ) . as_str ( ) ,
423381 Some ( vm. fs_encode_errors ( ) . to_owned ( ) ) ,
424- vm,
425382 )
426383 } )
427384}
0 commit comments