Skip to content

Commit 1bc6ed6

Browse files
authored
Misc changes (IronLanguages#1984)
* Misc changes * Disable test_process_time
1 parent cef87c2 commit 1bc6ed6

10 files changed

Lines changed: 116 additions & 9 deletions

File tree

src/core/IronPython.Modules/_winapi.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,16 @@ private static extern bool DuplicateHandlePI(IntPtr hSourceProcessHandle,
408408
public const int WAIT_ABANDONED_0 = 0x80;
409409
public const int WAIT_OBJECT_0 = 0x0;
410410
public const int WAIT_TIMEOUT = 0x102;
411+
public const int ABOVE_NORMAL_PRIORITY_CLASS = 0x8000;
412+
public const int BELOW_NORMAL_PRIORITY_CLASS = 0x4000;
413+
public const int HIGH_PRIORITY_CLASS = 0x80;
414+
public const int IDLE_PRIORITY_CLASS = 0x40;
415+
public const int NORMAL_PRIORITY_CLASS = 0x20;
416+
public const int REALTIME_PRIORITY_CLASS = 0x100;
417+
public const int CREATE_NO_WINDOW = 0x8000000;
418+
public const int DETACHED_PROCESS = 8;
419+
public const int CREATE_DEFAULT_ERROR_MODE = 0x4000000;
420+
public const int CREATE_BREAKAWAY_FROM_JOB = 0x1000000;
411421

412422
#endregion
413423
}

src/core/IronPython.Modules/nt.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,6 +2185,17 @@ private static Exception ToPythonException(Exception e, string? filename = null)
21852185
public const int W_OK = 2;
21862186
public const int R_OK = 4;
21872187

2188+
[PythonHidden(PlatformsAttribute.PlatformFamily.Unix)]
2189+
public const int _LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR = 0x0100;
2190+
[PythonHidden(PlatformsAttribute.PlatformFamily.Unix)]
2191+
public const int _LOAD_LIBRARY_SEARCH_DEFAULT_DIRS = 0x1000;
2192+
[PythonHidden(PlatformsAttribute.PlatformFamily.Unix)]
2193+
public const int _LOAD_LIBRARY_SEARCH_APPLICATION_DIR = 0x0200;
2194+
[PythonHidden(PlatformsAttribute.PlatformFamily.Unix)]
2195+
public const int _LOAD_LIBRARY_SEARCH_USER_DIRS = 0x0400;
2196+
[PythonHidden(PlatformsAttribute.PlatformFamily.Unix)]
2197+
public const int _LOAD_LIBRARY_SEARCH_SYSTEM32 = 0x0800;
2198+
21882199
private static void addBase(IEnumerable<string> files, PythonList ret) {
21892200
foreach (string file in files) {
21902201
ret.AddNoLock(Path.GetFileName(file));

src/core/IronPython/Compiler/Ast/TupleExpression.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public override void Walk(PythonWalker walker) {
7373
walker.PostWalk(this);
7474
}
7575

76+
public override string NodeName => "tuple";
77+
7678
public bool IsExpandable { get; }
7779

7880
internal override bool IsConstant {

src/core/IronPython/Modules/_io.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2912,6 +2912,9 @@ public static _IOBase open(
29122912
return res;
29132913
}
29142914

2915+
// new in Python 3.8
2916+
public static _IOBase open_code(CodeContext/*!*/ context, string path) => open(context, path, "rb");
2917+
29152918
internal static TextIOWrapper CreateConsole(PythonContext context, SharedIO io, ConsoleStreamType type, string name, out StreamBox sio) {
29162919
var cc = context.SharedContext;
29172920
if (type == ConsoleStreamType.Input) {

src/core/IronPython/Runtime/ByteArray.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,18 @@ public static object fromhex(CodeContext context, [NotNone] PythonType cls, [Not
483483

484484
public string hex() => Bytes.ToHex(_bytes.AsByteSpan()); // new in CPython 3.5
485485

486+
// new in CPython 3.8
487+
public string hex([NotNone] string sep, int bytes_per_sep = 1) {
488+
if (sep.Length != 1) throw PythonOps.ValueError($"{nameof(sep)} must be length 1");
489+
return Bytes.ToHex(_bytes.AsByteSpan(), sep[0], bytes_per_sep);
490+
}
491+
492+
// new in CPython 3.8
493+
public string hex([BytesLike, NotNone] IList<byte> sep, int bytes_per_sep = 1) {
494+
if (sep.Count != 1) throw PythonOps.ValueError($"{nameof(sep)} must be length 1");
495+
return Bytes.ToHex(_bytes.AsByteSpan(), (char)sep[0], bytes_per_sep);
496+
}
497+
486498
public int index([BytesLike, NotNone] IList<byte> sub) {
487499
lock (this) {
488500
return index(sub, 0, _bytes.Count);
@@ -544,6 +556,13 @@ public bool isalpha() {
544556
}
545557
}
546558

559+
// new in Python 3.7
560+
public bool isascii() {
561+
lock (this) {
562+
return _bytes.IsAscii();
563+
}
564+
}
565+
547566
public bool isdigit() {
548567
lock (this) {
549568
return _bytes.IsDigit();

src/core/IronPython/Runtime/Bytes.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,51 @@ static char ToAscii(int b) {
392392
}
393393
}
394394

395+
// new in CPython 3.8
396+
public string hex([NotNone] string sep, int bytes_per_sep = 1) {
397+
if (sep.Length != 1) throw PythonOps.ValueError($"{nameof(sep)} must be length 1");
398+
return ToHex(_bytes.AsSpan(), sep[0], bytes_per_sep);
399+
}
400+
401+
// new in CPython 3.8
402+
public string hex([BytesLike, NotNone] IList<byte> sep, int bytes_per_sep = 1) {
403+
if (sep.Count != 1) throw PythonOps.ValueError($"{nameof(sep)} must be length 1");
404+
return ToHex(_bytes.AsSpan(), (char)sep[0], bytes_per_sep);
405+
}
406+
407+
internal static string ToHex(ReadOnlySpan<byte> bytes, char sep, int bytes_per_sep) {
408+
if (sep >= 0x80) throw PythonOps.ValueError($"{nameof(sep)} must be ASCII");
409+
if (bytes.Length == 0) return string.Empty;
410+
if (bytes_per_sep == 0) return ToHex(bytes);
411+
412+
int sepLoc;
413+
if (bytes_per_sep < 0) {
414+
bytes_per_sep = -bytes_per_sep;
415+
sepLoc = 0;
416+
} else {
417+
sepLoc = bytes_per_sep - bytes.Length % bytes_per_sep;
418+
if (sepLoc == bytes_per_sep) sepLoc = 0;
419+
}
420+
421+
var builder = new StringBuilder(bytes.Length * 2 + (bytes.Length - 1) / bytes_per_sep);
422+
foreach (var b in bytes) {
423+
if (sepLoc == bytes_per_sep) {
424+
builder.Append(sep);
425+
sepLoc = 1;
426+
} else {
427+
sepLoc++;
428+
}
429+
builder.Append(ToAscii(b >> 4));
430+
builder.Append(ToAscii(b & 0xf));
431+
}
432+
Debug.Assert(builder.Length == bytes.Length * 2 + (bytes.Length - 1) / bytes_per_sep);
433+
return builder.ToString();
434+
435+
static char ToAscii(int b) {
436+
return (char)(b < 10 ? '0' + b : 'a' + (b - 10));
437+
}
438+
}
439+
395440
public int index([BytesLike, NotNone] IList<byte> sub)
396441
=> index(sub, 0, _bytes.Length);
397442

@@ -435,6 +480,9 @@ public int index(BigInteger @byte, object? start, object? end)
435480

436481
public bool isalpha() => _bytes.IsLetter();
437482

483+
// new in Python 3.7
484+
public bool isascii() => _bytes.IsAscii();
485+
438486
public bool isdigit() => _bytes.IsDigit();
439487

440488
public bool islower() => _bytes.IsLower();

src/core/IronPython/Runtime/Operations/IListOfByteOps.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,15 @@ internal static bool IsAlphaNumeric(this IList<byte> bytes) {
984984
return true;
985985
}
986986

987+
internal static bool IsAscii(this IList<byte> bytes) {
988+
foreach (byte b in bytes) {
989+
if (b > 0x7f) {
990+
return false;
991+
}
992+
}
993+
return true;
994+
}
995+
987996
internal static int Find(this IList<byte> bytes, IList<byte> sub, int start, int end) {
988997
if (!PythonOps.TryFixSubsequenceIndices(bytes.Count, ref start, ref end)) {
989998
return -1;
@@ -1017,7 +1026,7 @@ internal static List<byte> FromHex(string @string) {
10171026
iVal = (c - 'A' + 10) * 16;
10181027
} else if (c >= 'a' && c <= 'f') {
10191028
iVal = (c - 'a' + 10) * 16;
1020-
} else if (c == ' ') {
1029+
} else if (c < 0x80 && char.IsWhiteSpace(c)) {
10211030
continue;
10221031
} else {
10231032
throw PythonOps.ValueError("non-hexadecimal number found in fromhex() arg at position {0}", i);

src/core/IronPython/Runtime/Operations/StringOps.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,13 @@ public static bool isalpha([NotNone] this string self) {
623623
return true;
624624
}
625625

626+
public static bool isascii([NotNone] this string self) {
627+
foreach (char c in self) {
628+
if (c > 0x7f) return false;
629+
}
630+
return true;
631+
}
632+
626633
public static bool isdigit([NotNone] this string self) {
627634
if (self.Length == 0) return false;
628635
string v = self;

tests/suite/test_dict.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
x = dir(dict)
88
x = dir(dict.fromkeys)
99

10-
import collections
10+
import collections.abc
1111
import os
1212
import sys
1313

@@ -624,7 +624,7 @@ def test_same_but_different(self):
624624
def test_module_dict(self):
625625
me = sys.modules[__name__]
626626
moduleDict = me.__dict__
627-
self.assertTrue(isinstance(moduleDict, collections.Mapping))
627+
self.assertTrue(isinstance(moduleDict, collections.abc.Mapping))
628628
self.assertTrue(moduleDict.__contains__("DictTest"))
629629
self.assertEqual(moduleDict["DictTest"], DictTest)
630630
self.assertTrue(moduleDict.keys().__contains__("DictTest"))

tests/suite/test_time_stdlib.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@ def load_tests(loader, standard_tests, pattern):
2525
test.test_time.TimeTestCase('test_default_values_for_zero'), # AssertionError: '2000 01 01 00 00 00 1 001' != '2000 01 01 00 00 00 6 001'
2626
]
2727

28-
if is_netcoreapp21 and is_osx:
29-
failing_tests += [
30-
test.test_time.TimeTestCase('test_process_time'), # AssertionError
31-
]
32-
33-
skip_tests = []
28+
# frequently fails during CI
29+
skip_tests = [
30+
test.test_time.TimeTestCase('test_process_time'),
31+
]
3432

3533
return generate_suite(tests, failing_tests, skip_tests)
3634

0 commit comments

Comments
 (0)