Merge pull request #1665 from youknowone/small-changes

Small changes about parser, int test, new_bool and socket.has_ipv6
This commit is contained in:
Jeong YunWon
2020-01-08 17:57:39 +09:00
committed by GitHub
4 changed files with 34 additions and 0 deletions

View File

@@ -393,6 +393,31 @@ mod tests {
)
}
#[test]
fn test_parse_dict_comprehension() {
let source = String::from("{x1: x2 for y in z}");
let parse_ast = parse_expression(&source).unwrap();
assert_eq!(
parse_ast,
ast::Expression {
location: ast::Location::new(1, 1),
node: ast::ExpressionType::Comprehension {
kind: Box::new(ast::ComprehensionKind::Dict {
key: mk_ident("x1", 1, 2),
value: mk_ident("x2", 1, 6),
}),
generators: vec![ast::Comprehension {
location: ast::Location::new(1, 9),
target: mk_ident("y", 1, 13),
iter: mk_ident("z", 1, 18),
ifs: vec![],
is_async: false,
}],
}
}
);
}
#[test]
fn test_parse_list_comprehension() {
let source = String::from("[x for y in z]");

View File

@@ -273,6 +273,13 @@ class F(float):
assert int(F(1.2)) == 3
class BadInt(int):
def __int__(self):
return 42.0
with assert_raises(TypeError):
int(BadInt())
assert isinstance((0).__round__(), int)
assert isinstance((1).__round__(), int)
assert (0).__round__() == 0

View File

@@ -419,6 +419,7 @@ impl PyContext {
)
}
#[inline]
pub fn new_bool(&self, b: bool) -> PyObjectRef {
let value = if b {
&self.true_value

View File

@@ -643,6 +643,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
"htons" => ctx.new_rustfunc(socket_hton::<u16>),
"ntohl" => ctx.new_rustfunc(socket_ntoh::<u32>),
"ntohs" => ctx.new_rustfunc(socket_ntoh::<u16>),
"has_ipv6" => ctx.new_bool(false),
"getdefaulttimeout" => ctx.new_rustfunc(|vm: &VirtualMachine| vm.get_none()),
"getaddrinfo" => ctx.new_rustfunc(socket_getaddrinfo),
"gethostbyaddr" => ctx.new_rustfunc(socket_gethostbyaddr),