Skip to content

Commit e594139

Browse files
authored
Implement conversions to enum options (#1731)
1 parent 3d306d5 commit e594139

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

Src/IronPython/Runtime/PythonOptions.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public PythonOptions(IDictionary<string, object> options)
144144
Arguments = GetStringCollectionOption(options, "Arguments") ?? EmptyStringCollection;
145145
WarningFilters = GetStringCollectionOption(options, "WarningFilters", ';', ',') ?? EmptyStringCollection;
146146

147-
BytesWarning = GetOption(options, "BytesWarning", Severity.Ignore);
147+
BytesWarning = GetEnumOption(options, "BytesWarning", Severity.Ignore);
148148
Debug = GetOption(options, "Debug", false);
149149
Inspect = GetOption(options, "Inspect", false);
150150
NoUserSite = GetOption(options, "NoUserSite", false);
@@ -165,7 +165,7 @@ public PythonOptions(IDictionary<string, object> options)
165165
NoImportLib = GetOption(options, "NoImportLib", false);
166166
Isolated = GetOption(options, "Isolated", false);
167167
Utf8Mode = GetOption(options, "Utf8Mode", false);
168-
ConsoleSupportLevel = GetOption(options, "ConsoleSupportLevel", SharedIO.SupportLevel.Full);
168+
ConsoleSupportLevel = GetEnumOption(options, "ConsoleSupportLevel", SharedIO.SupportLevel.Full);
169169
}
170170

171171
private static IDictionary<string, object> EnsureSearchPaths(IDictionary<string, object> options) {
@@ -176,5 +176,19 @@ private static IDictionary<string, object> EnsureSearchPaths(IDictionary<string,
176176
}
177177
return options;
178178
}
179+
180+
private static T GetEnumOption<T>(IDictionary<string, object> options, string name, T defaultValue) where T : struct, Enum {
181+
if (options != null && options.TryGetValue(name, out object value)) {
182+
if (value is T variable) {
183+
return variable;
184+
}
185+
Type rettype = typeof(T);
186+
if (value is string strval) {
187+
return (T)Enum.Parse(rettype, strval, ignoreCase: false);
188+
}
189+
return (T)Convert.ChangeType(value, Enum.GetUnderlyingType(rettype), CultureInfo.CurrentCulture);
190+
}
191+
return defaultValue;
192+
}
179193
}
180194
}

0 commit comments

Comments
 (0)