Skip to content

Commit 54ee753

Browse files
authored
Cleanup exception related code (#1146)
* Change type of IPythonAwareException.PythonException to BaseException * Cleanup exceptions * Extend nullable annotations * Cleanup
1 parent 1b7d6a5 commit 54ee753

File tree

47 files changed

+458
-555
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+458
-555
lines changed

Src/IronPython/Modules/Builtin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static object __import__(CodeContext/*!*/ context, [NotNull]string name,
6161
object ret = Importer.ImportModule(context, globals, name, from != null && from.Count > 0, level);
6262
if (ret == null) {
6363
var err = PythonOps.ImportError("No module named '{0}'", name);
64-
((PythonExceptions._ImportError)err.GetPythonException()).name = name;
64+
((PythonExceptions._ImportError)err.GetPythonException()!).name = name;
6565
return LightExceptions.Throw(err);
6666
}
6767

Src/IronPython/Modules/sys.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ public static void displayhookImpl(CodeContext/*!*/ context, object value) {
9090
Handle an exception by displaying it with a traceback on sys.stderr._")]
9191
public static void excepthookImpl(CodeContext/*!*/ context, object exctype, object value, object traceback) {
9292
PythonContext pc = context.LanguageContext;
93+
var exc = PythonExceptions.ToClr(value);
94+
if (exc is null) throw PythonOps.TypeError($"Exception expected for {nameof(value)}, {PythonTypeOps.GetName(value)} found");
9395

9496
PythonOps.PrintWithDest(
9597
context,

Src/IronPython/Runtime/Exceptions/AssertionException.Generated.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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.Collections.Generic;
79
using System.Runtime.Serialization;
@@ -13,12 +15,11 @@ namespace IronPython.Runtime.Exceptions {
1315
// *** BEGIN GENERATED CODE ***
1416
// generated by function: gen_one_exception_specialized from: generate_exceptions.py
1517

16-
1718
[Serializable]
1819
public class AssertionException : Exception, IPythonAwareException {
19-
private object _pyExceptionObject;
20-
private List<DynamicStackFrame> _frames;
21-
private TraceBack _traceback;
20+
private PythonExceptions.BaseException? _pyExceptionObject;
21+
private List<DynamicStackFrame>? _frames;
22+
private TraceBack? _traceback;
2223

2324
public AssertionException() : base() { }
2425
public AssertionException(string msg) : base(msg) { }
@@ -28,38 +29,36 @@ public AssertionException(string message, Exception innerException)
2829
#if FEATURE_SERIALIZATION
2930
protected AssertionException(SerializationInfo info, StreamingContext context) : base(info, context) { }
3031

31-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
3232
public override void GetObjectData(SerializationInfo info, StreamingContext context) {
3333
info.AddValue("frames", _frames);
3434
info.AddValue("traceback", _traceback);
3535
base.GetObjectData(info, context);
3636
}
3737
#endif
3838

39-
object IPythonAwareException.PythonException {
40-
get {
39+
PythonExceptions.BaseException? IPythonAwareException.PythonException {
40+
get {
4141
if (_pyExceptionObject == null) {
4242
var newEx = new PythonExceptions.BaseException(PythonExceptions.AssertionError);
4343
newEx.InitializeFromClr(this);
4444
_pyExceptionObject = newEx;
4545
}
46-
return _pyExceptionObject;
46+
return _pyExceptionObject;
4747
}
4848
set { _pyExceptionObject = value; }
4949
}
5050

51-
List<DynamicStackFrame> IPythonAwareException.Frames {
51+
List<DynamicStackFrame>? IPythonAwareException.Frames {
5252
get { return _frames; }
5353
set { _frames = value; }
5454
}
5555

56-
TraceBack IPythonAwareException.TraceBack {
56+
TraceBack? IPythonAwareException.TraceBack {
5757
get { return _traceback; }
5858
set { _traceback = value; }
5959
}
6060
}
6161

62-
6362
// *** END GENERATED CODE ***
6463

6564
#endregion

Src/IronPython/Runtime/Exceptions/AttributeErrorException.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace IronPython.Runtime.Exceptions {
1616

1717
[Serializable]
1818
public class AttributeErrorException : MissingMemberException, IPythonAwareException {
19-
private object _pyExceptionObject;
19+
private PythonExceptions.BaseException _pyExceptionObject;
2020
private List<DynamicStackFrame> _frames;
2121
private TraceBack _traceback;
2222

@@ -36,7 +36,7 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont
3636
}
3737
#endif
3838

39-
object IPythonAwareException.PythonException {
39+
PythonExceptions.BaseException IPythonAwareException.PythonException {
4040
get { return _pyExceptionObject; }
4141
set { _pyExceptionObject = value; }
4242
}

Src/IronPython/Runtime/Exceptions/BlockingIOException.Generated.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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.Collections.Generic;
79
using System.Runtime.Serialization;
@@ -13,12 +15,11 @@ namespace IronPython.Runtime.Exceptions {
1315
// *** BEGIN GENERATED CODE ***
1416
// generated by function: gen_one_exception_specialized from: generate_exceptions.py
1517

16-
1718
[Serializable]
1819
public class BlockingIOException : OSException, IPythonAwareException {
19-
private object _pyExceptionObject;
20-
private List<DynamicStackFrame> _frames;
21-
private TraceBack _traceback;
20+
private PythonExceptions.BaseException? _pyExceptionObject;
21+
private List<DynamicStackFrame>? _frames;
22+
private TraceBack? _traceback;
2223

2324
public BlockingIOException() : base() { }
2425
public BlockingIOException(string msg) : base(msg) { }
@@ -28,40 +29,37 @@ public BlockingIOException(string message, Exception innerException)
2829
#if FEATURE_SERIALIZATION
2930
protected BlockingIOException(SerializationInfo info, StreamingContext context) : base(info, context) { }
3031

31-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
3232
public override void GetObjectData(SerializationInfo info, StreamingContext context) {
3333
info.AddValue("frames", _frames);
3434
info.AddValue("traceback", _traceback);
3535
base.GetObjectData(info, context);
3636
}
3737
#endif
3838

39-
object IPythonAwareException.PythonException {
40-
get {
39+
PythonExceptions.BaseException? IPythonAwareException.PythonException {
40+
get {
4141
if (_pyExceptionObject == null) {
4242
var newEx = new PythonExceptions._BlockingIOError();
4343
newEx.InitializeFromClr(this);
4444
_pyExceptionObject = newEx;
4545
}
46-
return _pyExceptionObject;
46+
return _pyExceptionObject;
4747
}
4848
set { _pyExceptionObject = value; }
4949
}
5050

51-
List<DynamicStackFrame> IPythonAwareException.Frames {
51+
List<DynamicStackFrame>? IPythonAwareException.Frames {
5252
get { return _frames; }
5353
set { _frames = value; }
5454
}
5555

56-
TraceBack IPythonAwareException.TraceBack {
56+
TraceBack? IPythonAwareException.TraceBack {
5757
get { return _traceback; }
5858
set { _traceback = value; }
5959
}
6060
}
6161

62-
6362
// *** END GENERATED CODE ***
6463

6564
#endregion
66-
6765
}

Src/IronPython/Runtime/Exceptions/BrokenPipeException.Generated.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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.Collections.Generic;
79
using System.Runtime.Serialization;
@@ -13,12 +15,11 @@ namespace IronPython.Runtime.Exceptions {
1315
// *** BEGIN GENERATED CODE ***
1416
// generated by function: gen_one_exception_specialized from: generate_exceptions.py
1517

16-
1718
[Serializable]
1819
public class BrokenPipeException : ConnectionException, IPythonAwareException {
19-
private object _pyExceptionObject;
20-
private List<DynamicStackFrame> _frames;
21-
private TraceBack _traceback;
20+
private PythonExceptions.BaseException? _pyExceptionObject;
21+
private List<DynamicStackFrame>? _frames;
22+
private TraceBack? _traceback;
2223

2324
public BrokenPipeException() : base() { }
2425
public BrokenPipeException(string msg) : base(msg) { }
@@ -28,15 +29,14 @@ public BrokenPipeException(string message, Exception innerException)
2829
#if FEATURE_SERIALIZATION
2930
protected BrokenPipeException(SerializationInfo info, StreamingContext context) : base(info, context) { }
3031

31-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
3232
public override void GetObjectData(SerializationInfo info, StreamingContext context) {
3333
info.AddValue("frames", _frames);
3434
info.AddValue("traceback", _traceback);
3535
base.GetObjectData(info, context);
3636
}
3737
#endif
3838

39-
object IPythonAwareException.PythonException {
39+
PythonExceptions.BaseException? IPythonAwareException.PythonException {
4040
get {
4141
if (_pyExceptionObject == null) {
4242
var newEx = new PythonExceptions._OSError(PythonExceptions.BrokenPipeError);
@@ -48,18 +48,17 @@ object IPythonAwareException.PythonException {
4848
set { _pyExceptionObject = value; }
4949
}
5050

51-
List<DynamicStackFrame> IPythonAwareException.Frames {
51+
List<DynamicStackFrame>? IPythonAwareException.Frames {
5252
get { return _frames; }
5353
set { _frames = value; }
5454
}
5555

56-
TraceBack IPythonAwareException.TraceBack {
56+
TraceBack? IPythonAwareException.TraceBack {
5757
get { return _traceback; }
5858
set { _traceback = value; }
5959
}
6060
}
6161

62-
6362
// *** END GENERATED CODE ***
6463

6564
#endregion

Src/IronPython/Runtime/Exceptions/BufferException.Generated.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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.Collections.Generic;
79
using System.Runtime.Serialization;
@@ -13,12 +15,11 @@ namespace IronPython.Runtime.Exceptions {
1315
// *** BEGIN GENERATED CODE ***
1416
// generated by function: gen_one_exception_specialized from: generate_exceptions.py
1517

16-
1718
[Serializable]
1819
public class BufferException : Exception, IPythonAwareException {
19-
private object _pyExceptionObject;
20-
private List<DynamicStackFrame> _frames;
21-
private TraceBack _traceback;
20+
private PythonExceptions.BaseException? _pyExceptionObject;
21+
private List<DynamicStackFrame>? _frames;
22+
private TraceBack? _traceback;
2223

2324
public BufferException() : base() { }
2425
public BufferException(string msg) : base(msg) { }
@@ -28,40 +29,37 @@ public BufferException(string message, Exception innerException)
2829
#if FEATURE_SERIALIZATION
2930
protected BufferException(SerializationInfo info, StreamingContext context) : base(info, context) { }
3031

31-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
3232
public override void GetObjectData(SerializationInfo info, StreamingContext context) {
3333
info.AddValue("frames", _frames);
3434
info.AddValue("traceback", _traceback);
3535
base.GetObjectData(info, context);
3636
}
3737
#endif
3838

39-
object IPythonAwareException.PythonException {
40-
get {
39+
PythonExceptions.BaseException? IPythonAwareException.PythonException {
40+
get {
4141
if (_pyExceptionObject == null) {
4242
var newEx = new PythonExceptions.BaseException(PythonExceptions.BufferError);
4343
newEx.InitializeFromClr(this);
4444
_pyExceptionObject = newEx;
4545
}
46-
return _pyExceptionObject;
46+
return _pyExceptionObject;
4747
}
4848
set { _pyExceptionObject = value; }
4949
}
5050

51-
List<DynamicStackFrame> IPythonAwareException.Frames {
51+
List<DynamicStackFrame>? IPythonAwareException.Frames {
5252
get { return _frames; }
5353
set { _frames = value; }
5454
}
5555

56-
TraceBack IPythonAwareException.TraceBack {
56+
TraceBack? IPythonAwareException.TraceBack {
5757
get { return _traceback; }
5858
set { _traceback = value; }
5959
}
6060
}
6161

62-
6362
// *** END GENERATED CODE ***
6463

6564
#endregion
66-
6765
}

Src/IronPython/Runtime/Exceptions/BytesWarningException.cs renamed to Src/IronPython/Runtime/Exceptions/BytesWarningException.Generated.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
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.Collections.Generic;
79
using System.ComponentModel;
@@ -14,12 +16,11 @@ namespace IronPython.Runtime.Exceptions {
1416
// *** BEGIN GENERATED CODE ***
1517
// generated by function: gen_one_exception_specialized from: generate_exceptions.py
1618

17-
1819
[Serializable]
1920
public class BytesWarningException : WarningException, IPythonAwareException {
20-
private object _pyExceptionObject;
21-
private List<DynamicStackFrame> _frames;
22-
private TraceBack _traceback;
21+
private PythonExceptions.BaseException? _pyExceptionObject;
22+
private List<DynamicStackFrame>? _frames;
23+
private TraceBack? _traceback;
2324

2425
public BytesWarningException() : base() { }
2526
public BytesWarningException(string msg) : base(msg) { }
@@ -29,40 +30,37 @@ public BytesWarningException(string message, Exception innerException)
2930
#if FEATURE_SERIALIZATION
3031
protected BytesWarningException(SerializationInfo info, StreamingContext context) : base(info, context) { }
3132

32-
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
3333
public override void GetObjectData(SerializationInfo info, StreamingContext context) {
3434
info.AddValue("frames", _frames);
3535
info.AddValue("traceback", _traceback);
3636
base.GetObjectData(info, context);
3737
}
3838
#endif
3939

40-
object IPythonAwareException.PythonException {
41-
get {
40+
PythonExceptions.BaseException? IPythonAwareException.PythonException {
41+
get {
4242
if (_pyExceptionObject == null) {
4343
var newEx = new PythonExceptions.BaseException(PythonExceptions.BytesWarning);
4444
newEx.InitializeFromClr(this);
4545
_pyExceptionObject = newEx;
4646
}
47-
return _pyExceptionObject;
47+
return _pyExceptionObject;
4848
}
4949
set { _pyExceptionObject = value; }
5050
}
5151

52-
List<DynamicStackFrame> IPythonAwareException.Frames {
52+
List<DynamicStackFrame>? IPythonAwareException.Frames {
5353
get { return _frames; }
5454
set { _frames = value; }
5555
}
5656

57-
TraceBack IPythonAwareException.TraceBack {
57+
TraceBack? IPythonAwareException.TraceBack {
5858
get { return _traceback; }
5959
set { _traceback = value; }
6060
}
6161
}
6262

63-
6463
// *** END GENERATED CODE ***
6564

6665
#endregion
67-
6866
}

0 commit comments

Comments
 (0)