| 461 | |
| 462 | @pytest.mark.parametrize("xlang", [True, False]) |
| 463 | def test_ref_tracking(xlang): |
| 464 | fory = Fory(xlang=xlang, ref=True, compatible=xlang) |
| 465 | |
| 466 | # Circular reference test - only works for Python native mode. |
| 467 | # Xlang mode doesn't support true circular references during deserialization |
| 468 | # because the object must be registered after it's fully constructed |
| 469 | if not xlang: |
| 470 | simple_list = [] |
| 471 | simple_list.append(simple_list) |
| 472 | new_simple_list = ser_de(fory, simple_list) |
| 473 | assert new_simple_list[0] is new_simple_list |
| 474 | |
| 475 | now = datetime.datetime.now(datetime.timezone.utc) |
| 476 | day = datetime.date(2021, 11, 23) |
| 477 | list_ = ["a", 1, -1.0, True, now, day] |
| 478 | dict1 = {f"k{i}": v for i, v in enumerate(list_)} |
| 479 | dict2 = {v: v for v in list_} |
| 480 | dict3 = { |
| 481 | "list1_0": list_, |
| 482 | "list1_1": list_, |
| 483 | "dict1_0": dict1, |
| 484 | "dict1_1": dict1, |
| 485 | "dict2_0": dict2, |
| 486 | "dict2_1": dict2, |
| 487 | } |
| 488 | # Circular reference in dict3 - only works for Python native mode |
| 489 | if not xlang: |
| 490 | dict3["dict3_0"] = dict3 |
| 491 | dict3["dict3_1"] = dict3 |
| 492 | new_dict3 = ser_de(fory, dict3) |
| 493 | assert new_dict3["list1_0"] == list_ |
| 494 | assert new_dict3["list1_0"] is new_dict3["list1_1"] |
| 495 | assert new_dict3["dict1_0"] == dict1 |
| 496 | assert new_dict3["dict1_0"] is new_dict3["dict1_1"] |
| 497 | assert new_dict3["dict2_0"] == dict2 |
| 498 | assert new_dict3["dict2_0"] is new_dict3["dict2_1"] |
| 499 | if not xlang: |
| 500 | assert new_dict3["dict3_0"] is new_dict3 |
| 501 | assert new_dict3["dict3_0"] is new_dict3["dict3_0"] |
| 502 | |
| 503 | |
| 504 | @pytest.mark.parametrize("xlang", [False, True]) |