Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Src/IronPython.Modules/_warnings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
10 changes: 7 additions & 3 deletions Src/IronPython/Hosting/PythonOptionsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -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" },
Expand Down
2 changes: 1 addition & 1 deletion Src/IronPython/Runtime/ByteArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion Src/IronPython/Runtime/Bytes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 7 additions & 1 deletion Src/IronPython/Runtime/PythonContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions Src/IronPython/Runtime/PythonOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public sealed class PythonOptions : LanguageOptions {
/// </summary>
public ReadOnlyCollection<string>/*!*/ WarningFilters { get; }

public bool BytesWarning { get; }
public Severity BytesWarning { get; }

/// <summary>
/// Enables debugging support. When enabled a .NET debugger can be attached
Expand Down Expand Up @@ -130,7 +130,7 @@ public PythonOptions(IDictionary<string, object> 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);
Expand Down