Skip to content

Commit fadeb23

Browse files
authored
Misc changes (#1435)
* Misc changes * Add ModuleNotFoundError * Fix failing test * Update exception_hierarchy.txt * Add winreg.REG_QWORD
1 parent 4ddcf30 commit fadeb23

11 files changed

Lines changed: 153 additions & 60 deletions

File tree

Src/IronPython.Modules/math.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ namespace IronPython.Modules {
2121
public static partial class PythonMath {
2222
public const string __doc__ = "Provides common mathematical functions.";
2323

24+
#if NET
25+
public const double tau = Math.Tau;
26+
#else
27+
public const double tau = 2 * Math.PI;
28+
#endif
2429
public const double pi = Math.PI;
2530
public const double e = Math.E;
2631

Src/IronPython.Modules/nt.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -438,22 +438,8 @@ public static void _exit(CodeContext/*!*/ context, int status) {
438438
context.LanguageContext.DomainManager.Platform.TerminateScriptExecution(status);
439439
}
440440

441-
public static object fspath(CodeContext context, [AllowNull] object path) {
442-
if (path is string) return path;
443-
if (path is Extensible<string>) return path;
444-
if (path is Bytes) return path;
445-
446-
if (PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, path, "__fspath__", out object res)) {
447-
return res switch {
448-
string => res,
449-
Extensible<string> => res,
450-
Bytes => res,
451-
_ => throw PythonOps.TypeError("expected {0}.__fspath__() to return str or bytes, not {0}", PythonOps.GetPythonTypeName(path), PythonOps.GetPythonTypeName(res))
452-
};
453-
}
454-
455-
throw PythonOps.TypeError("expected str, bytes or os.PathLike object, not {0}", PythonOps.GetPythonTypeName(path));
456-
}
441+
public static object fspath(CodeContext context, [AllowNull] object path)
442+
=> PythonOps.FsPath(path);
457443

458444
[LightThrowing]
459445
public static object fstat(CodeContext/*!*/ context, int fd) {

Src/IronPython.Modules/winreg.cs

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static class PythonWinReg {
3030

3131
public static PythonType error = PythonExceptions.OSError;
3232

33-
#region Constants
33+
#region Constants
3434

3535
public static BigInteger HKEY_CLASSES_ROOT = 0x80000000L;
3636
public static BigInteger HKEY_CURRENT_USER = 0x80000001L;
@@ -69,6 +69,7 @@ public static class PythonWinReg {
6969
public const int REG_RESOURCE_LIST = 0X8;
7070
public const int REG_FULL_RESOURCE_DESCRIPTOR = 0X9;
7171
public const int REG_RESOURCE_REQUIREMENTS_LIST = 0XA;
72+
public const int REG_QWORD = 0xB;
7273

7374
public const int REG_NOTIFY_CHANGE_NAME = 0X1;
7475
public const int REG_NOTIFY_CHANGE_ATTRIBUTES = 0X2;
@@ -88,9 +89,9 @@ public static class PythonWinReg {
8889
public const int REG_LEGAL_OPTION = 0XF;
8990
public const int REG_WHOLE_HIVE_VOLATILE = 0X1;
9091

91-
#endregion
92+
#endregion
9293

93-
#region Module Methods
94+
#region Module Methods
9495

9596
public static void CloseKey(HKEYType key) {
9697
key.Close();
@@ -106,7 +107,7 @@ public static HKEYType CreateKey(object key, string sub_key) {
106107
//if key is a system key and no subkey is specified return that.
107108
if (key is BigInteger && string.IsNullOrEmpty(sub_key))
108109
return rootKey;
109-
110+
110111
HKEYType subKey = new HKEYType(rootKey.GetKey().CreateSubKey(sub_key));
111112
return subKey;
112113
}
@@ -124,7 +125,7 @@ public static HKEYType CreateKeyEx(object key, string sub_key, int reserved = 0,
124125

125126
SafeRegistryHandle handle;
126127
int disposition;
127-
128+
128129
int result = RegCreateKeyEx(
129130
rootKey.GetKey().Handle,
130131
sub_key,
@@ -233,7 +234,7 @@ public static void DeleteKey(object key, string sub_key) {
233234
}
234235
}
235236

236-
public static void DeleteKeyEx(object key, string sub_key, int access=KEY_WOW64_64KEY, int reserved=0) {
237+
public static void DeleteKeyEx(object key, string sub_key, int access = KEY_WOW64_64KEY, int reserved = 0) {
237238
HKEYType rootKey = GetRootKey(key);
238239

239240
if (key is BigInteger && string.IsNullOrEmpty(sub_key))
@@ -365,10 +366,9 @@ private static void QueryValueExImpl(SafeRegistryHandle handle, string valueName
365366

366367
}
367368

368-
public static string ExpandEnvironmentStrings(string value)
369-
{
369+
public static string ExpandEnvironmentStrings(string value) {
370370
if (value == null)
371-
throw PythonExceptions.CreateThrowable(PythonExceptions.TypeError, "must be unicode, not None");
371+
throw PythonExceptions.CreateThrowable(PythonExceptions.TypeError, "must be unicode, not None");
372372

373373
return Environment.ExpandEnvironmentVariables(value);
374374
}
@@ -379,7 +379,7 @@ private static string ExtractString(byte[] data, int start, int end) {
379379
}
380380
char[] chars = new char[(end - start) / 2];
381381
for (int i = 0; i < chars.Length; i++) {
382-
chars[i] = (char)((data[i*2 + start]) | (data[i*2 + start + 1] << 8));
382+
chars[i] = (char)((data[i * 2 + start]) | (data[i * 2 + start + 1] << 8));
383383
}
384384
return new string(chars);
385385
}
@@ -393,7 +393,7 @@ public static HKEYType OpenKey(object key, string sub_key) {
393393
return OpenKey(key, sub_key, 0, KEY_READ);
394394
}
395395

396-
public static HKEYType OpenKey(object key, string sub_key, int reserved=0, int access=KEY_READ) {
396+
public static HKEYType OpenKey(object key, string sub_key, int reserved = 0, int access = KEY_READ) {
397397
HKEYType rootKey = GetRootKey(key);
398398
RegistryKey newKey = null;
399399

@@ -409,19 +409,19 @@ public static HKEYType OpenKey(object key, string sub_key, int reserved=0, int a
409409
try {
410410
if ((access & KEY_SET_VALUE) == KEY_SET_VALUE ||
411411
(access & KEY_CREATE_SUB_KEY) == KEY_CREATE_SUB_KEY) {
412-
if (reserved != 0) {
413-
newKey = nativeRootKey.OpenSubKey(sub_key, RegistryKeyPermissionCheck.Default, (RegistryRights)reserved);
414-
} else {
415-
newKey = nativeRootKey.OpenSubKey(sub_key, true);
416-
}
412+
if (reserved != 0) {
413+
newKey = nativeRootKey.OpenSubKey(sub_key, RegistryKeyPermissionCheck.Default, (RegistryRights)reserved);
414+
} else {
415+
newKey = nativeRootKey.OpenSubKey(sub_key, true);
416+
}
417417
} else if ((access & KEY_QUERY_VALUE) == KEY_QUERY_VALUE ||
418418
(access & KEY_ENUMERATE_SUB_KEYS) == KEY_ENUMERATE_SUB_KEYS ||
419419
(access & KEY_NOTIFY) == KEY_NOTIFY) {
420-
if (reserved != 0) {
421-
newKey = nativeRootKey.OpenSubKey(sub_key, RegistryKeyPermissionCheck.ReadSubTree, (RegistryRights)reserved);
422-
} else {
423-
newKey = nativeRootKey.OpenSubKey(sub_key, false);
424-
}
420+
if (reserved != 0) {
421+
newKey = nativeRootKey.OpenSubKey(sub_key, RegistryKeyPermissionCheck.ReadSubTree, (RegistryRights)reserved);
422+
} else {
423+
newKey = nativeRootKey.OpenSubKey(sub_key, false);
424+
}
425425
} else {
426426
throw new Win32Exception("Unexpected mode");
427427
}
@@ -437,7 +437,7 @@ public static HKEYType OpenKey(object key, string sub_key, int reserved=0, int a
437437
return new HKEYType(newKey);
438438
}
439439

440-
public static HKEYType OpenKeyEx(object key, string sub_key, int reserved=0, int access=KEY_READ) {
440+
public static HKEYType OpenKeyEx(object key, string sub_key, int reserved = 0, int access = KEY_READ) {
441441
return OpenKey(key, sub_key, reserved, access);
442442
}
443443

@@ -540,12 +540,10 @@ public static HKEYType ConnectRegistry(string computer_name, BigInteger key) {
540540
try {
541541
if (computer_name == string.Empty) {
542542
newKey = RegistryKey.OpenBaseKey(MapSystemKey(key), RegistryView.Default);
543-
}
544-
else {
543+
} else {
545544
newKey = RegistryKey.OpenRemoteBaseKey(MapSystemKey(key), computer_name);
546545
}
547-
}
548-
catch (IOException ioe) {
546+
} catch (IOException ioe) {
549547
throw PythonExceptions.CreateThrowable(PythonExceptions.OSError, PythonExceptions._OSError.ERROR_BAD_NETPATH, ioe.Message, null, PythonExceptions._OSError.ERROR_BAD_NETPATH);
550548
} catch (Exception e) {
551549
throw new ExternalException(e.Message);
@@ -583,7 +581,7 @@ public static bool QueryReflectionKey(object key) {
583581
HKEYType rootKey = GetRootKey(key);
584582
bool isDisabled;
585583

586-
if(!Environment.Is64BitOperatingSystem) {
584+
if (!Environment.Is64BitOperatingSystem) {
587585
throw new NotImplementedException("not implemented on this platform");
588586
}
589587

@@ -594,9 +592,9 @@ public static bool QueryReflectionKey(object key) {
594592
return isDisabled;
595593
}
596594

597-
#endregion
595+
#endregion
598596

599-
#region Helpers
597+
#region Helpers
600598
private static HKEYType GetRootKey(object key) {
601599
HKEYType rootKey;
602600
rootKey = key as HKEYType;
@@ -630,7 +628,7 @@ private static RegistryHive MapSystemKey(BigInteger hKey) {
630628
throw new ValueErrorException("Unknown system key");
631629
}
632630

633-
#endregion
631+
#endregion
634632

635633

636634
[PythonType]
@@ -694,13 +692,13 @@ public RegistryKey GetKey() {
694692
}
695693
}
696694

697-
#region IDisposable Members
695+
#region IDisposable Members
698696

699697
void IDisposable.Dispose() {
700698
Close();
701699
}
702700

703-
#endregion
701+
#endregion
704702
}
705703
}
706704
}

Src/IronPython/Modules/Builtin.Generated.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public static partial class Builtin {
2929
public static PythonType BufferError => PythonExceptions.BufferError;
3030
public static PythonType EOFError => PythonExceptions.EOFError;
3131
public static PythonType ImportError => PythonExceptions.ImportError;
32+
public static PythonType ModuleNotFoundError => PythonExceptions.ModuleNotFoundError;
3233
public static PythonType LookupError => PythonExceptions.LookupError;
3334
public static PythonType IndexError => PythonExceptions.IndexError;
3435
public static PythonType KeyError => PythonExceptions.KeyError;

Src/IronPython/Modules/_io.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2791,16 +2791,16 @@ public static _IOBase open(
27912791
bool closefd=true,
27922792
object opener=null
27932793
) {
2794-
int fd = -1;
2795-
string fname = file as string;
2796-
if (fname == null) {
2797-
if (file is Extensible<string>) {
2798-
fname = ((Extensible<string>)file).Value;
2799-
} else if (file is Bytes b) {
2800-
fname = b.decode(context, SysModule.getfilesystemencoding(), "strict");
2801-
} else {
2802-
fd = GetInt(file, 0);
2803-
}
2794+
string fname = null;
2795+
if (!Converter.TryConvertToIndex(file, out int fd, false, false)) {
2796+
fd = -1;
2797+
var path = PythonOps.FsPath(file);
2798+
fname = path switch {
2799+
string s => s,
2800+
Extensible<string> es => es,
2801+
Bytes b => b.decode(context, SysModule.getfilesystemencoding(), SysModule.getfilesystemencodeerrors()),
2802+
_ => throw new InvalidOperationException(),
2803+
};
28042804
}
28052805

28062806
HashSet<char> modes = MakeSet(mode);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information.
4+
5+
#nullable enable
6+
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Runtime.Serialization;
10+
11+
using Microsoft.Scripting.Runtime;
12+
13+
namespace IronPython.Runtime.Exceptions {
14+
#region Generated ModuleNotFoundException
15+
16+
// *** BEGIN GENERATED CODE ***
17+
// generated by function: gen_one_exception_specialized from: generate_exceptions.py
18+
19+
[Serializable]
20+
public class ModuleNotFoundException : ImportException, IPythonAwareException {
21+
private PythonExceptions.BaseException? _pyExceptionObject;
22+
private List<DynamicStackFrame>? _frames;
23+
private TraceBack? _traceback;
24+
25+
public ModuleNotFoundException() : base() { }
26+
public ModuleNotFoundException(string msg) : base(msg) { }
27+
public ModuleNotFoundException(string message, Exception innerException)
28+
: base(message, innerException) {
29+
}
30+
#if FEATURE_SERIALIZATION
31+
protected ModuleNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { }
32+
33+
public override void GetObjectData(SerializationInfo info, StreamingContext context) {
34+
info.AddValue("frames", _frames);
35+
info.AddValue("traceback", _traceback);
36+
base.GetObjectData(info, context);
37+
}
38+
#endif
39+
40+
PythonExceptions.BaseException? IPythonAwareException.PythonException {
41+
get {
42+
if (_pyExceptionObject == null) {
43+
var newEx = new PythonExceptions._ImportError(PythonExceptions.ModuleNotFoundError);
44+
newEx.InitializeFromClr(this);
45+
_pyExceptionObject = newEx;
46+
}
47+
return _pyExceptionObject;
48+
}
49+
set { _pyExceptionObject = value; }
50+
}
51+
52+
List<DynamicStackFrame>? IPythonAwareException.Frames {
53+
get { return _frames; }
54+
set { _frames = value; }
55+
}
56+
57+
TraceBack? IPythonAwareException.TraceBack {
58+
get { return _traceback; }
59+
set { _traceback = value; }
60+
}
61+
}
62+
63+
// *** END GENERATED CODE ***
64+
65+
#endregion
66+
}

Src/IronPython/Runtime/Exceptions/PythonExceptions.Generated.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,17 @@ public _ImportError(PythonType type) : base(type) { }
223223
public object path { get; set; }
224224
}
225225

226+
[MultiRuntimeAware]
227+
private static PythonType ModuleNotFoundErrorStorage;
228+
public static PythonType ModuleNotFoundError {
229+
get {
230+
if (ModuleNotFoundErrorStorage == null) {
231+
Interlocked.CompareExchange(ref ModuleNotFoundErrorStorage, CreateSubType(ImportError, "ModuleNotFoundError", (msg, innerException) => new ModuleNotFoundException(msg, innerException)), null);
232+
}
233+
return ModuleNotFoundErrorStorage;
234+
}
235+
}
236+
226237
[MultiRuntimeAware]
227238
private static PythonType LookupErrorStorage;
228239
public static PythonType LookupError {
@@ -898,6 +909,7 @@ public static PythonType ResourceWarning {
898909
if (clrException is InterruptedException) return new PythonExceptions._OSError(PythonExceptions.InterruptedError);
899910
if (clrException is IsADirectoryException) return new PythonExceptions._OSError(PythonExceptions.IsADirectoryError);
900911
if (clrException is KeyNotFoundException) return new PythonExceptions.BaseException(PythonExceptions.KeyError);
912+
if (clrException is ModuleNotFoundException) return new PythonExceptions._ImportError(PythonExceptions.ModuleNotFoundError);
901913
if (clrException is NotADirectoryException) return new PythonExceptions._OSError(PythonExceptions.NotADirectoryError);
902914
if (clrException is NotImplementedException) return new PythonExceptions.BaseException(PythonExceptions.NotImplementedError);
903915
if (clrException is OutOfMemoryException) return new PythonExceptions.BaseException(PythonExceptions.MemoryError);

Src/IronPython/Runtime/Operations/PythonOps.Generated.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ public static Exception StopAsyncIteration(string format, params object[] args)
155155
return new StopAsyncIterationException(string.Format(format, args));
156156
}
157157

158+
public static Exception ModuleNotFoundError(string format, params object[] args) {
159+
return new ModuleNotFoundException(string.Format(format, args));
160+
}
161+
158162
// *** END GENERATED CODE ***
159163

160164
#endregion

Src/IronPython/Runtime/Operations/PythonOps.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,23 @@ public static string FormatString(CodeContext/*!*/ context, string str, object d
321321
return StringFormatter.Format(context, str, data);
322322
}
323323

324+
internal static object FsPath(object? path) {
325+
if (path is string) return path;
326+
if (path is Extensible<string>) return path;
327+
if (path is Bytes) return path;
328+
329+
if (PythonTypeOps.TryInvokeUnaryOperator(DefaultContext.Default, path, "__fspath__", out object res)) {
330+
return res switch {
331+
string => res,
332+
Extensible<string> => res,
333+
Bytes => res,
334+
_ => throw PythonOps.TypeError("expected {0}.__fspath__() to return str or bytes, not {0}", PythonOps.GetPythonTypeName(path), PythonOps.GetPythonTypeName(res))
335+
};
336+
}
337+
338+
throw PythonOps.TypeError("expected str, bytes or os.PathLike object, not {0}", PythonOps.GetPythonTypeName(path));
339+
}
340+
324341
public static object Plus(object? o) {
325342
if (o is int) return o;
326343
else if (o is double) return o;

0 commit comments

Comments
 (0)