Skip to content

Commit c16ab3b

Browse files
authored
Unify 3.4 and 3.6 (#1967)
1 parent f47ed34 commit c16ab3b

File tree

13 files changed

+108
-19
lines changed

13 files changed

+108
-19
lines changed

Directory.Build.props

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +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>
30+
<PythonSymbols>PYTHON_$(MajorVersion)$(MinorVersion)</PythonSymbols>
31+
<PythonSymbols Condition="'$(MajorVersion)$(MinorVersion)' >= '36'">$(PythonSymbols);PYTHON_36_OR_GREATER</PythonSymbols>
3132

3233
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
3334

@@ -127,7 +128,7 @@
127128
<DebugType>portable</DebugType>
128129
<Optimize>true</Optimize>
129130
<CheckForOverflowUnderflow>false</CheckForOverflowUnderflow>
130-
<DefineConstants>$(DefineConstants);$(Features);$(SignedSym);TRACE</DefineConstants>
131+
<DefineConstants>$(PythonSymbols);$(Features);$(SignedSym);TRACE</DefineConstants>
131132
</PropertyGroup>
132133

133134
<!-- Debug -->
@@ -137,6 +138,6 @@
137138
<Optimize>false</Optimize>
138139
<!-- TODO: Python & zlib.net need some work -->
139140
<CheckForOverflowUnderflow>false</CheckForOverflowUnderflow>
140-
<DefineConstants>$(DefineConstants);$(Features);$(SignedSym);DEBUG;TRACE</DefineConstants>
141+
<DefineConstants>$(PythonSymbols);$(Features);$(SignedSym);DEBUG;TRACE</DefineConstants>
141142
</PropertyGroup>
142143
</Project>

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,11 @@ internal void SetAddress(IntPtr address) {
7272

7373
internal void InitializeFromBuffer(object? data, int offset, int size) {
7474
var bp = data as IBufferProtocol
75+
#if PYTHON_34
7576
?? throw PythonOps.TypeErrorForBadInstance("{0} object does not have the buffer interface", data);
76-
// Python 3.5: PythonOps.TypeErrorForBytesLikeTypeMismatch(data);
77+
#else
78+
?? throw PythonOps.TypeErrorForBytesLikeTypeMismatch(data);
79+
#endif
7780

7881
IPythonBuffer buffer = bp.GetBuffer(BufferFlags.FullRO);
7982
if (buffer.IsReadOnly) {
@@ -97,8 +100,11 @@ internal void InitializeFromBuffer(object? data, int offset, int size) {
97100

98101
internal void InitializeFromBufferCopy(object? data, int offset, int size) {
99102
var bp = data as IBufferProtocol
103+
#if PYTHON_34
100104
?? throw PythonOps.TypeErrorForBadInstance("{0} object does not have the buffer interface", data);
101-
// Python 3.5: PythonOps.TypeErrorForBytesLikeTypeMismatch(data);
105+
#else
106+
?? throw PythonOps.TypeErrorForBytesLikeTypeMismatch(data);
107+
#endif
102108

103109
using IPythonBuffer buffer = bp.GetBuffer();
104110
var span = buffer.AsReadOnlySpan();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ public SimpleType(CodeContext/*!*/ context, string name, PythonTuple bases, Pyth
6060
case 'I': _type = SimpleTypeKind.UnsignedInt; break;
6161
case 'l':
6262
_type = SimpleTypeKind.SignedLong;
63-
#if !PYTHON_34
63+
#if PYTHON_36_OR_GREATER
6464
_charType = TypecodeOps.IsCLong32Bit ? _charType : 'q';
6565
#endif
6666
break;
6767
case 'L':
6868
_type = SimpleTypeKind.UnsignedLong;
69-
#if !PYTHON_34
69+
#if PYTHON_36_OR_GREATER
7070
_charType = TypecodeOps.IsCLong32Bit ? _charType : 'Q';
7171
#endif
7272
break;

src/core/IronPython.Modules/_datetime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ public PythonTuple __reduce__() {
12371237
);
12381238
}
12391239

1240-
#if PYTHON_34
1240+
#if PYTHON_34 // removed in 3.5
12411241
public bool __bool__() {
12421242
return this.UtcTime.TimeSpan.Ticks != 0 || this.UtcTime.LostMicroseconds != 0;
12431243
}

src/core/IronPython.Modules/_heapq.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public static void heappush(CodeContext/*!*/ context, [NotNone] PythonList list,
7777
}
7878
}
7979

80-
// TODO: removed in Python 3.5
80+
#if PYTHON_34 // removed in 3.5
8181
[Documentation("Find the n largest elements in a dataset.\n\n"
8282
+ "Equivalent to: sorted(iterable, reverse=True)[:n]\n"
8383
)]
@@ -110,7 +110,6 @@ public static PythonList nlargest(CodeContext/*!*/ context, int n, object? itera
110110
return ret;
111111
}
112112

113-
// TODO: removed in Python 3.5
114113
[Documentation("Find the n smallest elements in a dataset.\n\n"
115114
+ "Equivalent to: sorted(iterable)[:n]\n"
116115
)]
@@ -142,6 +141,7 @@ public static PythonList nsmallest(CodeContext/*!*/ context, int n, object? iter
142141
HeapSort(context, ret);
143142
return ret;
144143
}
144+
#endif
145145

146146
#endregion
147147

src/core/IronPython.Modules/_sre.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ namespace IronPython.Modules {
1414
public static class PythonSRegEx {
1515
public const string __doc__ = "non-functional _sre module. Included only for completeness.";
1616

17+
#if PYTHON_34
1718
public const int MAGIC = 20031017;
19+
#else
20+
public const int MAGIC = 20140917;
21+
#endif
1822
public const int CODESIZE = 2;
1923
public const int MAXREPEAT = 65535;
2024
public const int MAXGROUPS = int.MaxValue;

src/core/IronPython.Modules/re.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,9 @@ public class Pattern : IWeakReferenceable {
165165
internal Pattern(CodeContext/*!*/ context, object pattern, ReFlags flags = 0, bool compiled = false) {
166166
_prePattern = PreParseRegex(context, PatternAsString(pattern, ref flags), verbose: flags.HasFlag(ReFlags.VERBOSE), isBytes: !flags.HasFlag(ReFlags.UNICODE), out ReFlags options);
167167
flags |= options;
168-
// TODO: re-enable in 3.6
169-
// if (flags.HasFlag(ReFlags.UNICODE | ReFlags.LOCALE)) throw PythonOps.ValueError("cannot use LOCALE flag with a str pattern");
168+
#if PYTHON_36_OR_GREATER
169+
if (flags.HasFlag(ReFlags.UNICODE | ReFlags.LOCALE)) throw PythonOps.ValueError("cannot use LOCALE flag with a str pattern");
170+
#endif
170171
if (flags.HasFlag(ReFlags.ASCII | ReFlags.LOCALE)) throw PythonOps.ValueError("ASCII and LOCALE flags are incompatible");
171172
_re = GenRegex(context, _prePattern, flags, compiled, false);
172173
this.pattern = pattern;
@@ -418,7 +419,7 @@ public object sub(CodeContext/*!*/ context, object? repl, object? @string, int c
418419
// only when not adjacent to a previous match
419420
if (string.IsNullOrEmpty(match.Value) && match.Index == prevEnd) {
420421
return "";
421-
};
422+
}
422423
prevEnd = match.Index + match.Length;
423424

424425
if (replacement != null) return UnescapeGroups(context, match, replacement);
@@ -445,7 +446,7 @@ public PythonTuple subn(CodeContext/*!*/ context, object? repl, object? @string,
445446
// only when not adjacent to a previous match
446447
if (string.IsNullOrEmpty(match.Value) && match.Index == prevEnd) {
447448
return "";
448-
};
449+
}
449450
prevEnd = match.Index + match.Length;
450451

451452
totalCount++;

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,20 @@ public TupleExpression(bool expandable, params Expression[] items)
1717
}
1818

1919
internal override string? CheckAssign() {
20+
#if !PYTHON_36_OR_GREATER
2021
if (Items.Count == 0) {
21-
// TODO: remove this when we get to 3.6
2222
return "can't assign to ()";
2323
}
24+
#endif
2425

2526
return base.CheckAssign();
2627
}
2728

2829
internal override string? CheckDelete() {
30+
#if !PYTHON_36_OR_GREATER
2931
if (Items.Count == 0)
30-
return "can't delete ()"; // TODO: remove this when we get to 3.6
32+
return "can't delete ()";
33+
#endif
3134
return base.CheckDelete();
3235
}
3336

src/core/IronPython/Modules/Builtin.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ public static object __import__(CodeContext/*!*/ context, [NotNone] string name,
5858

5959
object ret = Importer.ImportModule(context, globals, name, from != null && from.Count > 0, level);
6060
if (ret == null) {
61+
#if PYTHON_36_OR_GREATER // ModuleNotFoundError since Python 3.6
62+
var err = PythonOps.ModuleNotFoundError("No module named {0}", PythonOps.Repr(context, name));
63+
#else
6164
var err = PythonOps.ImportError("No module named '{0}'", name);
65+
#endif
6266
((PythonExceptions._ImportError)err.GetPythonException()!).name = name;
6367
return LightExceptions.Throw(err);
6468
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1219,8 +1219,11 @@ private static void AppendValueForTranslate(this StringBuilder ret, object? mapp
12191219
return;
12201220
case int mappedInt:
12211221
if (mappedInt > 0xFFFF) {
1222-
// TODO: change to a ValueError in Python 3.5
1222+
#if PYTHON_34
12231223
throw PythonOps.TypeError("character mapping must be in range(0x10000)");
1224+
#else
1225+
throw PythonOps.ValueError("character mapping must be in range(0x10000)");
1226+
#endif
12241227
}
12251228
ret.Append((char)mappedInt);
12261229
break;

0 commit comments

Comments
 (0)