Bug 126487 - id of objects changes on every access
Summary: id of objects changes on every access
Status: RESOLVED NOTABUG
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: sdk (show other bugs)
Version:
(earliest affected)
6.2.5.2 release
Hardware: All All
: medium minor
Assignee: Not Assigned
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-07-20 13:33 UTC by Konstantin Kharlamov
Modified: 2026-01-28 17:16 UTC (History)
3 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Konstantin Kharlamov 2019-07-20 13:33:58 UTC
When moving across document hierarchy, sometimes it's needed to determine whether 2 objects are the same thing *(not in terms of properties/values, but as in, really refer to the same thing)*. For example, when you have some cell1 and cell2, there's no need to waste energy comparing all properties with cell1 == cell2, when one can just do id(cell1) == id(cell2) (btw, the == operator doesn't work either).

But for some reason for every access to any UNO object the object id changes. It doesn't sound right even in terms of performance (like, why to create every time a different instance?).

# Steps to reproduce

1. Run python shell
2. In python shell execute the following commands

    >>> import uno
    >>> localContext = uno.getComponentContext()
    >>> list_of_same_objects = [localContext.ServiceManager for i in range(0,10)]
    >>> [id(obj) for obj in list_of_same_objects]
    [139769180615904, 139769180615928, 139769180615952, 139769180615976, 139769180616000, 139769180616024, 139769180616048, 139769180616072, 139769180616096, 139769180616120]

## Expected

The last command should've returned list of the same numbers.

## Actual

All numbers in the last command are different

# Additional information

See also: https://ask.libreoffice.org/en/question/201705/uno-determine-if-2-objects-refer-to-the-same-thing/
Comment 1 businessstepbystep 2019-07-21 10:12:28 UTC Comment hidden (spam)
Comment 2 Noel Grandin 2019-07-22 08:29:42 UTC
Those are different objects (in the python sense) which is why they return different id() values. 
There is no way to fix that, id() cannot be overridden.

However, it might be possible to make == work, by checking the underlying remote object identity, if that is exposed by the UNO bridge, I'm not sure.


Code pointers for anyone who is interested on working on this:

That would require changes to the code in /pyuno

I suspect we need the equivalent of m_oid from
   bridges/source/jni_uno/java/com/sun/star/bridges/jni_uno/JNI_proxy.java
implemented in the pyuno bridge.
Comment 3 Buovjaga 2020-04-17 15:34:47 UTC
NEW per comment 2
Comment 4 smdcitations 2020-07-20 04:59:35 UTC Comment hidden (spam)
Comment 5 QA Administrators 2022-07-21 03:33:44 UTC Comment hidden (obsolete)
Comment 6 Konstantin Kharlamov 2022-07-21 04:10:08 UTC
(In reply to QA Administrators from comment #5)
> Test to see if the bug is still present with the latest version of
> LibreOffice from https://www.libreoffice.org/download/

Still reproducible with LibreOffice 7.3.4.2 30(Build:2)
Comment 7 QA Administrators 2024-07-21 03:14:18 UTC Comment hidden (obsolete)
Comment 8 Konstantin Kharlamov 2024-09-09 03:50:59 UTC
Still reproducible with LibreOffice 24.8.0.3 480(Build:3)
Comment 9 Neil Roberts 2026-01-28 17:16:24 UTC
It looks like the == operator as suggested in comment #2 already works. For example:

import uno

localContext = uno.getComponentContext()

a = localContext.ServiceManager
b = localContext.ServiceManager

print(f"id(a) -> {id(a)}")
print(f"id(b) -> {id(b)}")

print(f"a == b -> {a == b}")

This prints:

id(a) -> 140688621675472
id(b) -> 140688615666448
a == b -> True

This is implemented in PyUNO_cmp in pyuno.cxx.

I also tested it in LibreOffice 6.2.0 and with a remote connection and it all works. Maybe it doesn’t work in the specific case that Konstantin is talking about because some API methods create new UNO objects to wrap internal objects every time they are called. This seems to be a common source of confusion (see #137664 and probably others).

I guess it would be theoretically possible to make id(a)==id(b) if we had some sort of lookup table for incoming objects so we could map them to same Python object if we see the same object again. However this would add overhead and probably isn’t worth it because being able to check if two objects are the same is probably enough in most use cases.

I’m closing the bug as NOTABUG because it doesn’t seem like a general pyuno problem but a problem specific to the API that is returning the cell objects. Feel free to open another bug if you think that API should be returning the same UNO object, or maybe provide some other way to check if they refer to the same cell.