Skip to content

Commit 2bc184f

Browse files
committed
Introduce py_class macro to define classes.
1 parent 4f944d3 commit 2bc184f

File tree

3 files changed

+68
-90
lines changed

3 files changed

+68
-90
lines changed

vm/src/macros.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,16 @@ macro_rules! py_module {
124124
}
125125
}
126126
}
127+
128+
#[macro_export]
129+
macro_rules! py_class {
130+
( $ctx:expr, $class_name:expr, $class_base:expr, { $($name:expr => $value:expr),* $(,)* }) => {
131+
{
132+
let py_class = $ctx.new_class($class_name, $class_base);
133+
$(
134+
$ctx.set_attr(&py_class, $name, $value);
135+
)*
136+
py_class
137+
}
138+
}
139+
}

vm/src/stdlib/io.rs

Lines changed: 47 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -346,101 +346,67 @@ pub fn io_open(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
346346
}
347347

348348
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-
353349
//IOBase the abstract base class of the IO Module
354350
let io_base = ctx.new_class("IOBase", ctx.object());
355-
ctx.set_attr(&py_mod, "IOBase", io_base.clone());
356351

357352
// IOBase Subclasses
358353
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+
});
371358

372359
//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+
});
379363

380364
// 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+
});
391372

392373
// 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+
});
414381

415382
//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+
});
426386

427387
//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+
436393
//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+
})
446412
}

vm/src/stdlib/re.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,12 @@ fn re_search(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
5050
}
5151

5252
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
53-
let py_mod = ctx.new_module("re", ctx.new_scope(None));
54-
55-
let match_type = ctx.new_class("Match", ctx.object());
56-
ctx.set_attr(&py_mod, "Match", match_type);
57-
58-
ctx.set_attr(&py_mod, "match", ctx.new_rustfunc(re_match));
59-
ctx.set_attr(&py_mod, "search", ctx.new_rustfunc(re_search));
60-
61-
py_mod
53+
let match_type = py_class!(ctx, "Match", ctx.object(), {
54+
});
55+
56+
py_module!(ctx, "re", {
57+
"Match" => match_type,
58+
"match" => ctx.new_rustfunc(re_match),
59+
"search" => ctx.new_rustfunc(re_search)
60+
})
6261
}

0 commit comments

Comments
 (0)