Skip to content

Commit f27d7be

Browse files
committed
Make NETSTANDARD the default
Drops also - Custom XDecref/XIncref (should be readded) - Remote object handling for .NET Framework
1 parent e933e79 commit f27d7be

File tree

6 files changed

+33
-193
lines changed

6 files changed

+33
-193
lines changed

Python.Runtime/codegenerator.cs

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,40 @@ namespace Python.Runtime
1313
/// </summary>
1414
internal class CodeGenerator
1515
{
16-
private AssemblyBuilder aBuilder;
17-
private ModuleBuilder mBuilder;
16+
private AssemblyBuilder _aBuilder = null;
1817

19-
internal CodeGenerator()
18+
private AssemblyBuilder aBuilder
19+
{
20+
get
21+
{
22+
if (_aBuilder == null)
23+
{
24+
var aname = new AssemblyName { Name = "__CodeGenerator_Assembly" };
25+
var aa = AssemblyBuilderAccess.Run;
26+
27+
_aBuilder = Thread.GetDomain().DefineDynamicAssembly(aname, aa);
28+
}
29+
30+
return _aBuilder;
31+
}
32+
}
33+
34+
private ModuleBuilder _mBuilder = null;
35+
private ModuleBuilder mBuilder
2036
{
21-
var aname = new AssemblyName { Name = "__CodeGenerator_Assembly" };
22-
var aa = AssemblyBuilderAccess.Run;
37+
get
38+
{
39+
if (_mBuilder == null)
40+
{
41+
_mBuilder = aBuilder.DefineDynamicModule("__CodeGenerator_Module");
42+
}
43+
44+
return _mBuilder;
45+
}
46+
}
2347

24-
aBuilder = Thread.GetDomain().DefineDynamicAssembly(aname, aa);
25-
mBuilder = aBuilder.DefineDynamicModule("__CodeGenerator_Module");
48+
internal CodeGenerator()
49+
{
2650
}
2751

2852
/// <summary>

Python.Runtime/converter.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,7 @@ internal static IntPtr ToPython(object value, Type type)
156156
var pyderived = value as IPythonDerivedType;
157157
if (null != pyderived)
158158
{
159-
#if NETSTANDARD
160159
return ClassDerivedObject.ToPython(pyderived);
161-
#else
162-
// if object is remote don't do this
163-
if (!System.Runtime.Remoting.RemotingServices.IsTransparentProxy(pyderived))
164-
{
165-
return ClassDerivedObject.ToPython(pyderived);
166-
}
167-
#endif
168160
}
169161

170162
// hmm - from Python, we almost never care what the declared

Python.Runtime/nativecall.cs

Lines changed: 0 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ namespace Python.Runtime
2323
/// </summary>
2424
internal class NativeCall
2525
{
26-
#if NETSTANDARD
2726
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
2827
private delegate void Void_1_Delegate(IntPtr a1);
2928

@@ -48,132 +47,5 @@ public static int Int_Call_3(IntPtr fp, IntPtr a1, IntPtr a2, IntPtr a3)
4847
var d = Marshal.GetDelegateForFunctionPointer<Interop.ObjObjArgFunc>(fp);
4948
return d(a1, a2, a3);
5049
}
51-
#else
52-
private static AssemblyBuilder aBuilder;
53-
private static ModuleBuilder mBuilder;
54-
55-
public static INativeCall Impl;
56-
57-
static NativeCall()
58-
{
59-
// The static constructor is responsible for generating the
60-
// assembly and the methods that implement the IJW thunks.
61-
//
62-
// To do this, we actually use reflection on the INativeCall
63-
// interface (defined below) and generate the required thunk
64-
// code based on the method signatures.
65-
66-
var aname = new AssemblyName { Name = "e__NativeCall_Assembly" };
67-
var aa = AssemblyBuilderAccess.Run;
68-
69-
aBuilder = Thread.GetDomain().DefineDynamicAssembly(aname, aa);
70-
mBuilder = aBuilder.DefineDynamicModule("e__NativeCall_Module");
71-
72-
var ta = TypeAttributes.Public;
73-
TypeBuilder tBuilder = mBuilder.DefineType("e__NativeCall", ta);
74-
75-
Type iType = typeof(INativeCall);
76-
tBuilder.AddInterfaceImplementation(iType);
77-
78-
// Use reflection to loop over the INativeCall interface methods,
79-
// calling GenerateThunk to create a managed thunk for each one.
80-
81-
foreach (MethodInfo method in iType.GetMethods())
82-
{
83-
GenerateThunk(tBuilder, method);
84-
}
85-
86-
Type theType = tBuilder.CreateType();
87-
88-
Impl = (INativeCall)Activator.CreateInstance(theType);
89-
}
90-
91-
private static void GenerateThunk(TypeBuilder tb, MethodInfo method)
92-
{
93-
ParameterInfo[] pi = method.GetParameters();
94-
int count = pi.Length;
95-
int argc = count - 1;
96-
97-
var args = new Type[count];
98-
for (var i = 0; i < count; i++)
99-
{
100-
args[i] = pi[i].ParameterType;
101-
}
102-
103-
MethodBuilder mb = tb.DefineMethod(
104-
method.Name,
105-
MethodAttributes.Public |
106-
MethodAttributes.Virtual,
107-
method.ReturnType,
108-
args
109-
);
110-
111-
// Build the method signature for the actual native function.
112-
// This is essentially the signature of the wrapper method
113-
// minus the first argument (the passed in function pointer).
114-
115-
var nargs = new Type[argc];
116-
for (var i = 1; i < count; i++)
117-
{
118-
nargs[i - 1] = args[i];
119-
}
120-
121-
// IL generation: the (implicit) first argument of the method
122-
// is the 'this' pointer and the second is the function pointer.
123-
// This code pushes the real args onto the stack, followed by
124-
// the function pointer, then the calli opcode to make the call.
125-
126-
ILGenerator il = mb.GetILGenerator();
127-
128-
for (var i = 0; i < argc; i++)
129-
{
130-
il.Emit(OpCodes.Ldarg_S, i + 2);
131-
}
132-
133-
il.Emit(OpCodes.Ldarg_1);
134-
135-
il.EmitCalli(OpCodes.Calli,
136-
CallingConvention.Cdecl,
137-
method.ReturnType,
138-
nargs
139-
);
140-
141-
il.Emit(OpCodes.Ret);
142-
143-
tb.DefineMethodOverride(mb, method);
144-
}
145-
146-
147-
public static void Void_Call_1(IntPtr fp, IntPtr a1)
148-
{
149-
Impl.Void_Call_1(fp, a1);
150-
}
151-
152-
public static IntPtr Call_3(IntPtr fp, IntPtr a1, IntPtr a2, IntPtr a3)
153-
{
154-
return Impl.Call_3(fp, a1, a2, a3);
155-
}
156-
157-
public static int Int_Call_3(IntPtr fp, IntPtr a1, IntPtr a2, IntPtr a3)
158-
{
159-
return Impl.Int_Call_3(fp, a1, a2, a3);
160-
}
161-
#endif
162-
}
163-
164-
#if !NETSTANDARD
165-
/// <summary>
166-
/// Defines native call signatures to be generated by NativeCall.
167-
/// </summary>
168-
public interface INativeCall
169-
{
170-
void Void_Call_0(IntPtr funcPtr);
171-
172-
void Void_Call_1(IntPtr funcPtr, IntPtr arg1);
173-
174-
int Int_Call_3(IntPtr funcPtr, IntPtr t, IntPtr n, IntPtr v);
175-
176-
IntPtr Call_3(IntPtr funcPtr, IntPtr a1, IntPtr a2, IntPtr a3);
17750
}
178-
#endif
17951
}

Python.Runtime/polyfill/ReflectionPolifills.cs renamed to Python.Runtime/polyfill/ReflectionPolyfills.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
namespace Python.Runtime
66
{
7-
#if NETSTANDARD
8-
public static class ReflectionPolifills
7+
public static class ReflectionPolyfills
98
{
109
public static AssemblyBuilder DefineDynamicAssembly(this AppDomain appDomain, AssemblyName assemblyName, AssemblyBuilderAccess assemblyBuilderAccess)
1110
{
@@ -17,5 +16,4 @@ public static Type CreateType(this TypeBuilder typeBuilder)
1716
return typeBuilder.GetTypeInfo().GetType();
1817
}
1918
}
20-
#endif
2119
}

Python.Runtime/runtime.cs

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -492,60 +492,14 @@ internal static Type[] PythonArgsToTypeArray(IntPtr arg, bool mangleObjects)
492492
/// </summary>
493493
internal static unsafe void XIncref(IntPtr op)
494494
{
495-
#if PYTHON_WITH_PYDEBUG || NETSTANDARD
496495
Py_IncRef(op);
497496
return;
498-
#else
499-
var p = (void*)op;
500-
if ((void*)0 != p)
501-
{
502-
if (Is32Bit)
503-
{
504-
(*(int*)p)++;
505-
}
506-
else
507-
{
508-
(*(long*)p)++;
509-
}
510-
}
511-
#endif
512497
}
513498

514499
internal static unsafe void XDecref(IntPtr op)
515500
{
516-
#if PYTHON_WITH_PYDEBUG || NETSTANDARD
517501
Py_DecRef(op);
518502
return;
519-
#else
520-
var p = (void*)op;
521-
if ((void*)0 != p)
522-
{
523-
if (Is32Bit)
524-
{
525-
--(*(int*)p);
526-
}
527-
else
528-
{
529-
--(*(long*)p);
530-
}
531-
if ((*(int*)p) == 0)
532-
{
533-
// PyObject_HEAD: struct _typeobject *ob_type
534-
void* t = Is32Bit
535-
? (void*)(*((uint*)p + 1))
536-
: (void*)(*((ulong*)p + 1));
537-
// PyTypeObject: destructor tp_dealloc
538-
void* f = Is32Bit
539-
? (void*)(*((uint*)t + 6))
540-
: (void*)(*((ulong*)t + 6));
541-
if ((void*)0 == f)
542-
{
543-
return;
544-
}
545-
NativeCall.Impl.Void_Call_1(new IntPtr(f), op);
546-
}
547-
}
548-
#endif
549503
}
550504

551505
internal static unsafe long Refcount(IntPtr op)

Python.Test.Embed/TestDomainReload.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//
1212
// Unfortunately this means no continuous integration testing for this case.
1313
//
14-
#if !NETSTANDARD && !NETCOREAPP
14+
#if NETFX
1515
namespace Python.EmbeddingTest
1616
{
1717
class TestDomainReload

0 commit comments

Comments
 (0)