From a5f498c39d8f750956f9ed16683ac6ed12946c00 Mon Sep 17 00:00:00 2001 From: Pavel Koneski Date: Tue, 13 Oct 2020 20:20:21 -0700 Subject: [PATCH] Complete support of commandline options -b and -bb --- Src/IronPython.Modules/_warnings.cs | 8 +++++++- Src/IronPython/Hosting/PythonOptionsParser.cs | 10 +++++++--- Src/IronPython/Runtime/ByteArray.cs | 2 +- Src/IronPython/Runtime/Bytes.cs | 2 +- Src/IronPython/Runtime/PythonContext.cs | 8 +++++++- Src/IronPython/Runtime/PythonOptions.cs | 4 ++-- 6 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Src/IronPython.Modules/_warnings.cs b/Src/IronPython.Modules/_warnings.cs index 8af7133dc..3270a9d4f 100644 --- a/Src/IronPython.Modules/_warnings.cs +++ b/Src/IronPython.Modules/_warnings.cs @@ -33,7 +33,13 @@ public static void PerformModuleReload(PythonContext/*!*/ context, PythonDiction defaultFilters.AddNoLock(PythonTuple.MakeTuple("ignore", null, PythonExceptions.DeprecationWarning, null, 0)); defaultFilters.AddNoLock(PythonTuple.MakeTuple("ignore", null, PythonExceptions.PendingDeprecationWarning, null, 0)); defaultFilters.AddNoLock(PythonTuple.MakeTuple("ignore", null, PythonExceptions.ImportWarning, null, 0)); - defaultFilters.AddNoLock(PythonTuple.MakeTuple("ignore", null, PythonExceptions.BytesWarning, null, 0)); + + string bytesWarningAction = context.PythonOptions.BytesWarning switch { + Severity.Ignore => "ignore", + Severity.Warning => "default", + _ => "error" + }; + defaultFilters.AddNoLock(PythonTuple.MakeTuple(bytesWarningAction, null, PythonExceptions.BytesWarning, null, 0)); context.GetOrCreateModuleState(_keyFields, () => { dict.Add(_keyDefaultAction, "default"); diff --git a/Src/IronPython/Hosting/PythonOptionsParser.cs b/Src/IronPython/Hosting/PythonOptionsParser.cs index 1b79fbfaa..140fe954d 100644 --- a/Src/IronPython/Hosting/PythonOptionsParser.cs +++ b/Src/IronPython/Hosting/PythonOptionsParser.cs @@ -26,11 +26,14 @@ protected override void ParseArgument(string/*!*/ arg) { switch (arg) { case "-B": break; // dont_write_bytecode always true in IronPython - case "-U": break; // unicode always true in IronPython case "-d": break; // debug output from parser, always False in IronPython - case "-b": // Not shown in help on CPython - LanguageSetup.Options["BytesWarning"] = ScriptingRuntimeHelpers.True; + case "-b": + LanguageSetup.Options["BytesWarning"] = LanguageSetup.Options.ContainsKey("BytesWarning") ? Severity.Error : Severity.Warning; + break; + + case "-bb": + LanguageSetup.Options["BytesWarning"] = Severity.Error; break; case "-c": @@ -232,6 +235,7 @@ public override void GetHelp(out string commandLine, out string[,] options, out #if !IRONPYTHON_WINDOW { "-v", "Verbose (trace import statements) (also PYTHONVERBOSE=x)" }, #endif + { "-b", "issue warnings about str(bytes_instance), str(bytearray_instance) and comparing bytes/bytearray with str. (-bb: issue errors)"}, { "-m module", "run library module as a script"}, { "-x", "Skip first line of the source" }, { "-u", "Unbuffered stdout & stderr" }, diff --git a/Src/IronPython/Runtime/ByteArray.cs b/Src/IronPython/Runtime/ByteArray.cs index e6a74b7eb..7922b0c92 100644 --- a/Src/IronPython/Runtime/ByteArray.cs +++ b/Src/IronPython/Runtime/ByteArray.cs @@ -1080,7 +1080,7 @@ private string Repr() { } public virtual string __str__(CodeContext context) { - if (context.LanguageContext.PythonOptions.BytesWarning) { + if (context.LanguageContext.PythonOptions.BytesWarning != Microsoft.Scripting.Severity.Ignore) { PythonOps.Warn(context, PythonExceptions.BytesWarning, "str() on a bytearray instance"); } return Repr(); diff --git a/Src/IronPython/Runtime/Bytes.cs b/Src/IronPython/Runtime/Bytes.cs index 35b56d900..0c416e14c 100644 --- a/Src/IronPython/Runtime/Bytes.cs +++ b/Src/IronPython/Runtime/Bytes.cs @@ -858,7 +858,7 @@ public PythonTuple __reduce__(CodeContext context) { } public virtual string __str__(CodeContext context) { - if (context.LanguageContext.PythonOptions.BytesWarning) { + if (context.LanguageContext.PythonOptions.BytesWarning != Microsoft.Scripting.Severity.Ignore) { PythonOps.Warn(context, PythonExceptions.BytesWarning, "str() on a bytes instance"); } return _bytes.BytesRepr(); diff --git a/Src/IronPython/Runtime/PythonContext.cs b/Src/IronPython/Runtime/PythonContext.cs index 3eacf9883..da6348dbe 100644 --- a/Src/IronPython/Runtime/PythonContext.cs +++ b/Src/IronPython/Runtime/PythonContext.cs @@ -722,7 +722,13 @@ private void InitializeSysFlags() { } flags.verbose = PythonOptions.Verbose ? 1 : 0; flags.unicode = 1; - flags.bytes_warning = PythonOptions.BytesWarning ? 1 : 0; + flags.bytes_warning = PythonOptions.BytesWarning switch { + Severity.Ignore => 0, + Severity.Warning => 1, + Severity.Error => 2, + Severity.FatalError => 3, + _ => (int)PythonOptions.BytesWarning + }; flags.quiet = PythonOptions.Quiet ? 1 : 0; } diff --git a/Src/IronPython/Runtime/PythonOptions.cs b/Src/IronPython/Runtime/PythonOptions.cs index 871c3936a..dea3b86f3 100644 --- a/Src/IronPython/Runtime/PythonOptions.cs +++ b/Src/IronPython/Runtime/PythonOptions.cs @@ -33,7 +33,7 @@ public sealed class PythonOptions : LanguageOptions { /// public ReadOnlyCollection/*!*/ WarningFilters { get; } - public bool BytesWarning { get; } + public Severity BytesWarning { get; } /// /// Enables debugging support. When enabled a .NET debugger can be attached @@ -130,7 +130,7 @@ public PythonOptions(IDictionary options) Arguments = GetStringCollectionOption(options, "Arguments") ?? EmptyStringCollection; WarningFilters = GetStringCollectionOption(options, "WarningFilters", ';', ',') ?? EmptyStringCollection; - BytesWarning = GetOption(options, "BytesWarning", false); + BytesWarning = GetOption(options, "BytesWarning", Severity.Ignore); Debug = GetOption(options, "Debug", false); Inspect = GetOption(options, "Inspect", false); NoUserSite = GetOption(options, "NoUserSite", false);