-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Stdlib] Add
PythonObject.__contains__
Signed-off-by: rd4com <[email protected]>
- Loading branch information
Showing
7 changed files
with
164 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
stdlib/test/python/module_for_test_python_object_dunder_contains.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
class Class_no_iterable_no_contains: | ||
x = 1 | ||
|
||
class Class_no_iterable_but_contains: | ||
x = 123 | ||
def __contains__(self, rhs): | ||
return rhs == self.x | ||
|
||
class Class_iterable_no_contains: | ||
def __init__(self): | ||
self.data = [123] | ||
|
||
def __iter__(self): | ||
self.i = 0 | ||
return self | ||
|
||
def __next__(self): | ||
if self.i >= len(self.data): | ||
raise StopIteration | ||
else: | ||
tmp = self.data[self.i] | ||
self.i += 1 | ||
return tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
stdlib/test/python/test_python_object_dunder_contains.mojo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# ===----------------------------------------------------------------------=== # | ||
# Copyright (c) 2024, Modular Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License v2.0 with LLVM Exceptions: | ||
# https://llvm.org/LICENSE.txt | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ===----------------------------------------------------------------------=== # | ||
# XFAIL: asan && !system-darwin | ||
# RUN: %mojo %s | ||
|
||
from python import Python, PythonObject | ||
from testing import assert_equal, assert_false, assert_raises, assert_true | ||
|
||
def test_contains_dunder(inout python: Python): | ||
with assert_raises(contains="'int' object is not iterable"): | ||
var z = PythonObject(0) | ||
_ = 5 in z | ||
|
||
var x = PythonObject([1.1, 2.2]) | ||
assert_true(1.1 in x) | ||
assert_false(3.3 in x) | ||
|
||
x = PythonObject(["Hello", "World"]) | ||
assert_true("World" in x) | ||
|
||
x = PythonObject((1.5, 2)) | ||
assert_true(1.5 in x) | ||
assert_false(3.5 in x) | ||
|
||
var y = Dict[PythonObject, PythonObject]() | ||
y["A"] = "A" | ||
y["B"] = 5 | ||
x = PythonObject(y) | ||
assert_true("A" in x) | ||
assert_false("C" in x) | ||
assert_true("B" in x) | ||
|
||
#tests with python modules: | ||
module = python.import_module( | ||
"module_for_test_python_object_dunder_contains" | ||
) | ||
|
||
x = module.Class_no_iterable_but_contains() | ||
assert_true(123 in x) | ||
|
||
x = module.Class_no_iterable_no_contains() | ||
with assert_raises( | ||
contains = "'Class_no_iterable_no_contains' object is not iterable" | ||
): | ||
_ = 123 in x | ||
|
||
x = module.Class_iterable_no_contains() | ||
assert_true(123 in x) | ||
assert_false(234 in x) | ||
x.data.append(234) | ||
assert_true(234 in x) | ||
|
||
|
||
def main(): | ||
# initializing Python instance calls init_python | ||
var python = Python() | ||
test_contains_dunder(python) |
39 changes: 39 additions & 0 deletions
39
stdlib/test/python/test_testing_evironement_is_clean_A.mojo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# ===----------------------------------------------------------------------=== # | ||
# Copyright (c) 2024, Modular Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License v2.0 with LLVM Exceptions: | ||
# https://llvm.org/LICENSE.txt | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ===----------------------------------------------------------------------=== # | ||
# XFAIL: asan && !system-darwin | ||
# RUN: %mojo %s | ||
|
||
from python import Python, PythonObject | ||
from testing import assert_equal, assert_false, assert_raises, assert_true | ||
|
||
# Goal: assert that there are no pollutions between tests. | ||
# Invariant: test A is performed before test B | ||
# The tests are in pairs: | ||
# - test_testing_environement_is_clean_A | ||
# (modify the environement) | ||
# - test_testing_environement_is_clean_B | ||
# (check that modifications are not reflected there) | ||
|
||
def test_populate_environement_a(inout python: Python): | ||
python.eval("x = 123") | ||
assert_equal(int(python.evaluate("x")), 123) | ||
|
||
_ = python.import_module("my_module") | ||
modules = python.import_module("sys").modules.keys() | ||
assert_true( | ||
modules.__getattr__("__contains__")("my_module").__bool__() | ||
) | ||
|
||
def main(): | ||
var python = Python() | ||
test_populate_environement_a(python) |
33 changes: 33 additions & 0 deletions
33
stdlib/test/python/test_testing_evironement_is_clean_B.mojo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# ===----------------------------------------------------------------------=== # | ||
# Copyright (c) 2024, Modular Inc. All rights reserved. | ||
# | ||
# Licensed under the Apache License v2.0 with LLVM Exceptions: | ||
# https://llvm.org/LICENSE.txt | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# ===----------------------------------------------------------------------=== # | ||
# XFAIL: asan && !system-darwin | ||
# RUN: %mojo %s | ||
|
||
from python import Python, PythonObject | ||
from testing import assert_equal, assert_false, assert_raises, assert_true | ||
|
||
# Goal: assert that there are no pollutions between tests. | ||
# see test_testing_environement_is_clean_A for description. | ||
|
||
def test_populate_environement_b(inout python: Python): | ||
with assert_raises(contains = "name 'x' is not defined"): | ||
_ = python.evaluate("x") | ||
|
||
modules = python.import_module("sys").modules.keys() | ||
assert_false( | ||
modules.__getattr__("__contains__")("my_module").__bool__() | ||
) | ||
|
||
def main(): | ||
var python = Python() | ||
test_populate_environement_b(python) |