Skip to content

Commit 391a240

Browse files
authored
Complete support of commandline options -b and -bb (#981)
1 parent 82f1816 commit 391a240

File tree

6 files changed

+25
-9
lines changed

6 files changed

+25
-9
lines changed

Src/IronPython.Modules/_warnings.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ public static void PerformModuleReload(PythonContext/*!*/ context, PythonDiction
3333
defaultFilters.AddNoLock(PythonTuple.MakeTuple("ignore", null, PythonExceptions.DeprecationWarning, null, 0));
3434
defaultFilters.AddNoLock(PythonTuple.MakeTuple("ignore", null, PythonExceptions.PendingDeprecationWarning, null, 0));
3535
defaultFilters.AddNoLock(PythonTuple.MakeTuple("ignore", null, PythonExceptions.ImportWarning, null, 0));
36-
defaultFilters.AddNoLock(PythonTuple.MakeTuple("ignore", null, PythonExceptions.BytesWarning, null, 0));
36+
37+
string bytesWarningAction = context.PythonOptions.BytesWarning switch {
38+
Severity.Ignore => "ignore",
39+
Severity.Warning => "default",
40+
_ => "error"
41+
};
42+
defaultFilters.AddNoLock(PythonTuple.MakeTuple(bytesWarningAction, null, PythonExceptions.BytesWarning, null, 0));
3743

3844
context.GetOrCreateModuleState(_keyFields, () => {
3945
dict.Add(_keyDefaultAction, "default");

Src/IronPython/Hosting/PythonOptionsParser.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ protected override void ParseArgument(string/*!*/ arg) {
2626

2727
switch (arg) {
2828
case "-B": break; // dont_write_bytecode always true in IronPython
29-
case "-U": break; // unicode always true in IronPython
3029
case "-d": break; // debug output from parser, always False in IronPython
3130

32-
case "-b": // Not shown in help on CPython
33-
LanguageSetup.Options["BytesWarning"] = ScriptingRuntimeHelpers.True;
31+
case "-b":
32+
LanguageSetup.Options["BytesWarning"] = LanguageSetup.Options.ContainsKey("BytesWarning") ? Severity.Error : Severity.Warning;
33+
break;
34+
35+
case "-bb":
36+
LanguageSetup.Options["BytesWarning"] = Severity.Error;
3437
break;
3538

3639
case "-c":
@@ -232,6 +235,7 @@ public override void GetHelp(out string commandLine, out string[,] options, out
232235
#if !IRONPYTHON_WINDOW
233236
{ "-v", "Verbose (trace import statements) (also PYTHONVERBOSE=x)" },
234237
#endif
238+
{ "-b", "issue warnings about str(bytes_instance), str(bytearray_instance) and comparing bytes/bytearray with str. (-bb: issue errors)"},
235239
{ "-m module", "run library module as a script"},
236240
{ "-x", "Skip first line of the source" },
237241
{ "-u", "Unbuffered stdout & stderr" },

Src/IronPython/Runtime/ByteArray.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ private string Repr() {
10801080
}
10811081

10821082
public virtual string __str__(CodeContext context) {
1083-
if (context.LanguageContext.PythonOptions.BytesWarning) {
1083+
if (context.LanguageContext.PythonOptions.BytesWarning != Microsoft.Scripting.Severity.Ignore) {
10841084
PythonOps.Warn(context, PythonExceptions.BytesWarning, "str() on a bytearray instance");
10851085
}
10861086
return Repr();

Src/IronPython/Runtime/Bytes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ public PythonTuple __reduce__(CodeContext context) {
858858
}
859859

860860
public virtual string __str__(CodeContext context) {
861-
if (context.LanguageContext.PythonOptions.BytesWarning) {
861+
if (context.LanguageContext.PythonOptions.BytesWarning != Microsoft.Scripting.Severity.Ignore) {
862862
PythonOps.Warn(context, PythonExceptions.BytesWarning, "str() on a bytes instance");
863863
}
864864
return _bytes.BytesRepr();

Src/IronPython/Runtime/PythonContext.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,13 @@ private void InitializeSysFlags() {
722722
}
723723
flags.verbose = PythonOptions.Verbose ? 1 : 0;
724724
flags.unicode = 1;
725-
flags.bytes_warning = PythonOptions.BytesWarning ? 1 : 0;
725+
flags.bytes_warning = PythonOptions.BytesWarning switch {
726+
Severity.Ignore => 0,
727+
Severity.Warning => 1,
728+
Severity.Error => 2,
729+
Severity.FatalError => 3,
730+
_ => (int)PythonOptions.BytesWarning
731+
};
726732
flags.quiet = PythonOptions.Quiet ? 1 : 0;
727733
}
728734

Src/IronPython/Runtime/PythonOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public sealed class PythonOptions : LanguageOptions {
3333
/// </summary>
3434
public ReadOnlyCollection<string>/*!*/ WarningFilters { get; }
3535

36-
public bool BytesWarning { get; }
36+
public Severity BytesWarning { get; }
3737

3838
/// <summary>
3939
/// Enables debugging support. When enabled a .NET debugger can be attached
@@ -130,7 +130,7 @@ public PythonOptions(IDictionary<string, object> options)
130130
Arguments = GetStringCollectionOption(options, "Arguments") ?? EmptyStringCollection;
131131
WarningFilters = GetStringCollectionOption(options, "WarningFilters", ';', ',') ?? EmptyStringCollection;
132132

133-
BytesWarning = GetOption(options, "BytesWarning", false);
133+
BytesWarning = GetOption(options, "BytesWarning", Severity.Ignore);
134134
Debug = GetOption(options, "Debug", false);
135135
Inspect = GetOption(options, "Inspect", false);
136136
NoUserSite = GetOption(options, "NoUserSite", false);

0 commit comments

Comments
 (0)