Skip to content

Commit 90739ab

Browse files
authored
Fix invalid access to a struct meta name when the struct is a base struct (#119)
Signed-off-by: Adam Glustein <Adam.Glustein@Point72.com>
1 parent 2aef70b commit 90739ab

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

cpp/csp/python/Conversions.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,13 @@ inline StructPtr fromPython( PyObject * o, const CspType & type )
422422
( static_cast<const CspStructType &>( type ).meta() && //could be csp.Struct as a type annotation which is allowed
423423
!StructMeta::isDerivedType( static_cast<PyStruct *>( o ) -> structMeta(),
424424
static_cast<const CspStructType &>( type ).meta().get() ) ) )
425-
CSP_THROW( TypeError, "Invalid struct type, expected struct " << static_cast<const CspStructType &>( type ).meta() -> name() << " got " << Py_TYPE( o ) -> tp_name );
425+
{
426+
std::string name;
427+
auto meta = static_cast<const CspStructType &>( type ).meta();
428+
if( meta )
429+
name = " " + meta -> name();
430+
CSP_THROW( TypeError, "Invalid struct type, expected struct" << name << " got " << Py_TYPE( o ) -> tp_name );
431+
}
426432

427433
return static_cast<PyStruct *>( o ) -> struct_;
428434
}

csp/tests/impl/test_struct.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,20 @@ class Outer(csp.Struct):
10781078
repr(all)
10791079
all = all[:100]
10801080

1081+
def test_python_conversion_on_nested_base_struct(self):
1082+
''' Was a BUG due to the error message in fromPython trying to access the meta name of a base struct class'''
1083+
class A(csp.Struct):
1084+
a: csp.Struct
1085+
1086+
# 1) in constructor
1087+
with self.assertRaises(TypeError) as e:
1088+
my_a = A(a=None)
1089+
1090+
# 2) setting the member
1091+
with self.assertRaises(TypeError) as e:
1092+
my_a = A()
1093+
my_a.a = None
1094+
10811095

10821096
if __name__ == "__main__":
10831097
unittest.main()

0 commit comments

Comments
 (0)