Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

CP #32786: Implement PEP 421, sys.implementation.

There is currently one difference: CPython's sys.implementation is
writable (you can change attributes and and new ones) while IronPython's
is not.

As a side-effect, the calculation of the sys version attributes is now simplified.
  • Loading branch information...
commit cd28b9ec356021ea0cbece043166438ac9d67d43 1 parent 071f405
@jdhardy authored
View
1  Languages/IronPython/IronPython/IronPython.csproj
@@ -103,6 +103,7 @@
<Compile Include="Runtime\FutureBuiltins.cs" />
<Compile Include="Runtime\Binding\IPythonConvertible.cs" />
<Compile Include="Runtime\IBufferProtocol.cs" />
+ <Compile Include="Runtime\Implementation.cs" />
<Compile Include="Runtime\InstancedModuleDictionaryStorage.cs" />
<Compile Include="Runtime\MemoryView.cs" />
<Compile Include="Runtime\Method.Generated.cs" />
View
2  Languages/IronPython/IronPython/Modules/sys.cs
@@ -273,8 +273,6 @@ internal windows_version(int major, int minor, int build, int platform, string s
public const string platform = "cli";
- // TODO Add a cli_platform attribute
-
public static readonly string prefix = GetPrefix();
// ps1 and ps2 are set by PythonContext and only on the initial load
View
99 Languages/IronPython/IronPython/Runtime/Implementation.cs
@@ -0,0 +1,99 @@
+using System.Linq;
+using IronPython.Runtime.Operations;
+
+namespace IronPython.Runtime {
+ [PythonHidden, PythonType("sys.implementation")]
+ public class Implementation {
+ internal static readonly string _Name = "IronPython";
+ internal static readonly string _name = _Name.ToLowerInvariant();
+ internal static readonly VersionInfo _version = new VersionInfo();
+ internal static readonly int _hexversion = _version.GetHexVersion();
+
+ public readonly string cache_tag = null;
+ public readonly string name = _name;
+ public readonly VersionInfo version = _version;
+ public readonly int hexversion = _hexversion;
+
+ public string __repr__(CodeContext context) {
+ var attrs = from attr in PythonOps.GetAttrNames(context, this)
+ where !attr.ToString().StartsWith("_")
+ select string.Format("{0}={1}",
+ attr,
+ PythonOps.Repr(context, PythonOps.GetBoundAttr(context, this, attr.ToString()))
+ );
+
+ return string.Format("{0}({1})",
+ PythonOps.GetPythonTypeName(this),
+ string.Join(",", attrs.ToArray())
+ );
+ }
+ }
+
+ [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 VersionInfo()
+ : this(CurrentVersion.Major,
+ CurrentVersion.Micro,
+ 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(string _initialVersionString) {
+ var version = string.Format("{0}.{1}.{2}{4}{5} ({3})",
+ major,
+ minor,
+ micro,
+ _initialVersionString,
+ releaselevel != "final" ? CurrentVersion.ShortReleaseLevel : "",
+ releaselevel != "final" ? serial.ToString() : "");
+ return version;
+ }
+ }
+}
View
39 Languages/IronPython/IronPython/Runtime/PythonContext.cs
@@ -1950,44 +1950,17 @@ class AssemblyResolveHolder {
internal void SetHostVariables(PythonDictionary dict) {
dict["executable"] = _initialExecutable;
- SystemState.__dict__["prefix"] = _initialPrefix;
+ dict["prefix"] = _initialPrefix;
dict["exec_prefix"] = _initialPrefix;
SetVersionVariables(dict);
}
- [PythonType("sys.version_info")]
- public class VersionInfo : PythonTuple {
- internal 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;
- }
-
- public readonly int major;
- public readonly int minor;
- public readonly int micro;
- public readonly string releaselevel;
- public readonly int serial;
-
- 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);
- }
- }
-
private void SetVersionVariables(PythonDictionary dict) {
- dict["hexversion"] = (CurrentVersion.Major << 24) + (CurrentVersion.Minor << 16) + (CurrentVersion.Micro << 8);
- dict["version_info"] = new VersionInfo(CurrentVersion.Major, CurrentVersion.Minor, CurrentVersion.Micro, CurrentVersion.ReleaseLevel, CurrentVersion.ReleaseSerial);
- dict["version"] = String.Format("{0}.{1}.{2}{4}{5} ({3})",
- CurrentVersion.Major,
- CurrentVersion.Minor,
- CurrentVersion.Micro,
- _initialVersionString,
- CurrentVersion.ReleaseLevel != "final" ? CurrentVersion.ShortReleaseLevel : "",
- CurrentVersion.ReleaseLevel != "final" ? CurrentVersion.ReleaseSerial.ToString() : "");
+ var implementation = new Implementation();
+ dict["implementation"] = implementation;
+ dict["version_info"] = implementation.version;
+ dict["hexversion"] = implementation.hexversion;
+ dict["version"] = implementation.version.GetVersionString(_initialVersionString);
}
internal static string GetVersionString() {

0 comments on commit cd28b9e

Please sign in to comment.
Something went wrong with that request. Please try again.