Skip to content

Commit

Permalink
Fixes for several tests in test_bool (#261)
Browse files Browse the repository at this point in the history
* Fixes for several tests in test_bool

* Merge in from IronLanguages/main#1332
  • Loading branch information
slide committed Mar 2, 2017
1 parent a20de8f commit a790849
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 11 deletions.
16 changes: 15 additions & 1 deletion Src/IronPython/Runtime/Binding/PythonProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,21 @@ static partial class PythonProtocol {
);
}

return Ast.NotEqual(callAsInt, AstUtils.Constant(0));
var res = Expression.Parameter(typeof(int));
return Ast.Block(
new[] { res },
Ast.Assign(res, callAsInt),
Ast.IfThen(Ast.LessThan(res, Ast.Constant(0)),
Ast.Throw(
Ast.Call(
typeof(PythonOps).GetMethod("ValueError"),
Ast.Constant("__len__() should return >= 0"),
Ast.NewArrayInit(typeof(object))
)
)
),
Ast.NotEqual(res, Ast.Constant(0))
);
}

#endregion
Expand Down
4 changes: 2 additions & 2 deletions Src/IronPython/Runtime/Operations/MarshalOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ private class MarshalWriter {
infinite.Add (o);
try {
if (o == null) _bytes.Add ((byte)'N');
else if (o == ScriptingRuntimeHelpers.True) _bytes.Add ((byte)'T');
else if (o == ScriptingRuntimeHelpers.False) _bytes.Add ((byte)'F');
else if (o == ScriptingRuntimeHelpers.True || (o is bool && (bool)o)) _bytes.Add ((byte)'T');
else if (o == ScriptingRuntimeHelpers.False || (o is bool && (!(bool)o))) _bytes.Add ((byte)'F');
else if (o is string) WriteString (o as string);
else if (o is int) WriteInt ((int)o);
else if (o is float) WriteFloat ((float)o);
Expand Down
23 changes: 15 additions & 8 deletions Src/IronPython/Runtime/Operations/PythonOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -470,19 +470,26 @@ public static partial class PythonOps {
}

public static object Is(object x, object y) {
return x == y ? ScriptingRuntimeHelpers.True : ScriptingRuntimeHelpers.False;
return IsRetBool(x, y) ? ScriptingRuntimeHelpers.True : ScriptingRuntimeHelpers.False;
}

public static bool IsRetBool(object x, object y) {
return x == y;
}
if (x == y)
return true;

public static object IsNot(object x, object y) {
return x != y ? ScriptingRuntimeHelpers.True : ScriptingRuntimeHelpers.False;
// Special case "is True"/"is False" checks. They are somewhat common in
// Python (particularly in tests), but non-Python code may not stick to the
// convention of only using the two singleton instances at ScriptingRuntimeHelpers.
// (https://github.com/IronLanguages/main/issues/1299)
var xb = x as bool?;
if (xb.HasValue)
return xb == (y as bool?);

return false;
}

public static bool IsNotRetBool(object x, object y) {
return x != y;
public static object IsNot(object x, object y) {
return IsRetBool(x, y) ? ScriptingRuntimeHelpers.False : ScriptingRuntimeHelpers.True;
}

internal delegate T MultiplySequenceWorker<T>(T self, int count);
Expand Down Expand Up @@ -839,7 +846,7 @@ public static partial class PythonOps {
}

if (res < 0) {
throw PythonOps.ValueError("__len__ should return >= 0, got {0}", res);
throw PythonOps.ValueError("__len__() should return >= 0");
}
return res;
}
Expand Down
46 changes: 46 additions & 0 deletions Tests/test_is.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#####################################################################################
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# This source code is subject to terms and conditions of the Apache License, Version 2.0. A
# copy of the license can be found in the License.html file at the root of this distribution. If
# you cannot locate the Apache License, Version 2.0, please send an email to
# ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
# by the terms of the Apache License, Version 2.0.
#
# You must not remove this notice, or any other, from this software.
#
#
#####################################################################################

from iptest.assert_util import *

from System import Nullable

def test_object():
a = object()
b = object()

AssertEqual(True, a is a)
AssertEqual(False, a is b)
AssertEqual(False, a is not a)
AssertEqual(True, a is not b)

def test_bool_nullablebool():
tc = [
# (a, b, a is b)
(True, True, True),
(True, False, False),
(Nullable[bool](True), True, True), # https://github.com/IronLanguages/main/issues/1299
(Nullable[bool](True), False, False),
(Nullable[bool](False), True, False), # dito
(Nullable[bool](False), False, True),
(None, True, False),
(None, False, False),
]

for a, b, result in tc:
AssertEqual(result, a is b)
AssertEqual(result, b is a)
AssertEqual(not result, a is not b)
AssertEqual(not result, b is not a)

0 comments on commit a790849

Please sign in to comment.