forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementation.cs
More file actions
112 lines (96 loc) · 4.18 KB
/
Implementation.cs
File metadata and controls
112 lines (96 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System;
using System.Linq;
using System.Reflection;
using IronPython.Runtime.Operations;
namespace IronPython.Runtime {
[PythonType("sys.version_info")]
public class VersionInfo : PythonTuple {
public readonly int major;
public readonly int minor;
public readonly int micro;
public readonly string releaselevel;
public readonly int serial;
private VersionInfo(int major, int minor, int micro, string releaselevel, int serial)
: base(new object[] { major, minor, micro, releaselevel, serial }) {
this.major = major;
this.minor = minor;
this.micro = micro;
this.releaselevel = releaselevel;
this.serial = serial;
}
internal static VersionInfo Instance { get; } = new VersionInfo(CurrentVersion.Major,
CurrentVersion.Minor,
CurrentVersion.Micro,
CurrentVersion.ReleaseLevel,
CurrentVersion.ReleaseSerial);
public override string __repr__(CodeContext context) {
return string.Format("sys.version_info(major={0}, minor={1}, micro={2}, releaselevel='{3}', serial={4})",
major, minor, micro, releaselevel, serial);
}
internal int GetHexVersion() {
int hexlevel = 0;
switch (releaselevel) {
case "alpha":
hexlevel = 0xA;
break;
case "beta":
hexlevel = 0xB;
break;
case "candidate":
hexlevel = 0xC;
break;
case "final":
hexlevel = 0xF;
break;
}
return (major << 24) |
(minor << 16) |
(micro << 8) |
(hexlevel << 4) |
(serial << 0);
}
internal string GetVersionString()
=> CurrentVersion.GetVersionString(major, minor, micro, releaselevel, serial);
}
internal static class CurrentVersion {
public static int Major { get; }
public static int Minor { get; }
public static int Micro { get; }
public static string ReleaseLevel { get; }
public static int ReleaseSerial { get; }
public static string Series { get; }
public static string DisplayName { get; }
public static string GetVersionString(int major, int minor, int micro, string releaselevel, int serial) {
return string.Format("{0}.{1}.{2}{3}{4}",
major,
minor,
micro,
releaselevel != "final" ? GetShortReleaseLevel(releaselevel) : string.Empty,
releaselevel != "final" ? serial.ToString() : string.Empty);
static string GetShortReleaseLevel(string releaselevel) {
switch (releaselevel) {
case "alpha": return "a";
case "beta": return "b";
case "candidate": return "rc";
case "final": return "f";
default: return "";
}
}
}
static CurrentVersion() {
var assembly = typeof(CurrentVersion).Assembly;
var version = new AssemblyName(assembly.FullName).Version; // don't use Assembly.GetName since it fails in partial trust scenarios
Major = version.Major;
Minor = version.Minor;
Micro = version.Build;
Series = version.ToString(2);
var split = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
ReleaseLevel = split[split.Length - 2];
ReleaseSerial = int.Parse(split[split.Length - 1]);
DisplayName = $"IronPython {GetVersionString(Major, Minor, Micro, ReleaseLevel, ReleaseSerial)}";
}
}
}