@@ -346,101 +346,67 @@ pub fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
346
346
}
347
347
348
348
pub fn mk_module ( ctx : & PyContext ) -> PyObjectRef {
349
- let py_mod = ctx. new_module ( & "io" . to_string ( ) , ctx. new_scope ( None ) ) ;
350
-
351
- ctx. set_attr ( & py_mod, "open" , ctx. new_rustfunc ( io_open) ) ;
352
-
353
349
//IOBase the abstract base class of the IO Module
354
350
let io_base = ctx. new_class ( "IOBase" , ctx. object ( ) ) ;
355
- ctx. set_attr ( & py_mod, "IOBase" , io_base. clone ( ) ) ;
356
351
357
352
// IOBase Subclasses
358
353
let raw_io_base = ctx. new_class ( "RawIOBase" , ctx. object ( ) ) ;
359
- ctx. set_attr ( & py_mod, "RawIOBase" , raw_io_base. clone ( ) ) ;
360
-
361
- let buffered_io_base = {
362
- let buffered_io_base = ctx. new_class ( "BufferedIOBase" , io_base. clone ( ) ) ;
363
- ctx. set_attr (
364
- & buffered_io_base,
365
- "__init__" ,
366
- ctx. new_rustfunc ( buffered_io_base_init) ,
367
- ) ;
368
- buffered_io_base
369
- } ;
370
- ctx. set_attr ( & py_mod, "BufferedIOBase" , buffered_io_base. clone ( ) ) ;
354
+
355
+ let buffered_io_base = py_class ! ( ctx, "BufferedIOBase" , io_base. clone( ) , {
356
+ "__init__" => ctx. new_rustfunc( buffered_io_base_init)
357
+ } ) ;
371
358
372
359
//TextIO Base has no public constructor
373
- let text_io_base = {
374
- let text_io_base = ctx. new_class ( "TextIOBase" , io_base. clone ( ) ) ;
375
- ctx. set_attr ( & text_io_base, "read" , ctx. new_rustfunc ( text_io_base_read) ) ;
376
- text_io_base
377
- } ;
378
- ctx. set_attr ( & py_mod, "TextIOBase" , text_io_base. clone ( ) ) ;
360
+ let text_io_base = py_class ! ( ctx, "TextIOBase" , io_base. clone( ) , {
361
+ "read" => ctx. new_rustfunc( text_io_base_read)
362
+ } ) ;
379
363
380
364
// RawBaseIO Subclasses
381
- let file_io = {
382
- let file_io = ctx. new_class ( "FileIO" , raw_io_base. clone ( ) ) ;
383
- ctx. set_attr ( & file_io, "__init__" , ctx. new_rustfunc ( file_io_init) ) ;
384
- ctx. set_attr ( & file_io, "name" , ctx. str_type ( ) ) ;
385
- ctx. set_attr ( & file_io, "read" , ctx. new_rustfunc ( file_io_read) ) ;
386
- ctx. set_attr ( & file_io, "readinto" , ctx. new_rustfunc ( file_io_readinto) ) ;
387
- ctx. set_attr ( & file_io, "write" , ctx. new_rustfunc ( file_io_write) ) ;
388
- file_io
389
- } ;
390
- ctx. set_attr ( & py_mod, "FileIO" , file_io. clone ( ) ) ;
365
+ let file_io = py_class ! ( ctx, "FileIO" , raw_io_base. clone( ) , {
366
+ "__init__" => ctx. new_rustfunc( file_io_init) ,
367
+ "name" => ctx. str_type( ) ,
368
+ "read" => ctx. new_rustfunc( file_io_read) ,
369
+ "readinto" => ctx. new_rustfunc( file_io_readinto) ,
370
+ "write" => ctx. new_rustfunc( file_io_write)
371
+ } ) ;
391
372
392
373
// BufferedIOBase Subclasses
393
- let buffered_reader = {
394
- let buffered_reader = ctx. new_class ( "BufferedReader" , buffered_io_base. clone ( ) ) ;
395
- ctx. set_attr (
396
- & buffered_reader,
397
- "read" ,
398
- ctx. new_rustfunc ( buffered_reader_read) ,
399
- ) ;
400
- buffered_reader
401
- } ;
402
- ctx. set_attr ( & py_mod, "BufferedReader" , buffered_reader. clone ( ) ) ;
403
-
404
- let buffered_writer = {
405
- let buffered_writer = ctx. new_class ( "BufferedWriter" , buffered_io_base. clone ( ) ) ;
406
- ctx. set_attr (
407
- & buffered_writer,
408
- "write" ,
409
- ctx. new_rustfunc ( buffered_writer_write) ,
410
- ) ;
411
- buffered_writer
412
- } ;
413
- ctx. set_attr ( & py_mod, "BufferedWriter" , buffered_writer. clone ( ) ) ;
374
+ let buffered_reader = py_class ! ( ctx, "BufferedReader" , buffered_io_base. clone( ) , {
375
+ "read" => ctx. new_rustfunc( buffered_reader_read)
376
+ } ) ;
377
+
378
+ let buffered_writer = py_class ! ( ctx, "BufferedWriter" , buffered_io_base. clone( ) , {
379
+ "write" => ctx. new_rustfunc( buffered_writer_write)
380
+ } ) ;
414
381
415
382
//TextIOBase Subclass
416
- let text_io_wrapper = {
417
- let text_io_wrapper = ctx. new_class ( "TextIOWrapper" , text_io_base. clone ( ) ) ;
418
- ctx. set_attr (
419
- & text_io_wrapper,
420
- "__init__" ,
421
- ctx. new_rustfunc ( text_io_wrapper_init) ,
422
- ) ;
423
- text_io_wrapper
424
- } ;
425
- ctx. set_attr ( & py_mod, "TextIOWrapper" , text_io_wrapper. clone ( ) ) ;
383
+ let text_io_wrapper = py_class ! ( ctx, "TextIOWrapper" , text_io_base. clone( ) , {
384
+ "__init__" => ctx. new_rustfunc( text_io_wrapper_init)
385
+ } ) ;
426
386
427
387
//StringIO: in-memory text
428
- let string_io = {
429
- let string_io = ctx. new_class ( "StringIO" , text_io_base. clone ( ) ) ;
430
- ctx. set_attr ( & string_io, "__init__" , ctx. new_rustfunc ( string_io_init) ) ;
431
- ctx. set_attr ( & string_io, "getvalue" , ctx. new_rustfunc ( string_io_getvalue) ) ;
432
- string_io
433
- } ;
434
- ctx. set_attr ( & py_mod, "StringIO" , string_io) ;
435
-
388
+ let string_io = py_class ! ( ctx, "StringIO" , text_io_base. clone( ) , {
389
+ "__init__" => ctx. new_rustfunc( string_io_init) ,
390
+ "getvalue" => ctx. new_rustfunc( string_io_getvalue)
391
+ } ) ;
392
+
436
393
//BytesIO: in-memory bytes
437
- let bytes_io = {
438
- let bytes_io = ctx. new_class ( "BytesIO" , buffered_io_base. clone ( ) ) ;
439
- ctx. set_attr ( & bytes_io, "__init__" , ctx. new_rustfunc ( bytes_io_init) ) ;
440
- ctx. set_attr ( & bytes_io, "getvalue" , ctx. new_rustfunc ( bytes_io_getvalue) ) ;
441
- bytes_io
442
- } ;
443
- ctx. set_attr ( & py_mod, "BytesIO" , bytes_io) ;
444
-
445
- py_mod
394
+ let bytes_io = py_class ! ( ctx, "BytesIO" , buffered_io_base. clone( ) , {
395
+ "__init__" => ctx. new_rustfunc( bytes_io_init) ,
396
+ "getvalue" => ctx. new_rustfunc( bytes_io_getvalue)
397
+ } ) ;
398
+
399
+ py_module ! ( ctx, "io" , {
400
+ "open" => ctx. new_rustfunc( io_open) ,
401
+ "IOBase" => io_base. clone( ) ,
402
+ "RawIOBase" => raw_io_base. clone( ) ,
403
+ "BufferedIOBase" => buffered_io_base. clone( ) ,
404
+ "TextIOBase" => text_io_base. clone( ) ,
405
+ "FileIO" => file_io. clone( ) ,
406
+ "BufferedReader" => buffered_reader. clone( ) ,
407
+ "BufferedWriter" => buffered_writer. clone( ) ,
408
+ "TextIOWrapper" => text_io_wrapper. clone( ) ,
409
+ "StringIO" => string_io,
410
+ "BytesIO" => bytes_io,
411
+ } )
446
412
}
0 commit comments