Skip to content

Commit 1ff0332

Browse files
committed
Exceptions cleanup
1 parent d42aa93 commit 1ff0332

File tree

5 files changed

+46
-62
lines changed

5 files changed

+46
-62
lines changed

Src/IronPython/Runtime/Exceptions/PythonExceptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ internal static BaseException CreateBaseExceptionForRaise(CodeContext/*!*/ conte
719719
/// <summary>
720720
/// Given a CLR exception returns the Python exception which most closely maps to the CLR exception.
721721
/// </summary>
722-
public static object ToPython(System.Exception/*!*/ clrException) {
722+
public static BaseException ToPython(System.Exception/*!*/ clrException) {
723723
var res = clrException.GetPythonException();
724724
if (res is null) {
725725
// explicit extra conversions that need a special transformation

Src/IronPython/Runtime/Exceptions/SystemExitException.cs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
6+
57
using System;
68
using System.Runtime.Serialization;
9+
710
using IronPython.Modules;
811
using IronPython.Runtime.Operations;
912
using IronPython.Runtime.Types;
@@ -15,15 +18,19 @@ namespace IronPython.Runtime.Exceptions {
1518
[Serializable]
1619
public sealed class SystemExitException : Exception {
1720
public SystemExitException() : base() { }
21+
1822
public SystemExitException(string msg)
1923
: base(msg) {
2024
}
25+
2126
public SystemExitException(string message, Exception innerException)
2227
: base(message, innerException) {
2328
}
29+
2430
#if FEATURE_SERIALIZATION
2531
private SystemExitException(SerializationInfo info, StreamingContext context) : base(info, context) { }
2632
#endif
33+
2734
/// <summary>
2835
/// Result of sys.exit(n)
2936
/// </summary>
@@ -36,30 +43,26 @@ private SystemExitException(SerializationInfo info, StreamingContext context) :
3643
/// int_value if the script exited using "sys.exit(int_value)"
3744
/// 1 otherwise
3845
/// </returns>
39-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1007:UseGenericsWhereAppropriate")]
4046
[PythonHidden]
41-
public int GetExitCode(out object otherCode) {
47+
public int GetExitCode(out object? otherCode) {
4248
otherCode = null;
43-
object pyObj = PythonExceptions.ToPython(this);
49+
var pyObj = PythonExceptions.ToPython(this);
4450

45-
object args;
46-
PythonTuple t;
47-
48-
if (!PythonOps.TryGetBoundAttr(pyObj, "args", out args) ||
49-
(t = args as PythonTuple) == null ||
50-
t.__len__() == 0) {
51-
return 0;
52-
} else if (Builtin.isinstance(t[0], TypeCache.Int32)) {
53-
return Converter.ConvertToInt32(t[0]);
54-
} else if (Builtin.isinstance(t[0], TypeCache.BigInteger)) {
55-
var b = Converter.ConvertToBigInteger(t[0]);
56-
if (b > int.MaxValue || b < int.MinValue) {
57-
return -1;
51+
if (PythonOps.TryGetBoundAttr(pyObj, "code", out object? code)) {
52+
if (code is null) {
53+
return 0;
54+
} else if (Builtin.isinstance(code, TypeCache.Int32)) {
55+
return Converter.ConvertToInt32(code);
56+
} else if (Builtin.isinstance(code, TypeCache.BigInteger)) {
57+
var b = Converter.ConvertToBigInteger(code);
58+
if (b > int.MaxValue || b < int.MinValue) {
59+
return -1;
60+
}
61+
return (int)b;
62+
} else {
63+
otherCode = code;
5864
}
59-
return (int)b;
6065
}
61-
62-
otherCode = t[0];
6366
return 1;
6467
}
6568
}

Src/IronPython/Runtime/Exceptions/TraceBack.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,15 @@
88
using System.Runtime.CompilerServices;
99
using System.Text;
1010

11-
using Microsoft.Scripting;
12-
using Microsoft.Scripting.Runtime;
13-
1411
using IronPython.Runtime.Operations;
1512

16-
[module: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", Scope = "member", Target = "IronPython.Runtime.Exceptions.TraceBackFrame..ctor(System.Object,System.Object,System.Object)", MessageId = "0#globals")]
17-
[module: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", Scope = "member", Target = "IronPython.Runtime.Exceptions.TraceBackFrame.Globals", MessageId = "Globals")]
13+
using Microsoft.Scripting;
14+
using Microsoft.Scripting.Runtime;
1815

1916
namespace IronPython.Runtime.Exceptions {
2017
[PythonType("traceback")]
2118
[Serializable]
22-
public class TraceBack {
19+
public sealed class TraceBack {
2320
private readonly TraceBack _next;
2421
private readonly TraceBackFrame _frame;
2522
private int _line;
@@ -79,7 +76,7 @@ internal string Extract() {
7976
[PythonType("frame")]
8077
[DebuggerDisplay("Code = {f_code.co_name}, Line = {f_lineno}")]
8178
[Serializable]
82-
public class TraceBackFrame {
79+
public sealed class TraceBackFrame {
8380
private readonly PythonTracebackListener _traceAdapter;
8481
private TracebackDelegate _trace;
8582
private object _traceObject;
@@ -118,11 +115,11 @@ internal TraceBackFrame(PythonTracebackListener traceAdapter, FunctionCode code,
118115

119116
[SpecialName, PropertyMethod]
120117
public object Getf_trace() {
121-
if (_traceAdapter != null) {
122-
return _traceObject;
123-
} else {
124-
return null;
125-
}
118+
if (_traceAdapter != null) {
119+
return _traceObject;
120+
} else {
121+
return null;
122+
}
126123
}
127124

128125
[SpecialName, PropertyMethod]
@@ -230,7 +227,7 @@ private void SetLineNumber(int newLineNum) {
230227

231228
Dictionary<int, bool> currentLoopIds = null;
232229
bool inForLoopOrFinally = loopAndFinallyLocations != null && loopAndFinallyLocations.TryGetValue(_lineNo, out currentLoopIds);
233-
230+
234231
int originalNewLine = newLineNum;
235232

236233
if (newLineNum < funcCode.Span.Start.Line) {
@@ -246,7 +243,7 @@ private void SetLineNumber(int newLineNum) {
246243
// Check if we're jumping onto a handler
247244
bool handlerIsFinally;
248245
if (handlerLocations != null && handlerLocations.TryGetValue(newLineNum, out handlerIsFinally)) {
249-
throw PythonOps.ValueError("can't jump to 'except' line");
246+
throw PythonOps.ValueError("can't jump to 'except' line");
250247
}
251248

252249
// Check if we're jumping into a for-loop

Src/IronPython/Runtime/Operations/PythonOps.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,20 +2457,9 @@ public static PythonTuple GetExceptionInfo(CodeContext/*!*/ context) {
24572457
return PythonTuple.MakeTuple(null, null, null);
24582458
}
24592459

2460-
PythonContext pc = context.LanguageContext;
2461-
2462-
object pyExcep = PythonExceptions.ToPython(ex);
2463-
TraceBack? tb = CreateTraceBack(pc, ex);
2464-
2465-
object excType;
2466-
if (pyExcep is IPythonObject pyObj) {
2467-
// class is always the Python type for new-style types (this is also the common case)
2468-
excType = pyObj.PythonType;
2469-
} else {
2470-
excType = PythonOps.GetBoundAttr(context, pyExcep, "__class__");
2471-
}
2472-
2473-
return PythonTuple.MakeTuple(excType, pyExcep, tb);
2460+
var pyExcep = PythonExceptions.ToPython(ex);
2461+
TraceBack? tb = CreateTraceBack(context.LanguageContext, ex);
2462+
return PythonTuple.MakeTuple(((IPythonObject)pyExcep).PythonType, pyExcep, tb);
24742463
}
24752464

24762465
/// <summary>

Src/IronPython/Runtime/PythonContext.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,7 @@ public override string FormatException(Exception exception) {
13321332

13331333
StringBuilder result = new StringBuilder();
13341334

1335-
object pythonEx = PythonExceptions.ToPython(exception);
1335+
var pythonEx = PythonExceptions.ToPython(exception);
13361336

13371337
if (exception.InnerException != null) {
13381338
if (exception.InnerException.GetPythonException() is PythonExceptions.BaseException pythonInnerException) {
@@ -1438,28 +1438,24 @@ private static string FormatCLSException(Exception e) {
14381438
return result.ToString();
14391439
}
14401440

1441-
internal static string FormatPythonException(object pythonException) {
1441+
internal static string FormatPythonException(PythonExceptions.BaseException pythonException) {
14421442
string result = "";
14431443

14441444
// dump the python exception.
14451445
if (pythonException != null) {
1446-
if (pythonException is string str) {
1447-
result += str;
1448-
} else {
1449-
result += GetPythonExceptionClassName(pythonException);
1446+
result += GetPythonExceptionClassName(pythonException);
14501447

1451-
string excepStr = PythonOps.ToString(pythonException);
1448+
string excepStr = PythonOps.ToString(pythonException);
14521449

1453-
if (!string.IsNullOrEmpty(excepStr)) {
1454-
result += ": " + excepStr;
1455-
}
1450+
if (!string.IsNullOrEmpty(excepStr)) {
1451+
result += ": " + excepStr;
14561452
}
14571453
}
14581454

14591455
return result;
14601456
}
14611457

1462-
private static string GetPythonExceptionClassName(object pythonException) {
1458+
private static string GetPythonExceptionClassName(PythonExceptions.BaseException pythonException) {
14631459
string className = string.Empty;
14641460
if (PythonOps.TryGetBoundAttr(pythonException, "__class__", out object val)) {
14651461
if (PythonOps.TryGetBoundAttr(val, "__name__", out val)) {
@@ -1644,9 +1640,8 @@ public override CompilerOptions GetCompilerOptions()
16441640
}
16451641

16461642
public override void GetExceptionMessage(Exception exception, out string message, out string typeName) {
1647-
object pythonEx = PythonExceptions.ToPython(exception);
1648-
1649-
message = FormatPythonException(PythonExceptions.ToPython(exception));
1643+
var pythonEx = PythonExceptions.ToPython(exception);
1644+
message = FormatPythonException(pythonEx);
16501645
typeName = GetPythonExceptionClassName(pythonEx);
16511646
}
16521647

0 commit comments

Comments
 (0)