Skip to content

Use py_class and py_module macros in socket #595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 19 additions & 29 deletions vm/src/stdlib/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,34 +420,24 @@ fn get_addr_tuple(vm: &mut VirtualMachine, addr: SocketAddr) -> PyResult {
}

pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
let py_mod = ctx.new_module(&"socket".to_string(), ctx.new_scope(None));

ctx.set_attr(&py_mod, "AF_INET", ctx.new_int(AddressFamily::Inet as i32));

ctx.set_attr(
&py_mod,
"SOCK_STREAM",
ctx.new_int(SocketKind::Stream as i32),
);

ctx.set_attr(&py_mod, "SOCK_DGRAM", ctx.new_int(SocketKind::Dgram as i32));

let socket = {
let socket = ctx.new_class("socket", ctx.object());
ctx.set_attr(&socket, "__new__", ctx.new_rustfunc(socket_new));
ctx.set_attr(&socket, "connect", ctx.new_rustfunc(socket_connect));
ctx.set_attr(&socket, "recv", ctx.new_rustfunc(socket_recv));
ctx.set_attr(&socket, "send", ctx.new_rustfunc(socket_send));
ctx.set_attr(&socket, "bind", ctx.new_rustfunc(socket_bind));
ctx.set_attr(&socket, "accept", ctx.new_rustfunc(socket_accept));
ctx.set_attr(&socket, "listen", ctx.new_rustfunc(socket_listen));
ctx.set_attr(&socket, "close", ctx.new_rustfunc(socket_close));
ctx.set_attr(&socket, "getsockname", ctx.new_rustfunc(socket_getsockname));
ctx.set_attr(&socket, "sendto", ctx.new_rustfunc(socket_sendto));
ctx.set_attr(&socket, "recvfrom", ctx.new_rustfunc(socket_recvfrom));
socket
};
ctx.set_attr(&py_mod, "socket", socket.clone());
let socket = py_class!(ctx, "socket", ctx.object(), {
"__new__" => ctx.new_rustfunc(socket_new),
"connect" => ctx.new_rustfunc(socket_connect),
"recv" => ctx.new_rustfunc(socket_recv),
"send" => ctx.new_rustfunc(socket_send),
"bind" => ctx.new_rustfunc(socket_bind),
"accept" => ctx.new_rustfunc(socket_accept),
"listen" => ctx.new_rustfunc(socket_listen),
"close" => ctx.new_rustfunc(socket_close),
"getsockname" => ctx.new_rustfunc(socket_getsockname),
"sendto" => ctx.new_rustfunc(socket_sendto),
"recvfrom" => ctx.new_rustfunc(socket_recvfrom),
});

py_mod
py_module!(ctx, "socket", {
"AF_INET" => ctx.new_int(AddressFamily::Inet as i32),
"SOCK_STREAM" => ctx.new_int(SocketKind::Stream as i32),
"SOCK_DGRAM" => ctx.new_int(SocketKind::Dgram as i32),
"socket" => socket.clone(),
})
}