Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jul 22, 2023. It is now read-only.
/pythonnetPublic archive
forked frompythonnet/pythonnet

Commit146e6ac

Browse files
committed
Stop using Runtime.IsPython2/3
Contrary to what I said before, I don't think trying to providea single DLL for Python 2 and 3. We will in the future defaultto Python 3, and if someone really wants Python 2, for a whilewe will support a separate compile configuration for that.
1 parentca99b17 commit146e6ac

File tree

6 files changed

+48
-35
lines changed

6 files changed

+48
-35
lines changed

‎Python.Runtime/CustomMarshaler.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,11 @@ public static int GetUnicodeByteLength(IntPtr p)
122122
/// </remarks>
123123
publicstaticIntPtrPy3UnicodePy2StringtoPtr(strings)
124124
{
125-
returnRuntime.IsPython3
126-
?Instance.MarshalManagedToNative(s)
127-
:Marshal.StringToHGlobalAnsi(s);
125+
#ifPYTHON2
126+
returnMarshal.StringToHGlobalAnsi(s);
127+
#else
128+
returnInstance.MarshalManagedToNative(s);
129+
#endif
128130
}
129131

130132
/// <summary>
@@ -139,9 +141,11 @@ public static IntPtr Py3UnicodePy2StringtoPtr(string s)
139141
/// </returns>
140142
publicstaticstringPtrToPy3UnicodePy2String(IntPtrp)
141143
{
142-
returnRuntime.IsPython3
143-
?PtrToStringUni(p)
144-
:Marshal.PtrToStringAnsi(p);
144+
#ifPYTHON2
145+
returnMarshal.PtrToStringAnsi(p);
146+
#else
147+
returnPtrToStringUni(p);
148+
#endif
145149
}
146150
}
147151

‎Python.Runtime/converter.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,10 @@ internal static IntPtr GetPythonTypeByAlias(Type op)
8686
if(op==int32Type)
8787
returnRuntime.PyIntType;
8888

89-
if(op==int64Type&&Runtime.IsPython2)
89+
#ifPYTHON2
90+
if(op==int64Type)
9091
returnRuntime.PyLongType;
92+
#endif
9193

9294
if(op==int64Type)
9395
returnRuntime.PyIntType;
@@ -455,8 +457,9 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
455457
returntrue;
456458

457459
caseTypeCode.Int32:
460+
#ifPYTHON2
458461
// Trickery to support 64-bit platforms.
459-
if(Runtime.IsPython2&&Runtime.Is32Bit)
462+
if(Runtime.Is32Bit)
460463
{
461464
op=Runtime.PyNumber_Int(value);
462465

@@ -480,7 +483,8 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
480483
result=ival;
481484
returntrue;
482485
}
483-
else// Python3 always use PyLong API
486+
#else
487+
// Python3 always use PyLong API
484488
{
485489
op=Runtime.PyNumber_Long(value);
486490
if(op==IntPtr.Zero)
@@ -505,6 +509,7 @@ private static bool ToPrimitive(IntPtr value, Type obType, out object result, bo
505509
result=(int)ll;
506510
returntrue;
507511
}
512+
#endif
508513

509514
caseTypeCode.Boolean:
510515
result=Runtime.PyObject_IsTrue(value)!=0;

‎Python.Runtime/exceptions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,12 @@ private Exceptions()
103103
/// </summary>
104104
internalstaticvoidInitialize()
105105
{
106-
stringexceptionsModuleName=Runtime.IsPython3?"builtins":"exceptions";
106+
stringexceptionsModuleName=
107+
#ifPYTHON2
108+
"exceptions";
109+
#else
110+
"builtins";
111+
#endif
107112
exceptions_module=Runtime.PyImport_ImportModule(exceptionsModuleName);
108113

109114
Exceptions.ErrorCheck(exceptions_module);

‎Python.Runtime/importhook.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,17 @@ internal static void InitializeModuleDef()
2828
/// </summary>
2929
staticIntPtrGetNewRefToBuiltins()
3030
{
31-
if(Runtime.IsPython3)
32-
{
33-
returnRuntime.PyImport_ImportModule("builtins");
34-
}
35-
else
36-
{
37-
// dict is a borrowed ref, no need to decref
38-
IntPtrdict=Runtime.PyImport_GetModuleDict();
31+
#if!PYTHON2
32+
returnRuntime.PyImport_ImportModule("builtins");
33+
#else
34+
// dict is a borrowed ref, no need to decref
35+
IntPtrdict=Runtime.PyImport_GetModuleDict();
3936

40-
// GetItemString is a borrowed ref; incref to get a new ref
41-
IntPtrbuiltins=Runtime.PyDict_GetItemString(dict,"__builtin__");
42-
Runtime.XIncref(builtins);
43-
returnbuiltins;
44-
}
37+
// GetItemString is a borrowed ref; incref to get a new ref
38+
IntPtrbuiltins=Runtime.PyDict_GetItemString(dict,"__builtin__");
39+
Runtime.XIncref(builtins);
40+
returnbuiltins;
41+
#endif
4542
}
4643

4744
/// <summary>
@@ -126,11 +123,10 @@ public static IntPtr GetCLRModule(IntPtr? fromList = null)
126123
{
127124
root.InitializePreload();
128125

129-
if(Runtime.IsPython2)
130-
{
131-
Runtime.XIncref(py_clr_module);
132-
returnpy_clr_module;
133-
}
126+
#ifPYTHON2
127+
Runtime.XIncref(py_clr_module);
128+
returnpy_clr_module;
129+
#endif
134130

135131
// Python 3
136132
// update the module dictionary with the contents of the root dictionary

‎Python.Runtime/pythonengine.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ public static string PythonPath
9595
}
9696
set
9797
{
98-
if(Runtime.IsPython2)
99-
{
100-
thrownewNotSupportedException("Set PythonPath not supported on Python 2");
101-
}
98+
#ifPYTHON2
99+
thrownewNotSupportedException("Set PythonPath not supported on Python 2");
100+
#else
102101
Marshal.FreeHGlobal(_pythonPath);
103102
_pythonPath=UcsMarshaler.Py3UnicodePy2StringtoPtr(value);
104103
Runtime.Py_SetPath(_pythonPath);
104+
#endif
105105
}
106106
}
107107

‎Python.Test.Embed/TestCallbacks.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ public void TestNoOverloadException() {
2525
dynamiccallWith42=PythonEngine.Eval("lambda f: f([42])");
2626
varerror=Assert.Throws<PythonException>(()=>callWith42(aFunctionThatCallsIntoPython.ToPython()));
2727
Assert.AreEqual("TypeError",error.PythonTypeName);
28-
stringexpectedArgTypes=Runtime.IsPython2
29-
?"(<type 'list'>)"
30-
:"(<class 'list'>)";
28+
stringexpectedArgTypes=
29+
#ifPYTHON2
30+
"(<type 'list'>)";
31+
#else
32+
"(<class 'list'>)";
33+
#endif
3134
StringAssert.EndsWith(expectedArgTypes,error.Message);
3235
}
3336
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp