Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

IDL inheritance causes runtime errors during Topic creation

未关闭
#226 0 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
55/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
python
领域
backend

调研方向

从 cyclonedds/idl/_xt_builder.py 中的 _xt_minimal_struct_header 和 _xt_complete_struct_header 开始,然后使用 issue 中所示的 foo.idl Parent/Child 示例和 Topic 创建来重现该失败。完成标准是:继承的 IDL 类型可以创建 Parent 和 Child Topics,并且能够写入和读取 Child 实例而不会出现 DDS 错误或断言。

由索引模型根据 Issue 内容生成。

描述

Hello!

I'm experiencing the following issue with the code generated from IDL definitions that use inheritance

foo.idl

struct Parent {
    long parent1;
};

struct Child : Parent {
    long child1;
};

Generate code from IDL via idlc:

"""
  Generated by Eclipse Cyclone DDS idlc Python Backend
  Cyclone DDS IDL version: v0.11.0
  Module: 
  IDL file: foo.idl

"""

from dataclasses import dataclass
from enum import auto
from typing import TYPE_CHECKING, Optional

import cyclonedds as dds

import cyclonedds.idl as idl
import cyclonedds.idl.annotations as annotate
import cyclonedds.idl.types as types


@dataclass
@annotate.final
@annotate.autoid("sequential")
class Parent(idl.IdlStruct, typename="Parent"):
    parent1: types.int32


@dataclass
@annotate.final
@annotate.autoid("sequential")
class Child(Parent, typename="Child"):
    child1: types.int32

Parent class instead of idl.IdlStruct in Python was fixed in https://github.com/eclipse-cyclonedds/cyclonedds-python/issues/162. The change also added some additional checks for base classes. However, trying to use the code (with main branch of the core library (d6684dc96e7ef092869688358753e49e33e9bbc2) fails, for example

"""
  Generated by Eclipse Cyclone DDS idlc Python Backend
  Cyclone DDS IDL version: v0.11.0
  Module: 
  IDL file: foo.idl

"""

from dataclasses import dataclass
from enum import auto
from typing import TYPE_CHECKING, Optional

import cyclonedds as dds

import cyclonedds.idl as idl
import cyclonedds.idl.annotations as annotate
import cyclonedds.idl.types as types


@dataclass
@annotate.final
@annotate.autoid("sequential")
class Parent(idl.IdlStruct, typename="Parent"):
    parent1: types.int32


@dataclass
@annotate.final
@annotate.autoid("sequential")
class Child(Parent, typename="Child"):
    child1: types.int32


d = dds.domain.DomainParticipant(42)
t = dds.topic.Topic(d, "test_topic", Child)
w = dds.pub.DataWriter(d, t)
r = dds.sub.DataReader(d, t)
w.write(Child(parent1=1, child1=2))
print(r.read())
$ python foo.py 
Traceback (most recent call last):
  File "<...>/cyclonedds-python/a/a.py", line 35, in <module>
    t = dds.topic.Topic(d, "test_topic", Child)
  File "<...>/cyclonedds-python/.venv/lib/python3.10/site-packages/cyclonedds/topic.py", line 59, in __init__
    super().__init__(
  File "<...>/cyclonedds-python/.venv/lib/python3.10/site-packages/cyclonedds/core.py", line 181, in __init__
    raise DDSException(
cyclonedds.core.DDSException: [DDS_RETCODE_BAD_PARAMETER] Bad parameter value. Occurred upon initialisation of a cyclonedds.topic.Topic

Interestingly, if you create a different topic with the Parent class, another error occurs, seemingly because some registration happens in the native DDS:

"""
  Generated by Eclipse Cyclone DDS idlc Python Backend
  Cyclone DDS IDL version: v0.11.0
  Module: 
  IDL file: foo.idl

"""

from dataclasses import dataclass
from enum import auto
from typing import TYPE_CHECKING, Optional

import cyclonedds as dds

import cyclonedds.idl as idl
import cyclonedds.idl.annotations as annotate
import cyclonedds.idl.types as types


@dataclass
@annotate.final
@annotate.autoid("sequential")
class Parent(idl.IdlStruct, typename="Parent"):
    parent1: types.int32


@dataclass
@annotate.final
@annotate.autoid("sequential")
class Child(Parent, typename="Child"):
    child1: types.int32


d = dds.domain.DomainParticipant(42)
t_parent = dds.topic.Topic(d, "test_parent", Parent)
t = dds.topic.Topic(d, "test_topic", Child)
w = dds.pub.DataWriter(d, t)
r = dds.sub.DataReader(d, t)
w.write(Child(parent1=1, child1=2))
print(r.read())
$ python foo.py 
python: <...>/cyclonedds/src/core/ddsi/src/ddsi_typewrap.c:3112: ddsi_xt_get_typeobject_kind_impl: Assertion `xt->kind == DDSI_TYPEID_KIND_COMPLETE' failed.
Aborted (core dumped)

I've tried digging around the code and I believe I might see the issue. My understanding of the XT type encoding is practically nonexistent, but it seems that the Child class has the wrong type information in the header section, since it points to the minimal version of the Parent class instead of the complete one. The following diff makes the script above working properly:

diff --git a/cyclonedds/idl/_xt_builder.py b/cyclonedds/idl/_xt_builder.py
index 37a5453..19010c1 100644
--- a/cyclonedds/idl/_xt_builder.py
+++ b/cyclonedds/idl/_xt_builder.py
@@ -905,7 +905,7 @@ class XTBuilder:
 
     @classmethod
     def _xt_minimal_struct_header(cls, entity: Type[IdlStruct]) -> xt.MinimalStructHeader:
-        if entity.__base__ is None or entity.__base__ == IdlStruct:
+        if entity.__base__ is None or issubclass(entity.__base__, IdlStruct):
             return xt.MinimalStructHeader(
                 base_type=xt.TypeIdentifier(discriminator=xt.TK_NONE, value=None),
                 detail=cls._xt_minimal_type_detail(entity)
@@ -918,7 +918,7 @@ class XTBuilder:
 
     @classmethod
     def _xt_complete_struct_header(cls, entity: Type[IdlStruct]) -> xt.CompleteStructHeader:
-        if entity.__base__ is None or entity.__base__ == IdlStruct:
+        if entity.__base__ is None or issubclass(entity.__base__, IdlStruct):
             return xt.CompleteStructHeader(
                 base_type=xt.TypeIdentifier(discriminator=xt.TK_NONE, value=None),
                 detail=cls._xt_complete_type_detail(entity)
$ python foo.py
[Child(parent1=1, child1=2)]

Hope this might help.

主要语言
Python
星标
110
派生
68
平均合并
3 天 5 小时
30 天内合并 PR
1

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

eclipse-cyclonedds/cyclonedds-python 的其他 Issue

查看 eclipse-cyclonedds/cyclonedds-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。