From 18af7600b50298afa090520ebd47a080d24ccb6f Mon Sep 17 00:00:00 2001 From: Tony Roberts Date: Wed, 26 Feb 2014 18:24:39 +0000 Subject: [PATCH] - make sure threads are initialized when starting/importing clr - replace use of Forms in unittests with alternative equivalent tests --- pythonnet/src/monoclr/pynetinit.c | 6 ++---- pythonnet/src/runtime/runtime.cs | 4 +++- pythonnet/src/testing/constructortests.cs | 5 ++--- pythonnet/src/testing/enumtest.cs | 10 ++++++++++ pythonnet/src/testing/fieldtest.cs | 1 + pythonnet/src/testing/methodtest.cs | 3 +-- pythonnet/src/tests/test_constructors.py | 10 +++++----- pythonnet/src/tests/test_enum.py | 9 +++------ pythonnet/src/tests/test_event.py | 7 ------- pythonnet/src/tests/test_method.py | 11 ++++------- pythonnet/src/tests/test_module.py | 3 +++ 11 files changed, 34 insertions(+), 35 deletions(-) diff --git a/pythonnet/src/monoclr/pynetinit.c b/pythonnet/src/monoclr/pynetinit.c index 70468d15f..eaa1d9c8b 100644 --- a/pythonnet/src/monoclr/pynetinit.c +++ b/pythonnet/src/monoclr/pynetinit.c @@ -104,10 +104,8 @@ void main_thread_handler (gpointer user_data) { for (ii = 0; ii < PyList_Size(syspath); ++ii) { const char* pydir = PyString_AsString(PyList_GetItem(syspath, ii)); char* curdir = (char*) malloc(1024); - if (strlen(pydir) == 0) pydir = "."; - - strcpy(curdir, pydir); - strcat(curdir, slash); + strncpy(curdir, strlen(pydir) > 0 ? pydir : ".", 1024); + strncat(curdir, slash, 1024); //look in this directory for the pn_args->pr_file DIR* dirp = opendir(curdir); diff --git a/pythonnet/src/runtime/runtime.cs b/pythonnet/src/runtime/runtime.cs index 6c3bad528..19aa2ca2d 100644 --- a/pythonnet/src/runtime/runtime.cs +++ b/pythonnet/src/runtime/runtime.cs @@ -77,9 +77,11 @@ internal static void Initialize() { if (0 == Runtime.Py_IsInitialized()) { Runtime.Py_Initialize(); - Runtime.PyEval_InitThreads(); } + // make sure threads are initialized even if python was initialized already + Runtime.PyEval_InitThreads(); + IntPtr dict = Runtime.PyImport_GetModuleDict(); IntPtr op = Runtime.PyDict_GetItemString(dict, "__builtin__"); diff --git a/pythonnet/src/testing/constructortests.cs b/pythonnet/src/testing/constructortests.cs index cf87ecce8..59181c3af 100644 --- a/pythonnet/src/testing/constructortests.cs +++ b/pythonnet/src/testing/constructortests.cs @@ -9,7 +9,6 @@ using System; using System.Collections; -using System.Windows.Forms; using System.IO; namespace Python.Test { @@ -53,9 +52,9 @@ public StructConstructorTest(Guid v) { public class SubclassConstructorTest { - public Control value; + public Exception value; - public SubclassConstructorTest(Control v) { + public SubclassConstructorTest(Exception v) { this.value = v; } diff --git a/pythonnet/src/testing/enumtest.cs b/pythonnet/src/testing/enumtest.cs index 2fafd5d64..eeabe8984 100644 --- a/pythonnet/src/testing/enumtest.cs +++ b/pythonnet/src/testing/enumtest.cs @@ -88,4 +88,14 @@ public enum ULongEnum : ulong { Five } + [FlagsAttribute] + public enum FlagsEnum { + Zero, + One, + Two, + Three, + Four, + Five + } + } diff --git a/pythonnet/src/testing/fieldtest.cs b/pythonnet/src/testing/fieldtest.cs index e4fdebbd3..1f345f423 100644 --- a/pythonnet/src/testing/fieldtest.cs +++ b/pythonnet/src/testing/fieldtest.cs @@ -57,6 +57,7 @@ public void Shutup() { public decimal DecimalField = 0; public string StringField; public ShortEnum EnumField; + public FlagsEnum FlagsField; public object ObjectField; public ISpam SpamField; diff --git a/pythonnet/src/testing/methodtest.cs b/pythonnet/src/testing/methodtest.cs index ae19a42d5..086aa58d5 100644 --- a/pythonnet/src/testing/methodtest.cs +++ b/pythonnet/src/testing/methodtest.cs @@ -9,7 +9,6 @@ using System; using System.IO; -using System.Windows.Forms; using System.Collections.Generic; namespace Python.Test { @@ -71,7 +70,7 @@ public Guid TestStructConversion(Guid v) { return v; } - public Control TestSubclassConversion(Control v) { + public Exception TestSubclassConversion(Exception v) { return v; } diff --git a/pythonnet/src/tests/test_constructors.py b/pythonnet/src/tests/test_constructors.py index 05247e9c8..4486e50bd 100644 --- a/pythonnet/src/tests/test_constructors.py +++ b/pythonnet/src/tests/test_constructors.py @@ -49,14 +49,14 @@ def testStructConstructor(self): def testSubclassConstructor(self): """Test subclass constructor args""" from Python.Test import SubclassConstructorTest - from System.Windows.Forms import Form, Control - class sub(Form): + class sub(System.Exception): pass - form = sub() - ob = SubclassConstructorTest(form) - self.assertTrue(isinstance(ob.value, Control)) + instance = sub() + ob = SubclassConstructorTest(instance) + print ob + self.assertTrue(isinstance(ob.value, System.Exception)) diff --git a/pythonnet/src/tests/test_enum.py b/pythonnet/src/tests/test_enum.py index 0f9b3b85f..98db3f3c6 100644 --- a/pythonnet/src/tests/test_enum.py +++ b/pythonnet/src/tests/test_enum.py @@ -122,16 +122,13 @@ def test(): def testEnumWithFlagsAttrConversion(self): """Test enumeration conversion with FlagsAttribute set.""" - from System.Windows.Forms import Label - - # This works because the AnchorStyles enum has FlagsAttribute. - label = Label() - label.Anchor = 99 + # This works because the FlagsField enum has FlagsAttribute. + Test.FieldTest().FlagsField = 99 # This should fail because our test enum doesn't have it. def test(): Test.FieldTest().EnumField = 99 - + self.assertRaises(ValueError, test) diff --git a/pythonnet/src/tests/test_event.py b/pythonnet/src/tests/test_event.py index 526abad40..614828a77 100644 --- a/pythonnet/src/tests/test_event.py +++ b/pythonnet/src/tests/test_event.py @@ -493,7 +493,6 @@ def testRandomMultipleHandlers(self): def testRemoveInternalCallHandler(self): """Test remove on an event sink implemented w/internalcall.""" - clr.AddReference('System.Windows.Forms') object = EventTest() def h(sender, args): @@ -502,12 +501,6 @@ def h(sender, args): object.PublicEvent += h object.PublicEvent -= h - from System.Windows.Forms import Form - f = Form() - f.Click += h - f.Click -= h - f.Dispose() - def testRemoveUnknownHandler(self): """Test removing an event handler that was never added.""" diff --git a/pythonnet/src/tests/test_method.py b/pythonnet/src/tests/test_method.py index 4410e7cfa..03a23cf84 100644 --- a/pythonnet/src/tests/test_method.py +++ b/pythonnet/src/tests/test_method.py @@ -235,16 +235,13 @@ def testMethodCallStructConversion(self): def testSubclassInstanceConversion(self): """Test subclass instance conversion in method call.""" - clr.AddReference("System.Windows.Forms") - from System.Windows.Forms import Form, Control - - class sub(Form): + class sub(System.Exception): pass object = MethodTest() - form = sub() - result = object.TestSubclassConversion(form) - self.assertTrue(isinstance(result, Control)) + instance = sub() + result = object.TestSubclassConversion(instance) + self.assertTrue(isinstance(result, System.Exception)) def testNullArrayConversion(self): diff --git a/pythonnet/src/tests/test_module.py b/pythonnet/src/tests/test_module.py index 57ca1a1d2..401f03cc3 100644 --- a/pythonnet/src/tests/test_module.py +++ b/pythonnet/src/tests/test_module.py @@ -204,6 +204,9 @@ def testFromModuleImportStar(self): def testImplicitAssemblyLoad(self): """Test implicit assembly loading via import.""" + # this test only applies to windows + if sys.platform != "win32": + return def test(): # This should fail until System.Windows.Forms has been pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy