|
1 | 1 | pub(crate) use _codecs::module_def; |
2 | 2 |
|
3 | | -#[pymodule] |
| 3 | +use crate::common::static_cell::StaticCell; |
| 4 | + |
| 5 | +#[pymodule(with(_codecs_windows))] |
4 | 6 | mod _codecs { |
5 | 7 | use crate::codecs::{ErrorsHandler, PyDecodeContext, PyEncodeContext}; |
6 | 8 | use crate::common::encodings; |
@@ -202,26 +204,146 @@ mod _codecs { |
202 | 204 |
|
203 | 205 | // TODO: implement these codecs in Rust! |
204 | 206 |
|
205 | | - use crate::common::static_cell::StaticCell; |
206 | | - #[inline] |
207 | | - fn delegate_pycodecs( |
208 | | - cell: &'static StaticCell<PyObjectRef>, |
209 | | - name: &'static str, |
210 | | - args: FuncArgs, |
211 | | - vm: &VirtualMachine, |
212 | | - ) -> PyResult { |
213 | | - let f = cell.get_or_try_init(|| { |
214 | | - let module = vm.import("_pycodecs", 0)?; |
215 | | - module.get_attr(name, vm) |
216 | | - })?; |
217 | | - f.call(args, vm) |
| 207 | + macro_rules! delegate_pycodecs { |
| 208 | + ($name:ident, $args:ident, $vm:ident) => {{ |
| 209 | + rustpython_common::static_cell!( |
| 210 | + static FUNC: PyObjectRef; |
| 211 | + ); |
| 212 | + super::delegate_pycodecs(&FUNC, stringify!($name), $args, $vm) |
| 213 | + }}; |
| 214 | + } |
| 215 | + |
| 216 | + #[pyfunction] |
| 217 | + fn readbuffer_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 218 | + delegate_pycodecs!(readbuffer_encode, args, vm) |
| 219 | + } |
| 220 | + #[pyfunction] |
| 221 | + fn escape_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 222 | + delegate_pycodecs!(escape_encode, args, vm) |
| 223 | + } |
| 224 | + #[pyfunction] |
| 225 | + fn escape_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 226 | + delegate_pycodecs!(escape_decode, args, vm) |
| 227 | + } |
| 228 | + #[pyfunction] |
| 229 | + fn unicode_escape_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 230 | + delegate_pycodecs!(unicode_escape_encode, args, vm) |
| 231 | + } |
| 232 | + #[pyfunction] |
| 233 | + fn unicode_escape_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 234 | + delegate_pycodecs!(unicode_escape_decode, args, vm) |
| 235 | + } |
| 236 | + #[pyfunction] |
| 237 | + fn raw_unicode_escape_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 238 | + delegate_pycodecs!(raw_unicode_escape_encode, args, vm) |
| 239 | + } |
| 240 | + #[pyfunction] |
| 241 | + fn raw_unicode_escape_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 242 | + delegate_pycodecs!(raw_unicode_escape_decode, args, vm) |
| 243 | + } |
| 244 | + #[pyfunction] |
| 245 | + fn utf_7_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 246 | + delegate_pycodecs!(utf_7_encode, args, vm) |
| 247 | + } |
| 248 | + #[pyfunction] |
| 249 | + fn utf_7_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 250 | + delegate_pycodecs!(utf_7_decode, args, vm) |
| 251 | + } |
| 252 | + #[pyfunction] |
| 253 | + fn utf_16_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 254 | + delegate_pycodecs!(utf_16_encode, args, vm) |
| 255 | + } |
| 256 | + #[pyfunction] |
| 257 | + fn utf_16_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 258 | + delegate_pycodecs!(utf_16_decode, args, vm) |
| 259 | + } |
| 260 | + #[pyfunction] |
| 261 | + fn charmap_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 262 | + delegate_pycodecs!(charmap_encode, args, vm) |
| 263 | + } |
| 264 | + #[pyfunction] |
| 265 | + fn charmap_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 266 | + delegate_pycodecs!(charmap_decode, args, vm) |
| 267 | + } |
| 268 | + #[pyfunction] |
| 269 | + fn charmap_build(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 270 | + delegate_pycodecs!(charmap_build, args, vm) |
| 271 | + } |
| 272 | + #[pyfunction] |
| 273 | + fn utf_16_le_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 274 | + delegate_pycodecs!(utf_16_le_encode, args, vm) |
| 275 | + } |
| 276 | + #[pyfunction] |
| 277 | + fn utf_16_le_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 278 | + delegate_pycodecs!(utf_16_le_decode, args, vm) |
| 279 | + } |
| 280 | + #[pyfunction] |
| 281 | + fn utf_16_be_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 282 | + delegate_pycodecs!(utf_16_be_encode, args, vm) |
| 283 | + } |
| 284 | + #[pyfunction] |
| 285 | + fn utf_16_be_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 286 | + delegate_pycodecs!(utf_16_be_decode, args, vm) |
| 287 | + } |
| 288 | + #[pyfunction] |
| 289 | + fn utf_16_ex_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 290 | + delegate_pycodecs!(utf_16_ex_decode, args, vm) |
| 291 | + } |
| 292 | + #[pyfunction] |
| 293 | + fn utf_32_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 294 | + delegate_pycodecs!(utf_32_encode, args, vm) |
| 295 | + } |
| 296 | + #[pyfunction] |
| 297 | + fn utf_32_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 298 | + delegate_pycodecs!(utf_32_decode, args, vm) |
| 299 | + } |
| 300 | + #[pyfunction] |
| 301 | + fn utf_32_le_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 302 | + delegate_pycodecs!(utf_32_le_encode, args, vm) |
| 303 | + } |
| 304 | + #[pyfunction] |
| 305 | + fn utf_32_le_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 306 | + delegate_pycodecs!(utf_32_le_decode, args, vm) |
| 307 | + } |
| 308 | + #[pyfunction] |
| 309 | + fn utf_32_be_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 310 | + delegate_pycodecs!(utf_32_be_encode, args, vm) |
| 311 | + } |
| 312 | + #[pyfunction] |
| 313 | + fn utf_32_be_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
| 314 | + delegate_pycodecs!(utf_32_be_decode, args, vm) |
218 | 315 | } |
| 316 | +} |
| 317 | + |
| 318 | +#[inline] |
| 319 | +fn delegate_pycodecs( |
| 320 | + cell: &'static StaticCell<crate::PyObjectRef>, |
| 321 | + name: &'static str, |
| 322 | + args: crate::function::FuncArgs, |
| 323 | + vm: &crate::VirtualMachine, |
| 324 | +) -> crate::PyResult { |
| 325 | + let f = cell.get_or_try_init(|| { |
| 326 | + let module = vm.import("_pycodecs", 0)?; |
| 327 | + module.get_attr(name, vm) |
| 328 | + })?; |
| 329 | + f.call(args, vm) |
| 330 | +} |
| 331 | + |
| 332 | +#[pymodule(sub)] |
| 333 | +mod _codecs_windows { |
| 334 | + #[cfg(not(windows))] |
| 335 | + use crate::{PyObjectRef, function::FuncArgs}; |
| 336 | + use crate::{PyResult, VirtualMachine}; |
| 337 | + #[cfg(windows)] |
| 338 | + use crate::{builtins::PyStrRef, function::ArgBytesLike}; |
| 339 | + |
| 340 | + #[cfg(not(windows))] |
219 | 341 | macro_rules! delegate_pycodecs { |
220 | 342 | ($name:ident, $args:ident, $vm:ident) => {{ |
221 | 343 | rustpython_common::static_cell!( |
222 | 344 | static FUNC: PyObjectRef; |
223 | 345 | ); |
224 | | - delegate_pycodecs(&FUNC, stringify!($name), $args, $vm) |
| 346 | + super::delegate_pycodecs(&FUNC, stringify!($name), $args, $vm) |
225 | 347 | }}; |
226 | 348 | } |
227 | 349 |
|
@@ -869,105 +991,4 @@ mod _codecs { |
869 | 991 | fn code_page_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
870 | 992 | delegate_pycodecs!(code_page_decode, args, vm) |
871 | 993 | } |
872 | | - |
873 | | - #[pyfunction] |
874 | | - fn readbuffer_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
875 | | - delegate_pycodecs!(readbuffer_encode, args, vm) |
876 | | - } |
877 | | - #[pyfunction] |
878 | | - fn escape_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
879 | | - delegate_pycodecs!(escape_encode, args, vm) |
880 | | - } |
881 | | - #[pyfunction] |
882 | | - fn escape_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
883 | | - delegate_pycodecs!(escape_decode, args, vm) |
884 | | - } |
885 | | - #[pyfunction] |
886 | | - fn unicode_escape_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
887 | | - delegate_pycodecs!(unicode_escape_encode, args, vm) |
888 | | - } |
889 | | - #[pyfunction] |
890 | | - fn unicode_escape_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
891 | | - delegate_pycodecs!(unicode_escape_decode, args, vm) |
892 | | - } |
893 | | - #[pyfunction] |
894 | | - fn raw_unicode_escape_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
895 | | - delegate_pycodecs!(raw_unicode_escape_encode, args, vm) |
896 | | - } |
897 | | - #[pyfunction] |
898 | | - fn raw_unicode_escape_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
899 | | - delegate_pycodecs!(raw_unicode_escape_decode, args, vm) |
900 | | - } |
901 | | - #[pyfunction] |
902 | | - fn utf_7_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
903 | | - delegate_pycodecs!(utf_7_encode, args, vm) |
904 | | - } |
905 | | - #[pyfunction] |
906 | | - fn utf_7_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
907 | | - delegate_pycodecs!(utf_7_decode, args, vm) |
908 | | - } |
909 | | - #[pyfunction] |
910 | | - fn utf_16_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
911 | | - delegate_pycodecs!(utf_16_encode, args, vm) |
912 | | - } |
913 | | - #[pyfunction] |
914 | | - fn utf_16_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
915 | | - delegate_pycodecs!(utf_16_decode, args, vm) |
916 | | - } |
917 | | - #[pyfunction] |
918 | | - fn charmap_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
919 | | - delegate_pycodecs!(charmap_encode, args, vm) |
920 | | - } |
921 | | - #[pyfunction] |
922 | | - fn charmap_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
923 | | - delegate_pycodecs!(charmap_decode, args, vm) |
924 | | - } |
925 | | - #[pyfunction] |
926 | | - fn charmap_build(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
927 | | - delegate_pycodecs!(charmap_build, args, vm) |
928 | | - } |
929 | | - #[pyfunction] |
930 | | - fn utf_16_le_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
931 | | - delegate_pycodecs!(utf_16_le_encode, args, vm) |
932 | | - } |
933 | | - #[pyfunction] |
934 | | - fn utf_16_le_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
935 | | - delegate_pycodecs!(utf_16_le_decode, args, vm) |
936 | | - } |
937 | | - #[pyfunction] |
938 | | - fn utf_16_be_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
939 | | - delegate_pycodecs!(utf_16_be_encode, args, vm) |
940 | | - } |
941 | | - #[pyfunction] |
942 | | - fn utf_16_be_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
943 | | - delegate_pycodecs!(utf_16_be_decode, args, vm) |
944 | | - } |
945 | | - #[pyfunction] |
946 | | - fn utf_16_ex_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
947 | | - delegate_pycodecs!(utf_16_ex_decode, args, vm) |
948 | | - } |
949 | | - #[pyfunction] |
950 | | - fn utf_32_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
951 | | - delegate_pycodecs!(utf_32_encode, args, vm) |
952 | | - } |
953 | | - #[pyfunction] |
954 | | - fn utf_32_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
955 | | - delegate_pycodecs!(utf_32_decode, args, vm) |
956 | | - } |
957 | | - #[pyfunction] |
958 | | - fn utf_32_le_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
959 | | - delegate_pycodecs!(utf_32_le_encode, args, vm) |
960 | | - } |
961 | | - #[pyfunction] |
962 | | - fn utf_32_le_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
963 | | - delegate_pycodecs!(utf_32_le_decode, args, vm) |
964 | | - } |
965 | | - #[pyfunction] |
966 | | - fn utf_32_be_encode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
967 | | - delegate_pycodecs!(utf_32_be_encode, args, vm) |
968 | | - } |
969 | | - #[pyfunction] |
970 | | - fn utf_32_be_decode(args: FuncArgs, vm: &VirtualMachine) -> PyResult { |
971 | | - delegate_pycodecs!(utf_32_be_decode, args, vm) |
972 | | - } |
973 | 994 | } |
0 commit comments