Skip to content

Commit d131917

Browse files
committed
Drop Python 2 support
1 parent 9fb545a commit d131917

16 files changed

+37
-544
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ python:
66
- 3.7
77
- 3.6
88
- 3.5
9-
- 2.7
109

1110
env:
1211
matrix:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
1010
### Added
1111

1212
### Changed
13+
- Drop support for Python 2
1314

1415
### Fixed
1516

appveyor.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ build: off
33

44
image:
55
- Visual Studio 2017
6-
6+
77
platform:
88
- x86
99
- x64
@@ -23,13 +23,10 @@ environment:
2323
BUILD_OPTS: --xplat
2424
- PYTHON_VERSION: 3.5
2525
BUILD_OPTS: --xplat
26-
- PYTHON_VERSION: 2.7
27-
BUILD_OPTS: --xplat
2826
- PYTHON_VERSION: 3.8
2927
- PYTHON_VERSION: 3.7
3028
- PYTHON_VERSION: 3.6
3129
- PYTHON_VERSION: 3.5
32-
- PYTHON_VERSION: 2.7
3330

3431
init:
3532
# Update Environment Variables based on matrix/platform

setup.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -255,17 +255,11 @@ def build_extension(self, ext):
255255

256256
# Up to Python 3.2 sys.maxunicode is used to determine the size of
257257
# Py_UNICODE, but from 3.3 onwards Py_UNICODE is a typedef of wchar_t.
258-
# TODO: Is this doing the right check for Py27?
259-
if sys.version_info[:2] <= (3, 2):
260-
unicode_width = 2 if sys.maxunicode < 0x10FFFF else 4
261-
else:
262-
import ctypes
263-
264-
unicode_width = ctypes.sizeof(ctypes.c_wchar)
258+
import ctypes
259+
unicode_width = ctypes.sizeof(ctypes.c_wchar)
265260

266261
defines = [
267262
"PYTHON{0}{1}".format(PY_MAJOR, PY_MINOR),
268-
"PYTHON{0}".format(PY_MAJOR), # Python Major Version
269263
"UCS{0}".format(unicode_width),
270264
]
271265

@@ -274,7 +268,6 @@ def build_extension(self, ext):
274268

275269
if sys.platform != "win32" and (DEVTOOLS == "Mono" or DEVTOOLS == "dotnet"):
276270
on_darwin = sys.platform == "darwin"
277-
defines.append("MONO_OSX" if on_darwin else "MONO_LINUX")
278271

279272
# Check if --enable-shared was set when Python was built
280273
enable_shared = sysconfig.get_config_var("Py_ENABLE_SHARED")
@@ -645,8 +638,6 @@ def run(self):
645638
"Intended Audience :: Developers",
646639
"License :: OSI Approved :: MIT License",
647640
"Programming Language :: C#",
648-
"Programming Language :: Python :: 2",
649-
"Programming Language :: Python :: 2.7",
650641
"Programming Language :: Python :: 3",
651642
"Programming Language :: Python :: 3.5",
652643
"Programming Language :: Python :: 3.6",

src/clrmodule/ClrModule.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,8 @@
3030

3131
public class clrModule
3232
{
33-
#if PYTHON3
3433
[DllExport("PyInit_clr", CallingConvention.StdCall)]
3534
public static IntPtr PyInit_clr()
36-
#elif PYTHON2
37-
[DllExport("initclr", CallingConvention.StdCall)]
38-
public static void initclr()
39-
#endif
4035
{
4136
DebugPrint("Attempting to load 'Python.Runtime' using standard binding rules.");
4237
#if USE_PYTHON_RUNTIME_PUBLIC_KEY_TOKEN
@@ -95,23 +90,15 @@ public static void initclr()
9590
catch (InvalidOperationException)
9691
{
9792
DebugPrint("Could not load 'Python.Runtime'.");
98-
#if PYTHON3
9993
return IntPtr.Zero;
100-
#elif PYTHON2
101-
return;
102-
#endif
10394
}
10495
}
10596

10697
// Once here, we've successfully loaded SOME version of Python.Runtime
10798
// So now we get the PythonEngine and execute the InitExt method on it.
10899
Type pythonEngineType = pythonRuntime.GetType("Python.Runtime.PythonEngine");
109100

110-
#if PYTHON3
111101
return (IntPtr)pythonEngineType.InvokeMember("InitExt", BindingFlags.InvokeMethod, null, null, null);
112-
#elif PYTHON2
113-
pythonEngineType.InvokeMember("InitExt", BindingFlags.InvokeMethod, null, null, null);
114-
#endif
115102
}
116103

117104
/// <summary>

src/embed_tests/TestCallbacks.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ public void TestNoOverloadException() {
2525
dynamic callWith42 = PythonEngine.Eval("lambda f: f([42])");
2626
var error = Assert.Throws<PythonException>(() => callWith42(aFunctionThatCallsIntoPython.ToPython()));
2727
Assert.AreEqual("TypeError", error.PythonTypeName);
28-
string expectedArgTypes = Runtime.IsPython2
29-
? "(<type 'list'>)"
30-
: "(<class 'list'>)";
28+
string expectedArgTypes = "(<class 'list'>)";
3129
StringAssert.EndsWith(expectedArgTypes, error.Message);
3230
}
3331
}

src/runtime/CustomMarshaler.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ public static int GetUnicodeByteLength(IntPtr p)
120120
/// </remarks>
121121
public static IntPtr Py3UnicodePy2StringtoPtr(string s)
122122
{
123-
return Runtime.IsPython3
124-
? Instance.MarshalManagedToNative(s)
125-
: Marshal.StringToHGlobalAnsi(s);
123+
Instance.MarshalManagedToNative(s);
126124
}
127125

128126
/// <summary>
@@ -137,9 +135,7 @@ public static IntPtr Py3UnicodePy2StringtoPtr(string s)
137135
/// </returns>
138136
public static string PtrToPy3UnicodePy2String(IntPtr p)
139137
{
140-
return Runtime.IsPython3
141-
? PtrToStringUni(p)
142-
: Marshal.PtrToStringAnsi(p);
138+
return PtrToStringUni(p);
143139
}
144140
}
145141

src/runtime/converter.cs

Lines changed: 22 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ internal static IntPtr GetPythonTypeByAlias(Type op)
8686
if (op == int32Type)
8787
return Runtime.PyIntType;
8888

89-
if (op == int64Type && Runtime.IsPython2)
90-
return Runtime.PyLongType;
91-
9289
if (op == int64Type)
9390
return Runtime.PyIntType;
9491

@@ -488,63 +485,35 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
488485
return true;
489486

490487
case TypeCode.Int32:
491-
// Trickery to support 64-bit platforms.
492-
if (Runtime.IsPython2 && Runtime.Is32Bit)
488+
// Python3 always use PyLong API
489+
op = Runtime.PyNumber_Long(value);
490+
if (op == IntPtr.Zero)
493491
{
494-
op = Runtime.PyNumber_Int(value);
495-
496-
// As of Python 2.3, large ints magically convert :(
497-
if (Runtime.PyLong_Check(op))
492+
Exceptions.Clear();
493+
if (Exceptions.ExceptionMatches(overflow))
498494
{
499-
Runtime.XDecref(op);
500495
goto overflow;
501496
}
502-
503-
if (op == IntPtr.Zero)
504-
{
505-
if (Exceptions.ExceptionMatches(overflow))
506-
{
507-
goto overflow;
508-
}
509-
goto type_error;
510-
}
511-
ival = (int)Runtime.PyInt_AsLong(op);
512-
Runtime.XDecref(op);
513-
result = ival;
514-
return true;
497+
goto type_error;
515498
}
516-
else // Python3 always use PyLong API
499+
long ll = (long)Runtime.PyLong_AsLongLong(op);
500+
Runtime.XDecref(op);
501+
if (ll == -1 && Exceptions.ErrorOccurred())
517502
{
518-
op = Runtime.PyNumber_Long(value);
519-
if (op == IntPtr.Zero)
520-
{
521-
Exceptions.Clear();
522-
if (Exceptions.ExceptionMatches(overflow))
523-
{
524-
goto overflow;
525-
}
526-
goto type_error;
527-
}
528-
long ll = (long)Runtime.PyLong_AsLongLong(op);
529-
Runtime.XDecref(op);
530-
if (ll == -1 && Exceptions.ErrorOccurred())
531-
{
532-
goto overflow;
533-
}
534-
if (ll > Int32.MaxValue || ll < Int32.MinValue)
535-
{
536-
goto overflow;
537-
}
538-
result = (int)ll;
539-
return true;
503+
goto overflow;
540504
}
505+
if (ll > Int32.MaxValue || ll < Int32.MinValue)
506+
{
507+
goto overflow;
508+
}
509+
result = (int)ll;
510+
return true;
541511

542512
case TypeCode.Boolean:
543513
result = Runtime.PyObject_IsTrue(value) != 0;
544514
return true;
545515

546516
case TypeCode.Byte:
547-
#if PYTHON3
548517
if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType))
549518
{
550519
if (Runtime.PyBytes_Size(value) == 1)
@@ -555,18 +524,6 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
555524
}
556525
goto type_error;
557526
}
558-
#elif PYTHON2
559-
if (Runtime.PyObject_TypeCheck(value, Runtime.PyStringType))
560-
{
561-
if (Runtime.PyString_Size(value) == 1)
562-
{
563-
op = Runtime.PyString_AsString(value);
564-
result = (byte)Marshal.ReadByte(op);
565-
return true;
566-
}
567-
goto type_error;
568-
}
569-
#endif
570527

571528
op = Runtime.PyNumber_Int(value);
572529
if (op == IntPtr.Zero)
@@ -589,7 +546,6 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
589546
return true;
590547

591548
case TypeCode.SByte:
592-
#if PYTHON3
593549
if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType))
594550
{
595551
if (Runtime.PyBytes_Size(value) == 1)
@@ -600,18 +556,6 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
600556
}
601557
goto type_error;
602558
}
603-
#elif PYTHON2
604-
if (Runtime.PyObject_TypeCheck(value, Runtime.PyStringType))
605-
{
606-
if (Runtime.PyString_Size(value) == 1)
607-
{
608-
op = Runtime.PyString_AsString(value);
609-
result = (sbyte)Marshal.ReadByte(op);
610-
return true;
611-
}
612-
goto type_error;
613-
}
614-
#endif
615559

616560
op = Runtime.PyNumber_Int(value);
617561
if (op == IntPtr.Zero)
@@ -634,7 +578,6 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
634578
return true;
635579

636580
case TypeCode.Char:
637-
#if PYTHON3
638581
if (Runtime.PyObject_TypeCheck(value, Runtime.PyBytesType))
639582
{
640583
if (Runtime.PyBytes_Size(value) == 1)
@@ -645,18 +588,6 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
645588
}
646589
goto type_error;
647590
}
648-
#elif PYTHON2
649-
if (Runtime.PyObject_TypeCheck(value, Runtime.PyStringType))
650-
{
651-
if (Runtime.PyString_Size(value) == 1)
652-
{
653-
op = Runtime.PyString_AsString(value);
654-
result = (char)Marshal.ReadByte(op);
655-
return true;
656-
}
657-
goto type_error;
658-
}
659-
#endif
660591
else if (Runtime.PyObject_TypeCheck(value, Runtime.PyUnicodeType))
661592
{
662593
if (Runtime.PyUnicode_GetSize(value) == 1)
@@ -753,20 +684,20 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
753684
}
754685
goto type_error;
755686
}
756-
687+
757688
uint ui;
758-
try
689+
try
759690
{
760691
ui = Convert.ToUInt32(Runtime.PyLong_AsUnsignedLong(op));
761692
} catch (OverflowException)
762693
{
763694
// Probably wasn't an overflow in python but was in C# (e.g. if cpython
764-
// longs are 64 bit then 0xFFFFFFFF + 1 will not overflow in
695+
// longs are 64 bit then 0xFFFFFFFF + 1 will not overflow in
765696
// PyLong_AsUnsignedLong)
766697
Runtime.XDecref(op);
767698
goto overflow;
768699
}
769-
700+
770701

771702
if (Exceptions.ErrorOccurred())
772703
{
@@ -900,7 +831,7 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s
900831

901832
var listType = typeof(List<>);
902833
var constructedListType = listType.MakeGenericType(elementType);
903-
IList list = IsSeqObj ? (IList) Activator.CreateInstance(constructedListType, new Object[] {(int) len}) :
834+
IList list = IsSeqObj ? (IList) Activator.CreateInstance(constructedListType, new Object[] {(int) len}) :
904835
(IList) Activator.CreateInstance(constructedListType);
905836
IntPtr item;
906837

@@ -921,7 +852,7 @@ private static bool ToArray(IntPtr value, Type obType, out object result, bool s
921852

922853
items = Array.CreateInstance(elementType, list.Count);
923854
list.CopyTo(items, 0);
924-
855+
925856
result = items;
926857
return true;
927858
}

src/runtime/delegateobject.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
9696
/// <summary>
9797
/// Implements __cmp__ for reflected delegate types.
9898
/// </summary>
99-
#if PYTHON3 // TODO: Doesn't PY2 implement tp_richcompare too?
10099
public new static IntPtr tp_richcompare(IntPtr ob, IntPtr other, int op)
101100
{
102101
if (op != Runtime.Py_EQ && op != Runtime.Py_NE)
@@ -126,13 +125,5 @@ public static IntPtr tp_call(IntPtr ob, IntPtr args, IntPtr kw)
126125
Runtime.XIncref(pyfalse);
127126
return pyfalse;
128127
}
129-
#elif PYTHON2
130-
public static int tp_compare(IntPtr ob, IntPtr other)
131-
{
132-
Delegate d1 = GetTrueDelegate(ob);
133-
Delegate d2 = GetTrueDelegate(other);
134-
return d1 == d2 ? 0 : -1;
135-
}
136-
#endif
137128
}
138129
}

0 commit comments

Comments
 (0)