Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions tests/snippets/tuple.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ def __eq__(self, x):
a = (1, 2, 3)
a += 1,
assert a == (1, 2, 3, 1)

assert () is ()

a = ()
b = ()
assert a is b
11 changes: 10 additions & 1 deletion vm/src/pyobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ pub struct PyContext {
pub none: PyNoneRef,
pub ellipsis: PyEllipsisRef,
pub not_implemented: PyNotImplementedRef,
pub empty_tuple: PyTupleRef,
pub tuple_type: PyClassRef,
pub tupleiterator_type: PyClassRef,
pub set_type: PyClassRef,
Expand Down Expand Up @@ -324,6 +325,9 @@ impl PyContext {

let true_value = create_object(PyInt::new(BigInt::one()), &bool_type);
let false_value = create_object(PyInt::new(BigInt::zero()), &bool_type);

let empty_tuple = create_object(PyTuple::from(vec![]), &tuple_type);

let context = PyContext {
bool_type,
memoryview_type,
Expand Down Expand Up @@ -382,6 +386,7 @@ impl PyContext {
weakproxy_type,
type_type,
exceptions,
empty_tuple,
};
objtype::init(&context);
objlist::init(&context);
Expand Down Expand Up @@ -645,7 +650,11 @@ impl PyContext {
}

pub fn new_tuple(&self, elements: Vec<PyObjectRef>) -> PyObjectRef {
PyObject::new(PyTuple::from(elements), self.tuple_type(), None)
if elements.is_empty() {
self.empty_tuple.clone().into_object()
} else {
PyObject::new(PyTuple::from(elements), self.tuple_type(), None)
}
}

pub fn new_list(&self, elements: Vec<PyObjectRef>) -> PyObjectRef {
Expand Down