Skip to content

Commit f07b680

Browse files
committed
Improve error messages for incorrect bitfields
1 parent 506916b commit f07b680

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -597,21 +597,17 @@ private static void GetFieldInfo(INativeType type, object o, out string fieldNam
597597
if (pt.Count != 3) {
598598
bitCount = null;
599599
} else {
600-
bitCount = CheckBits(cdata, pt);
600+
bitCount = Converter.ConvertToInt32(pt[2]);
601+
CheckBits(fieldName, cdata, bitCount.Value);
601602
}
602603
}
603604

604605
/// <summary>
605606
/// Verifies that the provided bit field settings are valid for this type.
606607
/// </summary>
607-
private static int CheckBits(INativeType cdata, PythonTuple pt) {
608-
int bitCount = Converter.ConvertToInt32(pt[2]);
609-
610-
if (!(cdata is SimpleType simpType)) {
611-
throw PythonOps.TypeError("bit fields not allowed for type {0}", ((PythonType)cdata).Name);
612-
}
613-
614-
switch (simpType._type) {
608+
private static void CheckBits(string fieldName, INativeType cdata, int bitCount) {
609+
switch ((cdata as SimpleType)?._type) {
610+
case null:
615611
case SimpleTypeKind.Object:
616612
case SimpleTypeKind.Pointer:
617613
case SimpleTypeKind.Single:
@@ -620,13 +616,13 @@ private static int CheckBits(INativeType cdata, PythonTuple pt) {
620616
case SimpleTypeKind.CharPointer:
621617
case SimpleTypeKind.WChar:
622618
case SimpleTypeKind.WCharPointer:
619+
case SimpleTypeKind.BStr:
623620
throw PythonOps.TypeError("bit fields not allowed for type {0}", ((PythonType)cdata).Name);
624621
}
625622

626623
if (bitCount <= 0 || bitCount > cdata.Size * 8) {
627-
throw PythonOps.ValueError("number of bits invalid for bit field");
624+
throw PythonOps.ValueError("number of bits invalid for bit field '{0}'", fieldName);
628625
}
629-
return bitCount;
630626
}
631627

632628
/// <summary>

0 commit comments

Comments
 (0)