Skip to content

Commit f47ed34

Browse files
authored
Backport changes from 3.6 (#1966)
* Backport changes from 3.6 * Fix test_pep3118
1 parent 5c55837 commit f47ed34

File tree

6 files changed

+83
-24
lines changed

6 files changed

+83
-24
lines changed

Directory.Build.props

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
<FileVersion>$(MajorVersion).$(MinorVersion).$(MicroVersion).$(AssemblyFileRevision)</FileVersion>
2828
<InformationalVersion>$(MSBuildProjectName) $(MajorVersion).$(MinorVersion).$(MicroVersion) $(ReleaseLevel) $(ReleaseSerial)</InformationalVersion>
2929

30+
<DefineConstants>PYTHON_$(MajorVersion)$(MinorVersion)</DefineConstants>
31+
3032
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
3133

3234
<ProduceReferenceAssembly>false</ProduceReferenceAssembly>
@@ -125,7 +127,7 @@
125127
<DebugType>portable</DebugType>
126128
<Optimize>true</Optimize>
127129
<CheckForOverflowUnderflow>false</CheckForOverflowUnderflow>
128-
<DefineConstants>$(Features);$(SignedSym);TRACE</DefineConstants>
130+
<DefineConstants>$(DefineConstants);$(Features);$(SignedSym);TRACE</DefineConstants>
129131
</PropertyGroup>
130132

131133
<!-- Debug -->
@@ -135,6 +137,6 @@
135137
<Optimize>false</Optimize>
136138
<!-- TODO: Python & zlib.net need some work -->
137139
<CheckForOverflowUnderflow>false</CheckForOverflowUnderflow>
138-
<DefineConstants>$(Features);$(SignedSym);DEBUG;TRACE</DefineConstants>
140+
<DefineConstants>$(DefineConstants);$(Features);$(SignedSym);DEBUG;TRACE</DefineConstants>
139141
</PropertyGroup>
140142
</Project>

IronPython.sln

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{17737ACB
4242
eng\net9.0-windows.props = eng\net9.0-windows.props
4343
eng\net9.0.props = eng\net9.0.props
4444
eng\netstandard2.0.props = eng\netstandard2.0.props
45-
eng\steps.yml = eng\steps.yml
4645
eng\Tasks.Targets = eng\Tasks.Targets
4746
EndProjectSection
4847
EndProject

src/core/IronPython.Modules/_ctypes/SimpleType.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,18 @@ public SimpleType(CodeContext/*!*/ context, string name, PythonTuple bases, Pyth
5858
case 'H': _type = SimpleTypeKind.UnsignedShort; break;
5959
case 'i': _type = SimpleTypeKind.SignedInt; break;
6060
case 'I': _type = SimpleTypeKind.UnsignedInt; break;
61-
case 'l': _type = SimpleTypeKind.SignedLong; break;
62-
case 'L': _type = SimpleTypeKind.UnsignedLong; break;
61+
case 'l':
62+
_type = SimpleTypeKind.SignedLong;
63+
#if !PYTHON_34
64+
_charType = TypecodeOps.IsCLong32Bit ? _charType : 'q';
65+
#endif
66+
break;
67+
case 'L':
68+
_type = SimpleTypeKind.UnsignedLong;
69+
#if !PYTHON_34
70+
_charType = TypecodeOps.IsCLong32Bit ? _charType : 'Q';
71+
#endif
72+
break;
6373
case 'f': _type = SimpleTypeKind.Single; break;
6474
case 'g': // long double, new in 2.6
6575
case 'd': _type = SimpleTypeKind.Double; break;

src/core/IronPython.Modules/_datetime.cs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ internal timedelta(TimeSpan ts, double microsecond)
5454
}
5555

5656
public timedelta(double days, double seconds, double microseconds, double milliseconds, double minutes, double hours, double weeks) {
57-
double totalDays = weeks * 7 + days;
58-
double totalSeconds = ((totalDays * 24 + hours) * 60 + minutes) * 60 + seconds;
57+
double totalSeconds = (((weeks * 7 + days) * 24 + hours) * 60 + minutes) * 60 + seconds;
58+
CheckDouble(totalSeconds);
5959

6060
double totalSecondsSharp = Math.Floor(totalSeconds);
6161
double totalSecondsFloat = totalSeconds - totalSecondsSharp;
6262

6363
double totalMicroseconds = Math.Round(totalSecondsFloat * 1e6 + milliseconds * 1000 + microseconds);
64+
CheckDouble(totalMicroseconds);
65+
6466
double otherSecondsFromMicroseconds = Math.Floor(totalMicroseconds / 1e6);
6567

6668
totalSecondsSharp += otherSecondsFromMicroseconds;
@@ -71,28 +73,45 @@ public timedelta(double days, double seconds, double microseconds, double millis
7173
totalMicroseconds += 1e6;
7274
}
7375

74-
_days = (int)(totalSecondsSharp / SECONDSPERDAY);
75-
_seconds = (int)(totalSecondsSharp - _days * SECONDSPERDAY);
76+
_days = ToInt(totalSecondsSharp / SECONDSPERDAY);
77+
_seconds = ToInt(totalSecondsSharp - _days * SECONDSPERDAY);
7678

7779
if (_seconds < 0) {
7880
_days--;
7981
_seconds += (int)SECONDSPERDAY;
8082
}
81-
_microseconds = (int)(totalMicroseconds);
83+
_microseconds = ToInt(totalMicroseconds);
8284

8385
if (Math.Abs(_days) > MAXDAYS) {
8486
throw PythonOps.OverflowError("days={0}; must have magnitude <= 999999999", _days);
8587
}
88+
89+
static void CheckDouble(double d) {
90+
if (double.IsInfinity(d)) {
91+
throw PythonOps.OverflowError("cannot convert float infinity to integer");
92+
} else if (double.IsNaN(d)) {
93+
throw PythonOps.ValueError("cannot convert float NaN to integer");
94+
}
95+
}
96+
97+
static int ToInt(double d) {
98+
if (Int32.MinValue <= d && d <= Int32.MaxValue) {
99+
return (int)d;
100+
} else {
101+
CheckDouble(d);
102+
return checked((int)d);
103+
}
104+
}
86105
}
87106

88107
public static timedelta __new__(CodeContext context, [NotNone] PythonType cls,
89-
double days = 0D,
90-
double seconds = 0D,
91-
double microseconds = 0D,
92-
double milliseconds = 0D,
93-
double minutes = 0D,
94-
double hours = 0D,
95-
double weeks = 0D) {
108+
double days = 0,
109+
double seconds = 0,
110+
double microseconds = 0,
111+
double milliseconds = 0,
112+
double minutes = 0,
113+
double hours = 0,
114+
double weeks = 0) {
96115
if (cls == DynamicHelpers.GetPythonTypeFromType(typeof(timedelta))) {
97116
return new timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks);
98117
} else {
@@ -1218,14 +1237,15 @@ public PythonTuple __reduce__() {
12181237
);
12191238
}
12201239

1221-
// TODO: get rid of __bool__ in 3.5
1240+
#if PYTHON_34
12221241
public bool __bool__() {
12231242
return this.UtcTime.TimeSpan.Ticks != 0 || this.UtcTime.LostMicroseconds != 0;
12241243
}
12251244

12261245
public static explicit operator bool([NotNone] time time) {
12271246
return time.__bool__();
12281247
}
1248+
#endif
12291249

12301250
// instance methods
12311251
public object replace() {

src/core/IronPython.Modules/mmap.cs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@ public MmapDefault(CodeContext/*!*/ context, int fileno, long length, string? ta
338338
_offset = 0; // offset is ignored without an underlying file
339339
_sourceStream = null;
340340

341+
if (length == 0) {
342+
throw PythonNT.GetOsError(PythonErrno.EINVAL);
343+
}
344+
341345
// work around the .NET bug whereby CreateOrOpen throws on a null mapName
342346
if (_mapName is null) {
343347
_file = MemoryMappedFile.CreateNew(null, length, _fileAccess);
@@ -859,7 +863,7 @@ public Bytes read(int len) {
859863
len = checked((int)(_view.Capacity - pos));
860864
}
861865

862-
if (len == 0) {
866+
if (len <= 0) {
863867
return Bytes.Empty;
864868
}
865869

@@ -960,11 +964,6 @@ public void resize(long newsize) {
960964
}
961965
}
962966

963-
if (_sourceStream == null) {
964-
// resizing is not supported without an underlying file
965-
throw WindowsError(PythonExceptions._OSError.ERROR_INVALID_PARAMETER);
966-
}
967-
968967
if (_view.Capacity == newsize) {
969968
// resizing to the same size
970969
return;
@@ -979,6 +978,33 @@ public void resize(long newsize) {
979978
);
980979
}
981980

981+
if (_sourceStream is null) {
982+
// resizing of anonymous map
983+
// TODO: non-copying implementation?
984+
985+
MemoryMappedFile file;
986+
// work around the .NET bug whereby CreateOrOpen throws on a null mapName
987+
if (_mapName is null) {
988+
file = MemoryMappedFile.CreateNew(null, newsize, _fileAccess);
989+
} else {
990+
Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));
991+
file = MemoryMappedFile.CreateOrOpen(_mapName, newsize, _fileAccess);
992+
}
993+
994+
using (var oldStream = _file.CreateViewStream(0, Math.Min(_view.Capacity, newsize))) {
995+
using var newStream = file.CreateViewStream();
996+
oldStream.CopyTo(newStream);
997+
}
998+
999+
_view.Flush();
1000+
_view.Dispose();
1001+
_file.Dispose();
1002+
1003+
_file = file;
1004+
_view = _file.CreateViewAccessor(_offset, newsize, _fileAccess);
1005+
return;
1006+
}
1007+
9821008
_view.Flush();
9831009
_view.Dispose();
9841010
_file.Dispose();

src/core/IronPython.Modules/nt.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,8 @@ public static object open(CodeContext/*!*/ context, [NotNone] string path, int f
849849
}
850850
}
851851

852+
VerifyPath(path, functionName: nameof(open), argName: nameof(path));
853+
852854
if ((RuntimeInformation.IsOSPlatform(OSPlatform.Linux) || RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) && !ClrModule.IsMono) {
853855
// Use PosixFileStream to operate on fd directly
854856
// On Mono, we must use FileStream due to limitations in MemoryMappedFile

0 commit comments

Comments
 (0)