Hey guys,**** ** **
I've been trying to use IronPython as a scripting solution in a project I am working on. I am using clrtype.py from the clrtype sample to expose attributes on methods into c# so I can use reflection to register them with my application. The first time the script runs, everything works fine. But I want the users to be able to change the script while the application is running so I need to load and run the script again. When I try to load and run the script a second time I get a ArgumentException “Duplicate type name within an assembly”. ** ** ** I threw together a little example using part of the Sample.py file that is included with the crltype sample. The exception gets thrown on the “scriptSource.Execute(scriptScope);” line the second time through. If I comment out “__metaclass__ = clrtype.ClrClass” from Product in sample.py it runs fine every time. But that disables the clrtype functionality I need.*** * ** ** Any ideas how to get around this? Am I doing something wrong that you can see?**** ** ** I got clrtype.py from the samples download in this link: http://ironpython.codeplex.com/releases/view/12482**** Here's the code snippet where the script is run var engine = Python.CreateEngine(); var scriptSource = engine.CreateScriptSourceFromFile("C:\\Sample\\Python\\Sample.py"); var scriptScope = engine.CreateScope(); scriptSource.Execute(scriptScope); var myClassVar = scriptScope.GetVariable("Product"); var myClass = myClassVar("Name", 10, 2); var result = myClass.calc_total(); System.ArgumentException was unhandled ** Message=Duplicate type name within an assembly.**** Source=Microsoft.Dynamic**** StackTrace:**** at Microsoft.Scripting.Interpreter.NewInstruction.Run(InterpretedFrame frame)** ** at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)**** at Microsoft.Scripting.Interpreter.LightLambda.Run7[T0,T1,T2,T3,T4,T5,T6,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6)**** at System.Dynamic.UpdateDelegates.UpdateAndExecute6[T0,T1,T2,T3,T4,T5,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)**** at IronPython.Runtime.Types.UserInstanceCreator.CreateInstance(CodeContext context, Object arg0, Object arg1, Object arg2)**** at IronPython.Runtime.Types.PythonType.CreateInstance(CodeContext context, Object arg0, Object arg1, Object arg2)**** at IronPython.Runtime.Types.PythonType.__new__(CodeContext context, PythonType cls, String name, PythonTuple bases, PythonDictionary dict, String selfNames)**** at IronPython.Runtime.Types.PythonType.__new__(CodeContext context, PythonType cls, String name, PythonTuple bases, PythonDictionary dict)**** at Microsoft.Scripting.Interpreter.FuncCallInstruction`6.Run(InterpretedFrame frame)**** at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)**** at Microsoft.Scripting.Interpreter.LightLambda.Run6[T0,T1,T2,T3,T4,T5,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)**** at System.Dynamic.UpdateDelegates.UpdateAndExecute5[T0,T1,T2,T3,T4,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4)**** at Microsoft.Scripting.Interpreter.FuncCallInstruction`8.Run(InterpretedFrame frame)**** at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)**** at Microsoft.Scripting.Interpreter.LightLambda.Run6[T0,T1,T2,T3,T4,T5,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)**** at IronPython.Runtime.Operations.PythonOps.MakeClass(CodeContext context, String name, Object[] bases, String selfNames, PythonDictionary vars)**** at IronPython.Runtime.Operations.PythonOps.MakeClass(FunctionCode funcCode, Func`2 body, CodeContext parentContext, String name, Object[] bases, String selfNames)**** at Microsoft.Scripting.Interpreter.FuncCallInstruction`7.Run(InterpretedFrame frame)**** at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)**** at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)**** at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)*** * at IronPython.Compiler.PythonScriptCode.Run(Scope scope)**** at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)*** * at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)**** at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)**** at Microsoft.Scripting.SourceUnit.Execute(Scope scope)**** at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)**** at Commands.TempScriptingComand.Execute(Object lSelectedItem, Task lTask) in C:\dev\Commands\TempScriptingComand.cs:line 152**** at MS.Internal.Commands.CommandHelpers.CriticalExecuteCommandSource(ICommandSource commandSource, Boolean userInitiated)**** at System.Windows.Controls.Primitives.ButtonBase.OnClick()**** at System.Windows.Controls.Button.OnClick()**** at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)**** at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)**** at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)**** at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)**** at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)**** at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)**** at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)**** at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)**** at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)**** at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)**** at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)**** at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)**** at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)**** at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)** ** at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)**** at System.Windows.Input.InputManager.ProcessStagingArea()**** at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)**** at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)**** at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)**** at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)**** at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)**** at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)**** at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)**** at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)**** at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)* *** at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)**** at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)**** at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)**** at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)**** at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)**** at System.Windows.Application.RunDispatcher(Object ignore)**** at System.Windows.Application.RunInternal(Window window)**** at System.Windows.Application.Run(Window window)**** at System.Windows.Application.Run()**** ** ** Here’s the Sample.py file I’m using.** ** ** import clr**** import clrtype**** import System**** from System.Reflection import BindingFlags**** ** ** class IProduct(object):**** __metaclass__ = clrtype.ClrInterface**** ** ** _clrnamespace = "IronPython.Samples.ClrType" **** ** ** @property**** @clrtype.accepts()**** @clrtype.returns(str)**** def Name(self): raise RuntimeError("this should not get called")**** **** @property**** @clrtype.accepts()**** @clrtype.returns(float)**** def Cost(self): raise RuntimeError("this should not get called")**** **** @clrtype.accepts()**** @clrtype.returns(bool)**** def IsAvailable(self): raise RuntimeError("this should not get called")* *** ** ** class Product(IProduct):**** __metaclass__ = clrtype.ClrClass**** **** _clrnamespace = "IronPython.Samples.ClrType" **** **** _clrfields = {**** "name":str,**** "cost":float,**** "_quantity":int**** }**** **** CLSCompliant = clrtype.attribute(System.CLSCompliantAttribute)**** clr.AddReference("System.Xml")**** XmlRoot = clrtype.attribute(System.Xml.Serialization.XmlRootAttribute)** ** **** _clrclassattribs = [**** # Use System.Attribute subtype directly for custom attributes without arguments**** System.ObsoleteAttribute,**** # Use clrtype.attribute for custom attributes with arguments (either positional, named, or both)**** CLSCompliant(False),**** XmlRoot("product", Namespace="www.contoso.com")**** ]**** ** ** def __init__(self, name, cost, quantity):**** self.name = name**** self.cost = cost**** self._quantity = quantity**** ** ** # IProduct methods **** def Name(self): return self.name**** def Cost(self): return self.cost**** def IsAvailable(self): return self.quantity != 0**** ** ** @property**** @clrtype.accepts()**** @clrtype.returns(int)**** def quantity(self): return self._quantity**** **** @quantity.setter**** @clrtype.accepts(int)**** @clrtype.returns()**** def quantity(self, value): self._quantity = value**** ** ** @clrtype.accepts(float)**** @clrtype.returns(float)**** def calc_total(self, discount = 0.0):**** return (self.cost - discount) * self.quantity**** ** ** Thanks,**** ** ** Jared
_______________________________________________ Ironpython-users mailing list Ironpython-users@python.org http://mail.python.org/mailman/listinfo/ironpython-users