Skip to content

Commit 9a81e44

Browse files
committed
Improve handling anonymous fields of wrong type
1 parent 32ad73b commit 9a81e44

2 files changed

Lines changed: 21 additions & 12 deletions

File tree

src/core/IronPython.Modules/_ctypes/StructType.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,14 @@ private void SetFields(object? fields) {
262262
INativeType? lastType = null;
263263
List<Field> allFields = GetBaseSizeAlignmentAndFields(out int size, out int alignment);
264264

265-
IList<object>? anonFields = GetAnonymousFields(this);
265+
IList<string>? anonFields = GetAnonymousFields(this);
266266

267267
foreach (object fieldDef in fieldDefList) {
268268
GetFieldInfo(this, fieldDef, out string fieldName, out INativeType cdata, out bitCount);
269269

270-
int prevSize = UpdateSizeAndAlignment(cdata, bitCount, ref lastType, ref size, ref alignment, ref curBitCount);
270+
int fieldOffset = UpdateSizeAndAlignment(cdata, bitCount, ref lastType, ref size, ref alignment, ref curBitCount);
271271

272-
var newField = new Field(fieldName, cdata, prevSize, allFields.Count, bitCount, curBitCount - bitCount);
272+
var newField = new Field(fieldName, cdata, fieldOffset, allFields.Count, bitCount, curBitCount - bitCount);
273273
allFields.Add(newField);
274274
AddSlot(fieldName, newField);
275275

@@ -292,7 +292,7 @@ private void SetFields(object? fields) {
292292
}
293293
}
294294

295-
internal static void CheckAnonymousFields(List<Field> allFields, IList<object>? anonFields) {
295+
internal static void CheckAnonymousFields(List<Field> allFields, IList<string>? anonFields) {
296296
if (anonFields != null) {
297297
foreach (string s in anonFields) {
298298
bool found = false;
@@ -310,18 +310,26 @@ internal static void CheckAnonymousFields(List<Field> allFields, IList<object>?
310310
}
311311
}
312312

313-
internal static IList<object>? GetAnonymousFields(PythonType type) {
314-
object anonymous;
315-
IList<object>? anonFields = null;
316-
if (type.TryGetBoundAttr(type.Context.SharedContext, type, "_anonymous_", out anonymous)) {
317-
anonFields = anonymous as IList<object>;
318-
if (anonFields == null) {
313+
314+
internal static IList<string>? GetAnonymousFields(PythonType type) {
315+
IList<string>? anonFieldNames = null;
316+
if (type.TryGetBoundAttr(type.Context.SharedContext, type, "_anonymous_", out object anonymous)) {
317+
if (anonymous is not IList<object> anonFields) {
319318
throw PythonOps.TypeError("_anonymous_ must be a sequence");
320319
}
320+
anonFieldNames = [];
321+
foreach (object anonField in anonFields) {
322+
if (Converter.TryConvertToString(anonField, out string? anonFieldStr)) {
323+
anonFieldNames.Add(anonFieldStr);
324+
} else {
325+
throw PythonOps.TypeErrorForBadInstance("anonymous field must be a string, not '{0}'", anonField);
326+
}
327+
}
321328
}
322-
return anonFields;
329+
return anonFieldNames;
323330
}
324331

332+
325333
internal static void AddAnonymousFields(PythonType type, List<Field> allFields, INativeType cdata, Field newField) {
326334
Field[] childFields;
327335
if (cdata is StructType st) {
@@ -347,6 +355,7 @@ internal static void AddAnonymousFields(PythonType type, List<Field> allFields,
347355
}
348356
}
349357

358+
350359
private List<Field> GetBaseSizeAlignmentAndFields(out int size, out int alignment) {
351360
size = 0;
352361
alignment = 1;

src/core/IronPython.Modules/_ctypes/UnionType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ string INativeType.TypeFormat {
166166
private void SetFields(object? fields) {
167167
lock (this) {
168168
IList<object> fieldDefList = GetFieldsList(fields);
169-
IList<object>? anonFields = StructType.GetAnonymousFields(this);
169+
IList<string>? anonFields = StructType.GetAnonymousFields(this);
170170

171171
int size = 0, alignment = 1;
172172
List<Field> allFields = new List<Field>();//GetBaseSizeAlignmentAndFields(out size, out alignment);

0 commit comments

Comments
 (0)