Skip to content

Commit 38e0e0e

Browse files
Moultclaude
andcommitted
Compare entity instances in one file by identity again
entity_instance.file is a property backed by a fresh SWIG wrapper on every access, and ifcopenshell::file had no __eq__, so `self.file != other.file` in entity_instance.__eq__ compared two throwaway wrappers and was always true - even for an instance against itself. Every entity comparison therefore took the deep get_info() branch, making distinct but structurally identical instances compare equal and leaving the final `return False` unreachable. Bonsai's TestAddRepresentationItemToShapeAspect showed this as two separate IfcShapeAspects being treated as one, so the stale aspect was never removed. Restore the file_pointer() pair that was commented out on both ifcopenshell::file and express::Base - IfcParseWrapper.i already described it as the way to "trace file ownership of instances on the python side" - and give file the __eq__/__hash__ it was missing. The express::Base one needs $self->file() now that file_ lives on instance_data. This also repairs rocksdb_lazy_instance.__eq__, which already called file_pointer(). EXPRESS `=` is value comparison and `:=:` is instance comparison, but rule_compiler emits `==` for both (see the @todo on process_rel_op), and derived attributes build their operands in the shared global file, so rules compare same-file instances and need value semantics. Restore those for the duration of rule execution with settings.compare_instances_by_value, alongside the existing unpack_non_aggregate_inverses. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent d1027c5 commit 38e0e0e

6 files changed

Lines changed: 69 additions & 7 deletions

File tree

‎src/ifcopenshell-python/ifcopenshell/entity_instance.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,11 @@ def __eq__(self, other: object) -> bool:
218218
if self.is_a(True) != other.is_a(True):
219219
return False
220220

221-
if self.file != other.file or not self.is_entity():
221+
if (
222+
settings.compare_instances_by_value
223+
or self.file_pointer() != other.file_pointer()
224+
or not self.is_entity()
225+
):
222226
return self.get_info(recursive=True, include_identifier=False) == other.get_info(
223227
recursive=True, include_identifier=False
224228
)

‎src/ifcopenshell-python/ifcopenshell/express/rule_executor.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ def run(f: ifcopenshell.file, logger: Logger) -> None:
9090
orig = ifcopenshell.settings.unpack_non_aggregate_inverses
9191
ifcopenshell.settings.unpack_non_aggregate_inverses = True
9292

93+
# Rules are transpiled with EXPRESS `=` emitted as Python `==`, so `==` has
94+
# to mean value comparison for the duration. See the @todo on
95+
# rule_compiler.process_rel_op.
96+
orig_compare = ifcopenshell.settings.compare_instances_by_value
97+
ifcopenshell.settings.compare_instances_by_value = True
98+
9399
fn = os.path.join(os.path.dirname(__file__), "rules", f"{f.schema_identifier}.py")
94100
try:
95101
source = open(fn, "r").read()
@@ -272,6 +278,7 @@ def check(value, type, instance):
272278
)
273279

274280
ifcopenshell.settings.unpack_non_aggregate_inverses = orig
281+
ifcopenshell.settings.compare_instances_by_value = orig_compare
275282

276283

277284
if __name__ == "__main__":

‎src/ifcopenshell-python/ifcopenshell/ifcopenshell_wrapper.pyi‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,7 @@ class entity_instance(entity_instance_mixin):
750750
def declaration(self) -> declaration: ...
751751
@property
752752
def file(self) -> ifcopenshell.file: ...
753+
def file_pointer(self) -> int: ...
753754
def get_argument(self, *args: int | str) -> Any: ...
754755
def get_argument_index(self, a: str) -> int: ...
755756
def attribute_name(self, i: int) -> str: ...
@@ -836,6 +837,7 @@ class face:
836837
def print_impl(self, o, indent): ...
837838

838839
class file(file_mixin):
840+
def file_pointer(self) -> int: ...
839841
def fresh_id(self) -> int: ...
840842
def __init__(
841843
self,

‎src/ifcopenshell-python/ifcopenshell/settings.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,24 @@
3434
"""
3535

3636
unpack_non_aggregate_inverses = False
37+
38+
"""
39+
When true compare entity instances by value rather than by identity, even
40+
when they belong to the same file. EXPRESS uses `=` for value comparison and
41+
`:=:` for instance comparison, whereas the Python API only has `==`, which
42+
normally means "the same instance". Example:
43+
44+
>>> import ifcopenshell
45+
>>> f = ifcopenshell.file(schema='ifc2x3')
46+
>>> f.createIfcCartesianPoint((0., 0.))
47+
#1=IfcCartesianPoint((0.,0.))
48+
>>> f.createIfcCartesianPoint((0., 0.))
49+
#2=IfcCartesianPoint((0.,0.))
50+
>>> f[1] == f[2]
51+
False
52+
>>> ifcopenshell.settings.compare_instances_by_value = True
53+
>>> f[1] == f[2]
54+
True
55+
"""
56+
57+
compare_instances_by_value = False

‎src/ifcopenshell-python/test/test_entity_instance.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ def test_equality():
7575
assert f[1] == g[1]
7676
g[1].Coordinates = (1.0, 0.0)
7777
assert f[1] != g[1]
78+
f.createIfcCartesianPoint((0.0, 0.0))
79+
assert f[1] == f[1]
80+
assert f[1] != f[2]
81+
82+
83+
def test_equality_of_owning_file():
84+
f = ifcopenshell.file()
85+
g = ifcopenshell.file()
86+
f.createIfcCartesianPoint((0.0, 0.0))
87+
f.createIfcCartesianPoint((0.0, 0.0))
88+
g.createIfcCartesianPoint((0.0, 0.0))
89+
assert f[1].file == f[1].file
90+
assert f[1].file == f[2].file
91+
assert f[1].file != g[1].file
7892

7993

8094
def test_setting_logical():

‎src/ifcwrap/IfcParseWrapper.i‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,29 @@ private:
263263
%newobject ifcopenshell::file::create_uninitialized;
264264

265265
%extend ifcopenshell::file {
266-
/*
267266
// Use to correlate to entity_instance.file_pointer, so that we
268267
// can trace file ownership of instances on the python side.
269268
size_t file_pointer() const {
270269
return reinterpret_cast<size_t>($self);
271270
}
272-
*/
271+
272+
%pythoncode %{
273+
def __eq__(self, other):
274+
# Attribute access hands out a new wrapper every time, so identity
275+
# of the wrapper says nothing about the file it refers to.
276+
if self is other:
277+
return True
278+
if not isinstance(other, file):
279+
return NotImplemented
280+
return self.file_pointer() == other.file_pointer()
281+
282+
def __ne__(self, other):
283+
result = self.__eq__(other)
284+
return result if result is NotImplemented else not result
285+
286+
def __hash__(self):
287+
return self.file_pointer()
288+
%}
273289

274290
file(const std::string& schema) {
275291
return new ifcopenshell::file(ifcopenshell::schema_by_name(schema));
@@ -552,12 +568,10 @@ private:
552568
return oss.str();
553569
}
554570

555-
/*
556-
// Just something to have a somewhat sensible value to hash
571+
// Identifies the file owning this instance, zero when it has no owner.
557572
size_t file_pointer() const {
558-
return reinterpret_cast<size_t>($self->file_);
573+
return reinterpret_cast<size_t>($self->file());
559574
}
560-
*/
561575

562576
unsigned get_argument_index(const std::string& a) const {
563577
if ($self->declaration().as_entity()) {

0 commit comments

Comments
 (0)