Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/core/IronPython.Modules/_socket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public void close() {
var refs = System.Threading.Interlocked.Decrement(ref _referenceCount);

// Don't actually close the socket if other file objects are
// still referring to this socket.
// still referring to this socket.
if (refs < 1) {
_close();
}
Expand Down Expand Up @@ -329,19 +329,22 @@ private bool TryConnect(IPEndPoint remoteEP) {
return true;
}

public long detach() {
public object detach() {
var fd = fileno();
_socket = null!; // TODO: this is wrong, no null checks on _socket anywhere!
return fd;
}

[Documentation("fileno() -> file_handle\n\n"
+ "Return the underlying system handle for this socket (a 64-bit integer)."
+ "Return the underlying system handle for this socket."
)]
public long fileno() {
public object fileno() {
if (_closed) return -1;
try {
return _socket.Handle.ToInt64();
var handle = _socket.Handle.ToInt64();
if (int.MinValue <= handle && handle <= int.MaxValue)
return (int)handle;
return (BigInteger)handle;
} catch (Exception e) {
throw MakeException(_context, e);
}
Expand Down Expand Up @@ -1737,7 +1740,7 @@ public static void setdefaulttimeout(CodeContext/*!*/ context, object? timeout)
public const int RCVALL_SOCKETLEVELONLY = 2;
public const int RCVALL_MAX = 3;

public const int has_ipv6 = (int)1;
public const bool has_ipv6 = true;

#endregion

Expand Down Expand Up @@ -2014,7 +2017,7 @@ private static string EncodeHostName(string host) {
/// <summary>
/// Return the IP address associated with host, with optional address family checking.
/// host may be either a name or an IP address (in string form).
///
///
/// If family is non-null, a gaierror will be thrown if the host's address family is
/// not the same as the specified family. gaierror is also raised if the hostname cannot be
/// converted to an IP address (e.g. through a name lookup failure).
Expand All @@ -2026,7 +2029,7 @@ private static IPAddress HostToAddress(CodeContext/*!*/ context, string host, Ad
/// <summary>
/// Return the IP address associated with host, with optional address family checking.
/// host may be either a name or an IP address (in string form).
///
///
/// If family is non-null, a gaierror will be thrown if the host's address family is
/// not the same as the specified family. gaierror is also raised if the hostname cannot be
/// converted to an IP address (e.g. through a name lookup failure).
Expand Down Expand Up @@ -2086,7 +2089,7 @@ private static string RemoveLocalDomain(string fqdn) {
/// <summary>
/// Convert a (host, port) tuple [IPv4] (host, port, flowinfo, scopeid) tuple [IPv6]
/// to its corresponding IPEndPoint.
///
///
/// Throws gaierror if host is not a valid address.
/// Throws ArgumentTypeException if any of the following are true:
/// - address does not have exactly two elements
Expand Down Expand Up @@ -2155,7 +2158,7 @@ private static PythonTuple EndPointToTuple(IPEndPoint endPoint) {
case AddressFamily.InterNetwork:
return PythonTuple.MakeTuple(ip, port);
case AddressFamily.InterNetworkV6:
long flowInfo = 0; // RFC 3493 p. 7
long flowInfo = 0; // RFC 3493 p. 7
long scopeId = endPoint.Address.ScopeId;
return PythonTuple.MakeTuple(ip, port, flowInfo, scopeId);
default:
Expand Down
2 changes: 1 addition & 1 deletion src/core/IronPython/Compiler/Ast/FlowChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public override bool Walk(ListExpression node) {

public override bool Walk(Parameter node) {
_fc.Define(node.Name);
return true;
return false;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/core/IronPython/Compiler/Ast/SetExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class SetExpression : Expression {

public SetExpression(params Expression[] items) {
ContractUtils.RequiresNotNull(items, nameof(items));
ContractUtils.RequiresNotNullItems(items, nameof(items));

_items = items;
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/IronPython/Compiler/Ast/StarredExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public override void Walk(PythonWalker walker) {
}

internal class StarredExpressionChecker : PythonWalker {
private readonly CompilerContext context;
private readonly CompilerContext _context;

private StarredExpressionChecker(CompilerContext context) {
this.context = context;
_context = context;
}

public static void Check(PythonAst ast, CompilerContext context) {
Expand Down Expand Up @@ -122,7 +122,7 @@ public override bool Walk(StarredExpression node) {
public override bool Walk(TupleExpression node) => WalkItems(node.Items);

private void ReportSyntaxError(string message, Node node) {
context.Errors.Add(context.SourceUnit, message, node.Span, ErrorCodes.SyntaxError, Severity.FatalError);
_context.Errors.Add(_context.SourceUnit, message, node.Span, ErrorCodes.SyntaxError, Severity.FatalError);
}

private void WalkAssignmentTarget(Expression expr) {
Expand Down
16 changes: 8 additions & 8 deletions src/core/IronPython/Compiler/Ast/YieldFromExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ namespace IronPython.Compiler.Ast {
using Ast = MSAst.Expression;

public class YieldFromExpression : Expression {
private readonly Statement statement;
private readonly NameExpression result;
private readonly Statement _statement;
private readonly NameExpression _result;

public YieldFromExpression(Expression expression) {
statement = GenYieldFromStatement(expression);
result = new NameExpression("__yieldfromprefix_r");
_statement = GenYieldFromStatement(expression);
_result = new NameExpression("__yieldfromprefix_r");

Expression = expression;
}
Expand Down Expand Up @@ -83,16 +83,16 @@ private static Statement GenYieldFromStatement(Expression expression) {
public override MSAst.Expression Reduce() {
return Ast.Block(
typeof(object),
statement,
AstUtils.Convert(result, typeof(object))
_statement,
AstUtils.Convert(_result, typeof(object))
).Reduce();
}

public override void Walk(PythonWalker walker) {
if (walker.Walk(this)) {
// Expression is already walked as part of statement so skip it
statement.Walk(walker);
result.Walk(walker);
_statement.Walk(walker);
_result.Walk(walker);
}
walker.PostWalk(this);
}
Expand Down
10 changes: 8 additions & 2 deletions src/core/IronPython/Modules/_ast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,11 @@ internal BoolOp(OrExpression or)
}

internal override AstExpression Revert() {
if (values.Count < 2) throw PythonOps.ValueError($"{nameof(BoolOp)} with less than 2 values");
foreach (var val in values) {
if (val is null) throw PythonOps.ValueError("None disallowed in expression list");
}

if (op == And.Instance || op is And) {
AndExpression ae = new AndExpression(
expr.Revert(values[0]),
Expand Down Expand Up @@ -2564,8 +2569,9 @@ internal Set(SetExpression setExpression)
internal override AstExpression Revert() {
AstExpression[] e = new AstExpression[elts.Count];
int i = 0;
foreach (expr el in elts)
e[i++] = expr.Revert(el);
foreach (expr el in elts) {
e[i++] = expr.Revert(el) ?? throw PythonOps.ValueError("None disallowed in expression list");
}
return new SetExpression(e);
}

Expand Down
61 changes: 60 additions & 1 deletion src/core/IronPython/Modules/sys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public static int getsizeof(object o) {
public static PythonTuple getwindowsversion()
=> windows_version.GetWindowsVersion();

[PythonType("sys.getwindowsversion"), SupportedOSPlatform("windows"), PythonHidden]
[PythonType("getwindowsversion"), SupportedOSPlatform("windows"), PythonHidden]
public sealed class windows_version : PythonTuple {
internal static windows_version GetWindowsVersion() {
unsafe {
Expand Down Expand Up @@ -591,6 +591,65 @@ public override string __repr__(CodeContext context) {
}
}

[PythonType("version_info"), PythonHidden]
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);
}

[SpecialName]
public static void PerformModuleReload(PythonContext/*!*/ context, PythonDictionary/*!*/ dict) {
dict["stdin"] = dict["__stdin__"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,6 @@
using System.Reflection;

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; }
Expand Down
8 changes: 4 additions & 4 deletions src/core/IronPython/Runtime/PythonContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1881,9 +1881,9 @@ internal void SetHostVariables(PythonDictionary dict) {
}

private void SetVersionVariables(PythonDictionary dict) {
dict["implementation"] = new SimpleNamespace(new Dictionary<string, object>() { { "cache_tag", $"ironpython-{VersionInfo.Instance.major}{VersionInfo.Instance.minor}" }, { "name", "ironpython" }, { "version", VersionInfo.Instance }, { "hexversion", VersionInfo.Instance.GetHexVersion() } });
dict["version_info"] = VersionInfo.Instance;
dict["hexversion"] = VersionInfo.Instance.GetHexVersion();
dict["implementation"] = new SimpleNamespace(new Dictionary<string, object>() { { "cache_tag", $"ironpython-{SysModule.VersionInfo.Instance.major}{SysModule.VersionInfo.Instance.minor}" }, { "name", "ironpython" }, { "version", SysModule.VersionInfo.Instance }, { "hexversion", SysModule.VersionInfo.Instance.GetHexVersion() } });
dict["version_info"] = SysModule.VersionInfo.Instance;
dict["hexversion"] = SysModule.VersionInfo.Instance.GetHexVersion();
dict["version"] = _initialVersionString ?? GetVersionString();
}

Expand All @@ -1903,7 +1903,7 @@ internal static string GetVersionString() {
string configuration = Runtime.ClrModule.IsDebug ? " DEBUG" : string.Empty;
string bitness = (IntPtr.Size * 8).ToString();

return $"{VersionInfo.Instance.GetVersionString()}{configuration} ({Runtime.ClrModule.FileVersion})\n" +
return $"{SysModule.VersionInfo.Instance.GetVersionString()}{configuration} ({Runtime.ClrModule.FileVersion})\n" +
$"[{Runtime.ClrModule.TargetFramework} on {Runtime.ClrModule.FrameworkDescription} ({bitness}-bit)]";
}

Expand Down
Loading
Loading