Skip to content

Commit 623c39b

Browse files
committed
Remove obsolete serialization infrastructure and dead preprocessor directives
Serialization cleanup (breaking on .NET 8+, removed in .NET 9+): - Fx.cs: Remove [Serializable] and serialization constructors from InternalException and FatalInternalException; remove unused System.Runtime.Serialization using - NumSharpException.cs: Remove serialization constructor (SerializationInfo, StreamingContext) and System.Runtime.Serialization using - Hashset`1.cs: Remove ISerializable, IDeserializationCallback interfaces; remove [Serializable] attribute; remove serialization constructor, GetObjectData, OnDeserialization, SerializationInfo field, and serialization string constants; remove System.Runtime.Serialization and System.Security.Permissions usings - ConcurrentHashset`1.cs: Remove ISerializable, IDeserializationCallback interfaces; remove [Serializable]; remove serialization constructor, GetObjectData, OnDeserialization delegates; remove System.Runtime.Serialization using - Randomizer.cs: Remove [Serializable]; update doc comment - NativeRandomState.cs: Remove [Serializable] Dead preprocessor directive cleanup (always true/false on net8.0+): - np.load.cs: Remove 24 dead #if directives (#if !NET35, #if !NET40, #if NETSTANDARD1_4, #if !NETSTANDARD1_4) — all evaluate to their always-taken branch on net8.0/net10.0. Keeps ICloneable, IStructuralComparable, IStructuralEquatable constraints (always available). Keeps leaveOpen: true on BinaryReader (always available). Resolves readStringMatrix to use sbyte* constructor (not NETSTANDARD1_4 char* path). - np.save.cs: Remove 4 dead #if directives — keeps .LongLength (not .Length), keeps leaveOpen BinaryWriter constructor. - NpzDictionary.cs: Remove 1 dead #if !NETSTANDARD1_4 around ICloneable. - Hashset`1.cs: Remove #if !SILVERLIGHT (6 blocks), #if FEATURE_NETCORE, #if FEATURE_RANDOMIZED_STRING_HASHING blocks — none of these symbols are defined on net8.0/net10.0. Net result: -243 lines, 0 test regressions. Both TFMs: 1601 passed, 50 failed, 36 skipped — identical to baseline.
1 parent 715c423 commit 623c39b

9 files changed

Lines changed: 5 additions & 243 deletions

File tree

src/NumSharp.Core/APIs/np.load.cs

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static partial class np
1111
{
1212
#region NpyFormat
1313

14-
//Signature from numpy doc:
14+
//Signature from numpy doc:
1515
// numpy.load(file, mmap_mode=None, allow_pickle=True, fix_imports=True, encoding='ASCII')[source]
1616
public static NDArray load(string path)
1717
{
@@ -22,9 +22,7 @@ public static NDArray load(string path)
2222
public static NDArray load(Stream stream)
2323
{
2424
using (var reader = new BinaryReader(stream, System.Text.Encoding.ASCII
25-
#if !NET35 && !NET40
2625
, leaveOpen: true
27-
#endif
2826
))
2927
{
3028
int bytes;
@@ -42,13 +40,9 @@ public static NDArray load(Stream stream)
4240

4341
public static T Load<T>(byte[] bytes)
4442
where T : class,
45-
#if !NETSTANDARD1_4
4643
ICloneable,
47-
#endif
4844
IList, ICollection, IEnumerable
49-
#if !NET35
5045
, IStructuralComparable, IStructuralEquatable
51-
#endif
5246
{
5347
if (typeof(T).IsArray && (typeof(T).GetElementType().IsArray || typeof(T).GetElementType() == typeof(string)))
5448
return LoadJagged(bytes) as T;
@@ -57,54 +51,38 @@ public static T Load<T>(byte[] bytes)
5751

5852
public static T Load<T>(byte[] bytes, out T value)
5953
where T : class,
60-
#if !NETSTANDARD1_4
6154
ICloneable,
62-
#endif
6355
IList, ICollection, IEnumerable
64-
#if !NET35
6556
, IStructuralComparable, IStructuralEquatable
66-
#endif
6757
{
6858
return value = Load<T>(bytes);
6959
}
7060

7161

7262
public static T Load<T>(string path, out T value)
7363
where T : class,
74-
#if !NETSTANDARD1_4
7564
ICloneable,
76-
#endif
7765
IList, ICollection, IEnumerable
78-
#if !NET35
7966
, IStructuralComparable, IStructuralEquatable
80-
#endif
8167
{
8268
return value = Load<T>(path);
8369
}
8470

8571
public static T Load<T>(Stream stream, out T value)
8672
where T : class,
87-
#if !NETSTANDARD1_4
8873
ICloneable,
89-
#endif
9074
IList, ICollection, IEnumerable
91-
#if !NET35
9275
, IStructuralComparable, IStructuralEquatable
93-
#endif
9476
{
9577
return value = Load<T>(stream);
9678
}
9779

9880

9981
public static T Load<T>(string path)
10082
where T : class,
101-
#if !NETSTANDARD1_4
10283
ICloneable,
103-
#endif
10484
IList, ICollection, IEnumerable
105-
#if !NET35
10685
, IStructuralComparable, IStructuralEquatable
107-
#endif
10886
{
10987
using (var stream = new FileStream(path, FileMode.Open))
11088
return Load<T>(stream);
@@ -113,13 +91,9 @@ public static T Load<T>(string path)
11391

11492
public static T Load<T>(Stream stream)
11593
where T : class,
116-
#if !NETSTANDARD1_4
11794
ICloneable,
118-
#endif
11995
IList, ICollection, IEnumerable
120-
#if !NET35
12196
, IStructuralComparable, IStructuralEquatable
122-
#endif
12397
{
12498
if (typeof(T).IsArray && (typeof(T).GetElementType().IsArray || typeof(T).GetElementType() == typeof(string)))
12599
return LoadJagged(stream) as T;
@@ -155,9 +129,7 @@ public static Array LoadJagged(string path)
155129
public static Array LoadMatrix(Stream stream)
156130
{
157131
using (var reader = new BinaryReader(stream, System.Text.Encoding.ASCII
158-
#if !NET35 && !NET40
159132
, leaveOpen: true
160-
#endif
161133
))
162134
{
163135
int bytes;
@@ -178,9 +150,7 @@ public static Array LoadMatrix(Stream stream)
178150
public static Array LoadJagged(Stream stream, bool trim = true)
179151
{
180152
using (var reader = new BinaryReader(stream, System.Text.Encoding.ASCII
181-
#if !NET35 && !NET40
182153
, leaveOpen: true
183-
#endif
184154
))
185155
{
186156
int bytes;
@@ -301,11 +271,7 @@ private static Array readStringMatrix(BinaryReader reader, Array matrix, int byt
301271
}
302272
}
303273

304-
#if NETSTANDARD1_4
305-
String s = new String((char*)b);
306-
#else
307274
String s = new String((sbyte*)b);
308-
#endif
309275
matrix.SetValue(value: s, indices: p);
310276
}
311277
}
@@ -426,9 +392,7 @@ private static Type GetType(string dtype, out int bytes, out bool? isLittleEndia
426392

427393
public static void Load_Npz<T>(byte[] bytes, out T value)
428394
where T : class,
429-
#if !NETSTANDARD1_4
430395
ICloneable,
431-
#endif
432396
IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
433397
{
434398
using (var dict = Load_Npz<T>(bytes))
@@ -439,9 +403,7 @@ public static void Load_Npz<T>(byte[] bytes, out T value)
439403

440404
public static void Load_Npz<T>(string path, out T value)
441405
where T : class,
442-
#if !NETSTANDARD1_4
443406
ICloneable,
444-
#endif
445407
IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
446408
{
447409
using (var dict = Load_Npz<T>(path))
@@ -452,9 +414,7 @@ public static void Load_Npz<T>(string path, out T value)
452414

453415
public static void Load_Npz<T>(Stream stream, out T value)
454416
where T : class,
455-
#if !NETSTANDARD1_4
456417
ICloneable,
457-
#endif
458418
IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
459419
{
460420
using (var dict = Load_Npz<T>(stream))
@@ -465,49 +425,39 @@ public static void Load_Npz<T>(Stream stream, out T value)
465425

466426
public static NpzDictionary<T> Load_Npz<T>(byte[] bytes)
467427
where T : class,
468-
#if !NETSTANDARD1_4
469428
ICloneable,
470-
#endif
471429
IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
472430
{
473431
return Load_Npz<T>(new MemoryStream(bytes));
474432
}
475433

476434
public static NpzDictionary<T> Load_Npz<T>(string path, out NpzDictionary<T> value)
477435
where T : class,
478-
#if !NETSTANDARD1_4
479436
ICloneable,
480-
#endif
481437
IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
482438
{
483439
return value = Load_Npz<T>(new FileStream(path, FileMode.Open));
484440
}
485441

486442
public static NpzDictionary<T> Load_Npz<T>(Stream stream, out NpzDictionary<T> value)
487443
where T : class,
488-
#if !NETSTANDARD1_4
489444
ICloneable,
490-
#endif
491445
IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
492446
{
493447
return value = Load_Npz<T>(stream);
494448
}
495449

496450
public static NpzDictionary<T> Load_Npz<T>(string path)
497451
where T : class,
498-
#if !NETSTANDARD1_4
499452
ICloneable,
500-
#endif
501453
IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
502454
{
503455
return Load_Npz<T>(new FileStream(path, FileMode.Open));
504456
}
505457

506458
public static NpzDictionary<T> Load_Npz<T>(Stream stream)
507459
where T : class,
508-
#if !NETSTANDARD1_4
509460
ICloneable,
510-
#endif
511461
IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable
512462
{
513463
return new NpzDictionary<T>(stream);

src/NumSharp.Core/APIs/np.save.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ public static ulong Save(Array array, string path)
4545
public static ulong Save(Array array, Stream stream)
4646
{
4747
using (var writer = new BinaryWriter(stream
48-
#if !NET35 && !NET40
4948
, System.Text.Encoding.ASCII, leaveOpen: true
50-
#endif
5149
))
5250
{
5351
Type type;
@@ -83,11 +81,7 @@ private static ulong writeValueMatrix(BinaryWriter reader, Array matrix, int byt
8381
Buffer.BlockCopy(matrix, 0, buffer, 0, buffer.Length);
8482
reader.Write(buffer, 0, buffer.Length);
8583

86-
#if NETSTANDARD1_4
87-
return (ulong)buffer.Length;
88-
#else
8984
return (ulong)buffer.LongLength;
90-
#endif
9185
}
9286

9387
static IEnumerable<T> Enumerate<T>(Array a, int[] dimensions, int pos)
@@ -117,11 +111,7 @@ private static ulong writeValueJagged(BinaryWriter reader, Array matrix, int byt
117111
Array.Clear(buffer, arr.Length, buffer.Length - buffer.Length);
118112
Buffer.BlockCopy(arr, 0, buffer, 0, buffer.Length);
119113
reader.Write(buffer, 0, buffer.Length);
120-
#if NETSTANDARD1_4
121-
writtenBytes += (ulong)buffer.Length;
122-
#else
123114
writtenBytes += (ulong)buffer.LongLength;
124-
#endif
125115
}
126116

127117
return writtenBytes;
@@ -158,11 +148,7 @@ private static ulong writeStringMatrix(BinaryWriter reader, Array matrix, int by
158148
reader.Write(empty, 0, bytes);
159149
}
160150

161-
#if NETSTANDARD1_4
162-
writtenBytes += (ulong)buffer.Length;
163-
#else
164151
writtenBytes += (ulong)buffer.LongLength;
165-
#endif
166152
}
167153
}
168154
}

src/NumSharp.Core/Backends/Unmanaged/Pooling/Fx.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Reflection;
55
using System.Runtime.CompilerServices;
66
using System.Runtime.ConstrainedExecution;
7-
using System.Runtime.Serialization;
87
using System.Security;
98
using System.Threading;
109
using NumSharp.Utilities;
@@ -937,28 +936,18 @@ void UnhandledExceptionFrame(uint error, uint bytesRead, NativeOverlapped* nativ
937936
}
938937
}
939938

940-
[Serializable]
941939
class InternalException : SystemException
942940
{
943941
public InternalException(string description)
944942
: base(description)
945943
{ }
946-
947-
protected InternalException(SerializationInfo info, StreamingContext context)
948-
: base(info, context)
949-
{ }
950944
}
951945

952-
[Serializable]
953946
class FatalInternalException : InternalException
954947
{
955948
public FatalInternalException(string description)
956949
: base(description)
957950
{ }
958-
959-
protected FatalInternalException(SerializationInfo info, StreamingContext context)
960-
: base(info, context)
961-
{ }
962951
}
963952
}
964953
}

src/NumSharp.Core/Exceptions/NumSharpException.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Runtime.Serialization;
32

43
namespace NumSharp
54
{
@@ -11,14 +10,6 @@ public class NumSharpException : Exception, INumSharpException
1110
public NumSharpException()
1211
{ }
1312

14-
/// <summary>Initializes a new instance of the <see cref="T:System.Exception"></see> class with serialized data.</summary>
15-
/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"></see> that holds the serialized object data about the exception being thrown.</param>
16-
/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"></see> that contains contextual information about the source or destination.</param>
17-
/// <exception cref="T:System.ArgumentNullException">The <paramref name="info">info</paramref> parameter is null.</exception>
18-
/// <exception cref="T:System.Runtime.Serialization.SerializationException">The class name is null or <see cref="P:System.Exception.HResult"></see> is zero (0).</exception>
19-
protected NumSharpException(SerializationInfo info, StreamingContext context) : base(info, context)
20-
{ }
21-
2213
/// <summary>Initializes a new instance of the <see cref="T:System.Exception"></see> class with a specified error message.</summary>
2314
/// <param name="message">The message that describes the error.</param>
2415
public NumSharpException(string message) : base(message)

src/NumSharp.Core/RandomSampling/NativeRandomState.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public static Randomizer Restore(this NativeRandomState state)
1818
/// <summary>
1919
/// Represents the stored state of <see cref="Randomizer"/>.
2020
/// </summary>
21-
[Serializable]
2221
public struct NativeRandomState
2322
{
2423
public readonly byte[] State;

src/NumSharp.Core/RandomSampling/Randomizer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ namespace NumSharp
55
{
66
/// <summary>
77
/// Represents a pseudo-random number generator, which is a device that produces a sequence of numbers that meet certain statistical requirements for randomness.<br></br>
8-
/// Equivalent of <see cref="System.Random"/> but with a <see cref="SerializableAttribute"/>.
8+
/// Equivalent of <see cref="System.Random"/>.
99
/// </summary>
1010
/// <remarks>Copied and modified from https://referencesource.microsoft.com/#mscorlib/system/random.cs</remarks>
11-
[Serializable]
1211
public sealed class Randomizer : ICloneable
1312
{
1413
private const int MBIG = int.MaxValue;

src/NumSharp.Core/Utilities/ConcurrentHashset`1.cs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Diagnostics;
5-
using System.Runtime.Serialization;
65
using System.Threading;
76
using NumSharp.Backends.Unmanaged;
87

98
namespace NumSharp.Utilities
109
{
1110
[DebuggerDisplay("Count = {Count}")]
12-
[Serializable]
13-
public class ConcurrentHashset<T> : ICollection<T>, ISet<T>, ISerializable, IDeserializationCallback where T : unmanaged
11+
public class ConcurrentHashset<T> : ICollection<T>, ISet<T> where T : unmanaged
1412
{
1513
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
1614

@@ -34,15 +32,6 @@ public ConcurrentHashset(IEnumerable<T> collection, IEqualityComparer<T> compare
3432
hashset = new Hashset<T>(collection, comparer);
3533
}
3634

37-
protected ConcurrentHashset(SerializationInfo info, StreamingContext context)
38-
{
39-
hashset = new Hashset<T>();
40-
41-
// not sure about this one really...
42-
var iSerializable = hashset as ISerializable;
43-
iSerializable.GetObjectData(info, context);
44-
}
45-
4635
#region Dispose
4736

4837
public void Dispose()
@@ -68,16 +57,6 @@ public IEnumerator<T> GetEnumerator()
6857
Dispose(false);
6958
}
7059

71-
public void OnDeserialization(object sender)
72-
{
73-
hashset.OnDeserialization(sender);
74-
}
75-
76-
public void GetObjectData(SerializationInfo info, StreamingContext context)
77-
{
78-
hashset.GetObjectData(info, context);
79-
}
80-
8160
IEnumerator IEnumerable.GetEnumerator()
8261
{
8362
return GetEnumerator();

0 commit comments

Comments
 (0)