Skip to content

Commit 8241bd0

Browse files
authored
Misc changes (#1176)
1 parent 2596273 commit 8241bd0

File tree

6 files changed

+70
-25
lines changed

6 files changed

+70
-25
lines changed
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
using System;
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;
28
using System.Runtime.InteropServices;
39

410
using IronPython.Runtime;
@@ -8,11 +14,17 @@ namespace IronPython.Modules {
814
public static class PythonFaultHandler {
915
private const int STDERR = 2;
1016

11-
public static void dump_traceback(CodeContext context, [DefaultParameterValue(STDERR)]object file, bool all_threads=true) {
17+
public static void dump_traceback(CodeContext context, [DefaultParameterValue(STDERR)] object? file, bool all_threads = true) {
1218
// TODO: the default file object should be sys.stderr
1319

1420
// TODO: fill this up
1521
throw new NotImplementedException();
16-
}
22+
}
23+
24+
public static void enable(CodeContext context, [DefaultParameterValue(STDERR)] object? file, bool all_threads = true) {
25+
// TODO: the default file object should be sys.stderr
26+
27+
// TODO: fill this up
28+
}
1729
}
1830
}

Src/IronPython.Modules/msvcrt.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ public class PythonMsvcrt {
2929

3030
#region Public API
3131

32+
public const int SEM_FAILCRITICALERRORS = 1;
33+
public const int SEM_NOGPFAULTERRORBOX = 2;
34+
public const int SEM_NOALIGNMENTFAULTEXCEPT = 4;
35+
public const int SEM_NOOPENFILEERRORBOX = 32768;
36+
37+
public static void SetErrorMode(int mode) {
38+
// TODO: fill this up
39+
}
40+
3241
[Documentation(@"heapmin() -> None
3342
3443
Force the malloc() heap to clean itself up and return unused blocks
@@ -226,7 +235,9 @@ public static void ungetwch(char @char) {
226235

227236
[DllImport("msvcr100", SetLastError=true, CallingConvention=CallingConvention.Cdecl)]
228237
private static extern ushort _ungetwch(ushort c);
238+
229239
#endregion
230240
}
231241
}
242+
232243
#endif

Src/IronPython.Modules/nt.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,6 +1583,27 @@ public static terminal_size __new__(CodeContext context, [NotNull] PythonType cl
15831583
}
15841584
}
15851585

1586+
public static void truncate(CodeContext context, [NotNull] string path, BigInteger length) {
1587+
using var stream = new FileStream(path, FileMode.Open);
1588+
stream.SetLength((long)length);
1589+
}
1590+
1591+
[Documentation("")]
1592+
public static void truncate(CodeContext context, [NotNull] Bytes path, BigInteger length)
1593+
=> truncate(context, path.ToFsString(), length);
1594+
1595+
[Documentation("")]
1596+
public static void truncate(CodeContext context, [NotNull] IBufferProtocol path, BigInteger length) {
1597+
PythonOps.Warn(context, PythonExceptions.DeprecationWarning, $"{nameof(truncate)}: {nameof(path)} should be string or bytes, not {PythonTypeOps.GetName(path)}"); // deprecated in 3.6
1598+
truncate(context, path.ToFsBytes(context), length);
1599+
}
1600+
1601+
public static void truncate(CodeContext context, int fd, BigInteger length)
1602+
=> ftruncate(context, fd, length);
1603+
1604+
public static void ftruncate(CodeContext context, int fd, BigInteger length)
1605+
=> context.LanguageContext.FileManager.GetFileFromId(context.LanguageContext, fd).truncate(context, length);
1606+
15861607
#if FEATURE_FILESYSTEM
15871608
public static object times() {
15881609
System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess();

Src/IronPython/Modules/_io.cs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,6 +1951,7 @@ public class TextIOWrapper : _TextIOBase, IEnumerator<object>, IEnumerable<objec
19511951
private object _encoder, _decoder;
19521952

19531953
private bool _line_buffering;
1954+
private bool _write_through;
19541955
private bool _readUniversal;
19551956
private bool _readTranslate;
19561957
internal bool _writeTranslate;
@@ -1969,12 +1970,12 @@ public TextIOWrapper(CodeContext/*!*/ context) : base(context) { }
19691970

19701971
internal static TextIOWrapper Create(CodeContext/*!*/ context,
19711972
object buffer,
1972-
string encoding=null,
1973-
string errors=null,
1974-
string newline=null,
1975-
bool line_buffering=false) {
1973+
string encoding = null,
1974+
string errors = null,
1975+
string newline = null,
1976+
bool line_buffering = false, bool write_through = false) {
19761977
var res = new TextIOWrapper(context);
1977-
res.__init__(context, buffer, encoding, errors, newline, line_buffering);
1978+
res.__init__(context, buffer, encoding, errors, newline, line_buffering, write_through);
19781979
return res;
19791980
}
19801981

@@ -1983,8 +1984,9 @@ public void __init__(
19831984
object buffer,
19841985
string encoding = null,
19851986
string errors = null,
1986-
string newline=null,
1987-
bool line_buffering=false
1987+
string newline = null,
1988+
bool line_buffering = false,
1989+
bool write_through = false
19881990
) {
19891991
switch(newline) {
19901992
case null:
@@ -2017,6 +2019,7 @@ public void __init__(
20172019
PythonOps.IsTrue(PythonOps.Invoke(context, _buffer, "seekable"));
20182020

20192021
_line_buffering = line_buffering;
2022+
_write_through = write_through;
20202023
_readUniversal = string.IsNullOrEmpty(newline);
20212024
_readTranslate = newline == null;
20222025
_readNL = newline;
@@ -2029,23 +2032,15 @@ public void __init__(
20292032

20302033
#region Public API
20312034

2032-
public object buffer {
2033-
get {
2034-
return _buffer;
2035-
}
2036-
}
2035+
public object buffer => _buffer;
20372036

2038-
public override string encoding {
2039-
get { return _encoding; }
2040-
}
2037+
public override string encoding => _encoding;
20412038

2042-
public override string errors {
2043-
get { return _errors; }
2044-
}
2039+
public override string errors => _errors;
20452040

2046-
public bool line_buffering {
2047-
get { return _line_buffering; }
2048-
}
2041+
public bool line_buffering => _line_buffering;
2042+
2043+
public bool write_through => _write_through;
20492044

20502045
public override object newlines {
20512046
get {
@@ -2157,7 +2152,7 @@ public override BigInteger write(CodeContext/*!*/ context, object s) {
21572152
PythonOps.Invoke(context, _buffer, "write", bytes);
21582153
}
21592154

2160-
if (_line_buffering && (hasLF || str.Contains("\r"))) {
2155+
if (_write_through || _line_buffering && (hasLF || str.Contains("\r"))) {
21612156
flush(context);
21622157
}
21632158

Src/IronPython/Runtime/Operations/ObjectOps.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@ private static HashSet<PythonType> NativelyPickleableTypes {
329329
/// Implements the default __reduce_ex__ method as specified by PEP 307 case 3 (new-style instance, protocol 2)
330330
/// </summary>
331331
private static PythonTuple ReduceProtocol2(CodeContext/*!*/ context, object self) {
332+
// builtin types which can't be pickled (due to tp_itemsize != 0)
333+
if (self is MemoryView) {
334+
throw PythonOps.TypeError("can't pickle memoryview objects");
335+
}
336+
332337
PythonType myType = DynamicHelpers.GetPythonType(self);
333338

334339
object? state;

Src/IronPython/Runtime/Operations/PythonOps.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2698,6 +2698,7 @@ public static object ConvertToPythonPrimitive(object value) {
26982698
{
26992699
float f => (double)f,
27002700
double d => d,
2701+
sbyte sb => (int)sb,
27012702
byte b => (int)b,
27022703
char c => (int)c,
27032704
short s => (int)s,

0 commit comments

Comments
 (0)