Skip to content

Commit 1429b9f

Browse files
committed
Fixed mono_config.txt
Some fixes and enhancements for the Makefile under Windows and for make dist Fixed PyObject_TYPE for Py_DEBUG builds Fixed an issue with implicit calls of generic methods
1 parent 79a8b84 commit 1429b9f

File tree

7 files changed

+105
-42
lines changed

7 files changed

+105
-42
lines changed

pythonnet/Makefile

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# make PYTHON=/path/to/python DEFINE=additional,defines CSCARGS=additional_args
66
# make clean
77

8-
RELEASE = pythonnet-2.0-alpha3
8+
RELEASE = pythonnet-2.0-alpha2
99
KEYFILE = pythonnet.key
1010

1111
PYTHON ?= python
@@ -23,15 +23,17 @@ ifeq ($(origin WINDIR), undefined)
2323
CSC = gmcs
2424
RUNTIME_REF =
2525
ALL = clr.so monoclr
26-
GACUTIL = gacutil /nologo
26+
GACUTIL = gacutil
27+
SN = sn
2728
else
2829
RUNNER =
29-
ILDASM = ildasm.exe
30-
ILASM = $(WINDIR)/Microsoft.NET/Framework/v2.0.50727/ilasm.exe
31-
CSC = $(WINDIR)/Microsoft.NET/Framework/v2.0.50727/csc.exe
30+
ILDASM = $${PROGRAMFILES}/Microsoft.NET/SDK/v2.0/Bin/ildasm.exe
31+
ILASM = $${WINDIR}/Microsoft.NET/Framework/v2.0.50727/ilasm.exe
32+
CSC = $${WINDIR}/Microsoft.NET/Framework/v2.0.50727/csc.exe
3233
RUNTIME_REF =
3334
ALL =
34-
GACUTIL = $(ProgramFiles)/Microsoft.NET/SDK/v2.0/Bin/gacutil.exe /nologo
35+
GACUTIL = $${PROGRAMFILES}/Microsoft.NET/SDK/v2.0/Bin/gacutil.exe /nologo
36+
SN = $${PROGRAMFILES}/Microsoft.NET/SDK/v2.0/Bin/sn.exe /q
3537
endif
3638

3739
ifeq ($(origin DEFINE), undefined)
@@ -132,20 +134,24 @@ dist: realclean
132134
for PY in python2.4 python2.5; do \
133135
for PYUCS in UCS2 UCS4; do \
134136
make clean; \
135-
make PYTHON=$$PY UCS=$$PYUCS CSCARGS=/keyfile:$(BASEDIR)/$(KEYFILE); \
137+
make PYTHON=$$PY UCS=$$PYUCS CSCARGS="/keyfile:$(BASEDIR)/$(KEYFILE) /optimize"; \
136138
mkdir ./$(RELEASE)/$$PY-$$PYUCS; \
137139
cp *.dll *.exe *.pyd *.so ./$(RELEASE)/$$PY-$$PYUCS/; \
138140
done; \
139141
done;
140142
tar czf $(RELEASE).tar.gz ./$(RELEASE)/
141143
zip -r -6 $(RELEASE).zip ./$(RELEASE)
142-
md5sum $(RELEASE).tar.gz $(RELEASE).zip > $(RELEASE).md5
143-
sha256sum $(RELEASE).tar.gz $(RELEASE).zip > $(RELEASE).sha
144-
gpg -sb $(RELEASE).zip
145-
gpg -sb $(RELEASE).tar.gz
146144
mv $(RELEASE).* ./release/
147145
rm -rf ./$(RELEASE)/
148146

147+
sign:
148+
md5sum ./release/$(RELEASE).tar.gz ./release/$(RELEASE).zip > \
149+
./release/$(RELEASE).md5
150+
sha256sum ./release/$(RELEASE).tar.gz ./release/$(RELEASE).zip > \
151+
./release/$(RELEASE).sha
152+
gpg -sb ./release/$(RELEASE).zip
153+
gpg -sb ./release/$(RELEASE).tar.gz
154+
149155
dis:
150156
$(ILDASM) Python.Runtime.dll /out=Python.Runtime.il
151157

@@ -163,3 +169,5 @@ run: python.exe
163169
install: all
164170
$(PYTHON) setup.py install
165171

172+
mkkey:
173+
$(SN) /k $(KEYFILE)

pythonnet/doc/TODO.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ For PythonNet 2.0
1010

1111
* Deprecate implicit loading of assemblies
1212

13-
* Add support for implicit calls to overloaded methods.
14-
OverloadedMethod[string, int]("egg", 42) works
15-
OverloadedMethod("egg", 42) does not under some circumstances work
16-
1713
* Debug failing unit tests under Mono and report them if they are caused
1814
by incompatibilities in Mono.
1915

pythonnet/doc/mono_config.txt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
# copy this file to ~/.mono/config or add the dllmaps to the global
2-
# configuration file /etc/mono/config
1+
<!-- Mono Config for PythonNet
2+
3+
Copy this file to ~/.mono/config or add the dllmaps to the global
4+
configuration file /etc/mono/config
5+
-->
6+
37
<configuration>
4-
<dllmap dll="python23" target="libpython2.3.so" os="!windows" />
5-
<dllmap dll="python23.dll" target="libpython2.3.so" os="!windows" />
6-
<dllmap dll="python24" target="libpython2.4.so" os="!windows" />
7-
<dllmap dll="python24.dll" target="libpython2.4.so" os="!windows" />
8-
<dllmap dll="python25" target="libpython2.5.so" os="!windows" />
9-
<dllmap dll="python25.dll" target="libpython2.5.so" os="!windows" />
10-
<dllmap dll="python26" target="libpython2.6.so" os="!windows" />
11-
<dllmap dll="python26.dll" target="libpython2.6.so" os="!windows" />
8+
<dllmap dll="python24" target="libpython2.4.so.1" os="!windows" />
9+
<dllmap dll="python25" target="libpython2.5.so.1" os="!windows" />
10+
<dllmap dll="python26" target="libpython2.6.so.1" os="!windows" />
11+
<dllmap dll="python24.dll" target="libpython2.4.so.1" os="!windows" />
12+
<dllmap dll="python25.dll" target="libpython2.5.so.1" os="!windows" />
13+
<dllmap dll="python26.dll" target="libpython2.6.so.1" os="!windows" />
1214
</configuration>
1315

pythonnet/src/runtime/methodbinder.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,21 @@ internal static int ArgPrecedence(Type t) {
162162
//====================================================================
163163

164164
internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw) {
165-
return this.Bind(inst, args, kw, null);
165+
return this.Bind(inst, args, kw, null, null);
166166
}
167167

168168
internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw,
169169
MethodBase info) {
170+
return this.Bind(inst, args, kw, info, null);
171+
}
172+
173+
internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw,
174+
MethodBase info, MethodInfo[] methodinfo) {
170175
// loop to find match, return invoker w/ or /wo error
171176
MethodBase[] _methods = null;
172177
int pynargs = Runtime.PyTuple_Size(args);
173178
object arg;
179+
bool isGeneric = false;
174180

175181
if (info != null) {
176182
_methods = (MethodBase[])Array.CreateInstance(
@@ -184,6 +190,7 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw,
184190

185191
for (int i = 0; i < _methods.Length; i++) {
186192
MethodBase mi = _methods[i];
193+
if (mi.IsGenericMethod) { isGeneric = true; }
187194
ParameterInfo[] pi = mi.GetParameters();
188195
int clrnargs = pi.Length;
189196
bool match = false;
@@ -247,17 +254,32 @@ internal Binding Bind(IntPtr inst, IntPtr args, IntPtr kw,
247254
return new Binding(mi, target, margs, outs);
248255
}
249256
}
257+
// We weren't able to find a matching method but at least one
258+
// is a generic method and info is null. That happens when a generic
259+
// method was not called using the [] syntax. Let's introspect the
260+
// type of the arguments and use it to construct the correct method.
261+
if (isGeneric && (info == null) && (methodinfo != null))
262+
{
263+
Type[] types = Runtime.PythonArgsToTypeArray(args, true);
264+
MethodInfo mi = MethodBinder.MatchParameters(methodinfo, types);
265+
return Bind(inst, args, kw, mi, null);
266+
}
250267
return null;
251268
}
252269

253270
internal virtual IntPtr Invoke(IntPtr inst, IntPtr args, IntPtr kw) {
254-
return this.Invoke(inst, args, kw, null);
271+
return this.Invoke(inst, args, kw, null, null);
255272

256273
}
257274

258275
internal virtual IntPtr Invoke(IntPtr inst, IntPtr args, IntPtr kw,
259276
MethodBase info) {
260-
Binding binding = this.Bind(inst, args, kw, info);
277+
return this.Invoke(inst, args, kw, info, null);
278+
}
279+
280+
internal virtual IntPtr Invoke(IntPtr inst, IntPtr args, IntPtr kw,
281+
MethodBase info, MethodInfo[] methodinfo) {
282+
Binding binding = this.Bind(inst, args, kw, info, methodinfo);
261283
Object result;
262284
IntPtr ts = IntPtr.Zero;
263285

pythonnet/src/runtime/methodobject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public virtual IntPtr Invoke(IntPtr inst, IntPtr args, IntPtr kw) {
6060

6161
public virtual IntPtr Invoke(IntPtr target, IntPtr args, IntPtr kw,
6262
MethodBase info) {
63-
return binder.Invoke(target, args, kw, info);
63+
return binder.Invoke(target, args, kw, info, this.info);
6464
}
6565

6666
//====================================================================

pythonnet/src/runtime/runtime.cs

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,33 @@ public class Runtime {
2626
/// the responsibility of the caller to have acquired the GIL
2727
/// before calling any of these methods.
2828
/// </summary>
29+
#if (UCS4)
30+
public const int UCS = 4;
31+
#endif
32+
#if (UCS2)
33+
public const int UCS = 2;
34+
#endif
35+
#if ! (UCS2 || UCS4)
36+
#error You must define either UCS2 or UCS4!
37+
#endif
38+
2939
#if (PYTHON24)
30-
internal const string dll = "python24";
40+
public const string dll = "python24";
41+
public const string pyversion = "2.4";
42+
public const int pyversionnumber = 24;
3143
#endif
3244
#if (PYTHON25)
33-
internal const string dll = "python25";
45+
public const string dll = "python25";
46+
public const string pyversion = "2.5";
47+
public const int pyversionnumber = 25;
3448
#endif
3549
#if (PYTHON26)
36-
internal const string dll = "python26";
50+
public const string dll = "python26";
51+
public const string pyversion = "2.6";
52+
public const int pyversionnumber = 26;
3753
#endif
3854
#if ! (PYTHON24 || PYTHON25 || PYTHON26)
39-
#error You must define either PYTHON24 or PYTHON25!
55+
#error You must define either PYTHON24, PYTHON25 or PYTHON26!
4056
#endif
4157
internal static bool wrap_exceptions;
4258
internal static bool is32bit;
@@ -117,7 +133,7 @@ internal static void Initialize() {
117133
// of the Python runtime that do not allow new-style classes to
118134
// be used as exceptions (Python versions 2.4 and lower).
119135

120-
#if (PYTHON25)
136+
#if (PYTHON25 || PYTHON26)
121137
wrap_exceptions = false;
122138
#else
123139
IntPtr m = PyImport_ImportModule("exceptions");
@@ -131,7 +147,6 @@ internal static void Initialize() {
131147
Runtime.Decref(m);
132148
#endif
133149

134-
135150
// Initialize modules that depend on the runtime class.
136151
AssemblyManager.Initialize();
137152
PyCLRMetaType = MetaType.Initialize();
@@ -227,6 +242,10 @@ internal static IntPtr ExtendTuple(IntPtr t, params IntPtr[] args) {
227242
}
228243

229244
internal static Type[] PythonArgsToTypeArray(IntPtr arg) {
245+
return PythonArgsToTypeArray(arg, false);
246+
}
247+
248+
internal static Type[] PythonArgsToTypeArray(IntPtr arg, bool mangleObjects) {
230249
// Given a PyObject * that is either a single type object or a
231250
// tuple of (managed or unmanaged) type objects, return a Type[]
232251
// containing the CLR Type objects that map to those types.
@@ -246,6 +265,9 @@ internal static Type[] PythonArgsToTypeArray(IntPtr arg) {
246265

247266
for (int i = 0; i < n; i++) {
248267
IntPtr op = Runtime.PyTuple_GetItem(args, i);
268+
if (mangleObjects && (!Runtime.PyType_Check(op))) {
269+
op = Runtime.PyObject_TYPE(op);
270+
}
249271
ManagedType mt = ManagedType.GetManagedObject(op);
250272

251273
if (mt is ClassBase) {
@@ -575,15 +597,20 @@ internal unsafe static extern IntPtr
575597

576598
internal unsafe static IntPtr
577599
PyObject_TYPE(IntPtr op) {
578-
void *p = (void *)op;
579-
if ((void *)0 == p) {
580-
return IntPtr.Zero;
600+
void* p = (void*)op;
601+
if ((void*)0 == p) {
602+
return IntPtr.Zero;
581603
}
604+
#if (Py_DEBUG)
605+
int n = 3;
606+
#else
607+
int n = 1;
608+
#endif
582609
if (is32bit) {
583-
return new IntPtr((void *)(*((uint *)p + 1)));
610+
return new IntPtr((void*)(*((uint*)p + n)));
584611
}
585612
else {
586-
return new IntPtr((void *)(*((ulong *)p + 1)));
613+
return new IntPtr((void*)(*((ulong*)p + n)));
587614
}
588615
}
589616

@@ -1385,6 +1412,10 @@ internal unsafe static extern int
13851412
// Python type object API
13861413
//====================================================================
13871414

1415+
internal static bool PyType_Check(IntPtr ob) {
1416+
return PyObject_TypeCheck(ob, Runtime.PyTypeType);
1417+
}
1418+
13881419
[DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl,
13891420
ExactSpelling=true, CharSet=CharSet.Ansi)]
13901421
internal unsafe static extern bool

pythonnet/src/tests/test_generic.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def testGenericMethodTypeHandling(self):
299299
"""
300300
from Python.Test import InterfaceTest, ISayHello1, ShortEnum
301301
import System
302-
302+
303303
self._testGenericMethodByType(System.Boolean, True)
304304
self._testGenericMethodByType(bool, True)
305305
self._testGenericMethodByType(System.Byte, 255)
@@ -312,7 +312,7 @@ def testGenericMethodTypeHandling(self):
312312
self._testGenericMethodByType(long, 9223372036854775807L)
313313
self._testGenericMethodByType(System.UInt16, 65000)
314314
self._testGenericMethodByType(System.UInt32, 4294967295L)
315-
self._testGenericMethodByType(System.UInt64, 18446744073709551615L)
315+
self._testGenericMethodByType(System.UInt64, 1844674407370955161L)
316316
self._testGenericMethodByType(System.Single, 3.402823e38)
317317
self._testGenericMethodByType(System.Double, 1.7976931348623157e308)
318318
self._testGenericMethodByType(float, 1.7976931348623157e308)
@@ -324,6 +324,10 @@ def testGenericMethodTypeHandling(self):
324324
self._testGenericMethodByType(System.Object, InterfaceTest())
325325
self._testGenericMethodByType(InterfaceTest, InterfaceTest(), 1)
326326
self._testGenericMethodByType(ISayHello1, InterfaceTest(), 1)
327+
# XXX BUG: The value doesn't fit into Int64 and PythonNet doesn't
328+
# recognize it as UInt64 for unknown reasons.
329+
self._testGenericMethodByType(System.UInt64, 18446744073709551615L)
330+
327331

328332
def testGenericMethodOverloadSelection(self):
329333
"""

0 commit comments

Comments
 (0)