Skip to content

Commit 294cee9

Browse files
committed
Allow csp.Struct as a generic field member to be autogened. Fixes crash on _metadata_info in this case
Signed-off-by: Rob Ambalu <robert.ambalu@point72.com>
1 parent 1802741 commit 294cee9

3 files changed

Lines changed: 17 additions & 1 deletion

File tree

cpp/csp/python/PyStruct.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ static PyObjectPtr PyStructMeta_typeinfo( const CspType * type )
272272
{
273273
auto const * structType = static_cast<const CspStructType*>( type );
274274
auto const * structMeta = static_cast<const DialectStructMeta*>( structType -> meta().get() );
275-
if( PyDict_SetItemString( out.get(), "pytype", ( PyObject * ) structMeta -> pyType() ) < 0 )
275+
PyObject * pyType = structMeta ? ( PyObject * ) structMeta -> pyType() : Py_None;
276+
if( PyDict_SetItemString( out.get(), "pytype", pyType ) < 0 )
276277
CSP_THROW( PythonPassthrough, "" );
277278
}
278279
else if( type -> type() == CspType::Type::ARRAY )

csp/build/csp_autogen.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020

2121
def struct_type(type_info):
22+
# If struct pytype is None then we are dealing with a generic csp.Struct field type
23+
if type_info["pytype"] is None:
24+
return "csp::StructPtr"
2225
return f"""csp::autogen::{type_info["pytype"].__name__}::Ptr"""
2326

2427

csp/tests/impl/test_struct.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4201,6 +4201,18 @@ class PipeTypesConfig(csp.Struct):
42014201
with self.assertRaises(ValidationError):
42024202
TypeAdapter(PipeTypesConfig).validate_python(case)
42034203

4204+
def test__metadata_info(self):
4205+
class MyStruct(DerivedMixed):
4206+
typed: BaseNative
4207+
generic: csp.Struct
4208+
4209+
metadata_info = MyStruct._metadata_info()
4210+
self.assertEqual(metadata_info["is_native"], False)
4211+
typed_field = [f for f in metadata_info["fields"] if f["fieldname"] == "typed"][0]
4212+
generic_field = [f for f in metadata_info["fields"] if f["fieldname"] == "generic"][0]
4213+
self.assertEqual(typed_field["type"]["pytype"], BaseNative)
4214+
self.assertEqual(generic_field["type"]["pytype"], None)
4215+
42044216

42054217
if __name__ == "__main__":
42064218
unittest.main()

0 commit comments

Comments
 (0)