Re: [IronPython] repr() results with uint, etc.

2009-02-07 Thread Jeff Slutter
I totally agree that it should be done the right way - I didn't know
what the right way was.

CodePlex issue has been opened: 21040

Thanks!

Dino Viehland wrote:
 And the correct change to the source code would be adding __repr__ methods to 
 the various *Ops types (Int16Ops, UInt16Ops, etc...) which return the correct 
 formatting.  Presumably by updating the scripts that generate these types.  
 OTOH there's nothing particularly unsafe about your changes other then they 
 don't match the normal convention for __repr__.
___
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


[IronPython] repr() results with uint, etc.

2009-02-06 Thread Jeff Slutter
I have functions (in C#) that return results as everything from byte,
sbyte, System.UInt16, int, uint, float, etc.

If I use repr() on the returned value within IP2.0 only bool, int,
int64, float, double and string types print out a nice value using repr.

The other types (byte, char, sbyte, uint16, int16, uint, uint64) all
print out like:
System.UInt32 object at 0x002B [5]


Is there a way to get repr to print out a nice result like int types?
(some way from inside IronPython, a different version of repr?)

Thanks,
Jeff
___
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


Re: [IronPython] repr() results with uint, etc.

2009-02-06 Thread Jeff Slutter
Ok, looking at the source, I see the issue in:

public static string Repr(CodeContext/*!*/ context, object o)

Is it safe to add these types in to there like:
if ((s = o as string) != null) return StringOps.__repr__(s);
if (o is int) return Int32Ops.__repr__((int)o);
if (o is uint) return ((uint)o).ToString();
if (o is long) return ((long)o).ToString() + L;
if (o is ulong) return ((ulong)o).ToString() + UL;
if (o is byte) return ((byte)o).ToString();
if (o is sbyte) return ((sbyte)o).ToString();
if (o is char) return ((char)o).ToString();
if (o is Int16) return ((Int16)o).ToString();
if (o is UInt16) return ((UInt16)o).ToString();
?


Jeff Slutter wrote:
 I have functions (in C#) that return results as everything from byte,
 sbyte, System.UInt16, int, uint, float, etc.
 
 If I use repr() on the returned value within IP2.0 only bool, int,
 int64, float, double and string types print out a nice value using repr.
 
 The other types (byte, char, sbyte, uint16, int16, uint, uint64) all
 print out like:
 System.UInt32 object at 0x002B [5]
 
 
 Is there a way to get repr to print out a nice result like int types?
 (some way from inside IronPython, a different version of repr?)
 
 Thanks,
 Jeff
 ___
 Users mailing list
 Users@lists.ironpython.com
 http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
 

___
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


Re: [IronPython] Redirecting stdout/stderr, but with context

2009-01-30 Thread Jeff Slutter
Dino Viehland wrote:
 You can always provide your own stream which is aware of what the current 
 output window is.  It could store this in a thread static or just some 
 variable that you update whenever the active window changes.  You can set 
 that via ScriptRuntime.IO.OutputStream.  You could conceptually do the exact 
 same thing from Python just by setting sys.stdout to a file-like object which 
 knows what the current output window is.

This is what I'm doing right now and that's where I'm having my problem.
   I don't think this is a solvable problem since the output stream is
shared by all ScriptScopes, just as the output stream for
System.Console.WriteLine is the same across the process.

In a nutshell, I have this:

class Document
{
  void AppendOutput( string text )
  {
m_buffer.Add(text);
  }
  Liststring m_buffer;
}

class ConsoleWindow: Form
{
   public void WriteOutput( string text, Document doc )
   {
  doc.AppendOutput( text );
  if( doc == m_currentDisplayingDoc )
  {
 textBox.Text += text;
  }
   }
}

class DocWriter
{
  public DocWriter( ConsoleWindow console, Document doc )
  {
m_console = console;
m_document = doc;
  }

  public void write(string text)
  {
m_console.WriteOutput( text, m_document );
  }
  ConsoleWindow m_console;
  Document m_document;
}

void OnDocumentActivate(Document doc)
{
  DocWriter writer = new DocWrite(guiConsole, doc);
  object sysObj = doc.scope.GetVariable(sys);
  m_engine.Operations.SetMember(sysObj, stdout, writer);
  m_engine.Operations.SetMember(sysObj, stderr, writer);
}


The problem is:
If there is a script running on DocumentA, that is streaming output, and
then I switch to DocumentB while DocumentA continues to run - the output
for DocumentA starts appearing in the console window for DocumentB as if
it was for B. This is because sysout is system wide and if I change it
while DocumentA is writing, then it starts outputting to the new stream
that was set (and that was registered with DocumentB).


-Jeff
___
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


Re: [IronPython] Redirecting stdout/stderr, but with context

2009-01-30 Thread Jeff Slutter
Aha! That looks like exactly what I need! It will take some tweaking of
the code, but this should give me the information I need to route things
properly.

Now, I just need to find out if I can do something similar for
System.Console, but I can live with it if I can't for that one. Python
output was the one I *really* needed.

Thanks Dino!

Dino Viehland wrote:
 Can you run each document on its own thread?
 
 Oh, it also looks like we actually flow CodeContext through to the caller.  
 So you could do something like:
 
 class MyFile {
 public void write(CodeContext context, string data) {
 Scope curScope = context.Scope; // this gives you the 
 module you're running in
 
 // handle write here keyed off of the scope object
 }
 }
 
 And replace sys.stdout with that.
___
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


[IronPython] Handling the import of scripts

2009-01-29 Thread Jeff Slutter
If there a way for me to intercept when IronPython wants to import a
script, so I can provide the script text through my own ways. I have a
situation where my Python files are not files on the disk, but packed
into a zip file. When I go to run a script I can get the source to
IronPython to run, but, if that script imports another script that is
also in the zip file, I would like to go through my methods of resolving
the file, loading it, and passing it back to IronPython.

-Jeff
___
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


Re: [IronPython] Printing an object out as a string

2009-01-27 Thread Jeff Slutter
Curt Hagenlocher wrote:
 If you're willing to live on the bleeding edge and build from the latest
 sources, it turns out that Tomas just added this functionality to the
 hosting interfaces yesterday. ObjectOperations.Format(obj) should return
 a string that matches what the REPL would have printed.

I have no problem with living on the bleeding edge. But I realized why
repr wasn't working so well for me:

In a nutshell, I need to get the Python function call string that was
made (including arguments), for logging purposes. The functions are in
my C# code, the calls are made from Python.

I can get the name of the function through some means, but the arguments
are the part I've having a hard time with. I can make the assumption
that the arguments being passed into the function have a textual
representation.

So, if I have a function in C# like:

void MyFunc( IEnumerableint someList )
{
string someListAsStr = SomeMagicFunction(someList);
Log( someListAsStr );
}


and in Python I call it like:

a = [0,1,2]
MyFunc( a )


I want to log the string [0,1,2]

repr wasn't working for me, I believe, because once I get inside MyFunc,
IronPython has wrapped and converted the PythonList into this
IEnumerableint I have. So, repr was giving me something not so pretty.

Will the new Format function give me what I want? Or should I continue
down a different path?

Thanks,
Jeff
___
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


Re: [IronPython] Loading IronPython.dll from memory

2008-12-17 Thread Jeff Slutter
I snipped a little extra - my code is really:

Dictionarystring, object runtimeOptions = new Dictionarystring,
object();
runtimeOptions[Debug] = true;
m_runtime = IronPython.Hosting.Python.CreateRuntime(runtimeOptions);
m_runtime.LoadAssembly(typeof(String).Assembly); // Add reference to System
m_runtime.LoadAssembly(typeof(Uri).Assembly); // Add reference to mscorlib
m_runtime.LoadAssembly(System.Reflection.Assembly.GetCallingAssembly());
m_engine = m_runtime.GetEngine(Python);

I've also tried replacing the above with:
m_engine = Python.CreateEngine();

and I get the same exception in the call to Python.CreateEngine()

(Note: the exception exists without all the extra LoadAssemblys and
stripped to the bone).



Michael Foord wrote:
 Jeff Slutter wrote:
 [I can work around this, but I'm curious if there is a solution]

 Due to some crazy requirements, I'm trying to load my assemblies from
 memory. I have setup an Assembly Resolver with:

 AppDomain.CurrentDomain.AssemblyResolve += new
 ResolveEventHandler(ResolveInternalAssembly);

 And it gets called. Inside the ResolveInternalAssembly I do the
 following:

 1) check my Dictionary to see if I have already loaded the assembly, if
 so I return it, otherwise...
 2) I, currently, open the file, read in the file to byte[] and then call
 3) AppDomain.CurrentDomain.Load(asmbytes) to load the Assembly, store it
 to the dictionary and return it.

 (see:
 http://weblogs.asp.net/justin_rogers/archive/2004/06/07/149964.aspx)

 This works well and all until I get to the following code within one of
 my loaded assemblies:
 m_engine = m_runtime.GetEngine(Python);
   
 
 Is this using the IronPython hosting API? That doesn't look like code
 that would have worked with any recent version of IronPython.
 
 Try:
 
 engine = Python.CreateEngine();
 
 Michael
 
 
 
 It throws an exception saying:
 Microsoft.Scripting.InvalidImplementationException
 Type 'IronPython.Runtime.PythonContext' doesn't provide a suitable
 public constructor or its implementation is faulty: The type initializer
 for 'IronPython.Runtime.SysModule' threw an exception.


 I assume this is some sort of security/permissions problem because that
 is the main thing that is different when loading an assembly from memory
 than file.

 My work around is to pre-load the IronPython.dll assembly before hooking
 up my AssemblyResolve (its the only one I have to 'preload')
 dynamicAssemblyHash[IronPython] =
 Assembly.LoadFile(System.IO.Path.Combine(assemblyLocation,
 IronPython.dll));

 Any ideas?
 -Jeff



 ___
 Users mailing list
 Users@lists.ironpython.com
 http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
   
 
 

___
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


[IronPython] Determine the classes/interfaces a Python implements

2008-12-15 Thread Jeff Slutter
I have a Python script that creates a class within it. This Python class
is derived off of a class, or interface, I made in C# - something like:

class MyClass(Test.MainForm.IScript):
...

Now, back in C#, I have gotten access to MyClass by:

object myclass = someScope.GetVariable(MyClass);

Is there a way to determine either:
a) what classes/interfaces MyClass implements OR
b) if it implements a specific class/interface

I want to know if the object myclass is supposed to implement
Test.MainForm.IScript or not.

I don't want to create an instance of MyClass as this would cause
problems, executing things I'm not ready to execute.

Also, related but not as important, implementing an interface (as above)
doesn't cause any compiler errors if I'm missing functions - is there a
way to enforce this?


Thanks,
Jeff
___
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


Re: [IronPython] Announcing IronPython 2.0

2008-12-11 Thread Jeff Slutter
Excuse me if this is known or not, but searching around has only turned
up articles/mailing lists posts that are two years old, but does
IronPython 2.0 support .NET attributes yet?

I've seen some examples of doing it using stub classes in C# and then
deriving from the in Python, but I'm not sure if this will work for me.
I need to put an Attribute on a class, or method, in Python that takes a
string argument. I can't think of a workaround to get this support.

For example, in C# the class would be:

class Foo
{
 [MyNameAttribute(Test name)]
 public void Method()
 {
   Console.Write(test);
 }
}

And the string Test name could be different for each use of
MyNameAttribute. I use it for internal registration purposes in my C# code.

How would I write this class in IP?



___
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


[IronPython] SourceCodeKind.Statements versus SourceCodeKind.InteractiveCode

2008-12-06 Thread Jeff Slutter
I'm working on getting an interactive script console into my
application. Information on how to do this is a bit hard to find but I
came across an app.py by Jim Hugunin showing how to do it with Silverlight.

I based my C# app around that and have something working pretty well.
I'm always using SourceCodeKind.InteractiveCode when compiling the
source snippets and it works great. But there is a situation where it
doesn't do what I expect.

If I pass in the following string:

a = 10 + 3;
print(a);

(note: two statements, separated with a newline)

It will give me an error saying that the 'print' is an unexpected token.

If I pass in the following string:

a = 10 + 3; print(a);

It works as expected (printing out 13).

It works fine if there are multiple lines for a statement like:

if( a  10 ):
 print(a);

If I use SourceCodeKind.Statements then all of the above works just
fine, but I don't get the nice things like the automatic print of the
returned value of the statement or the _ variable.

Is there a reason why InteractiveCode does things different like that? I
want to give a consistent interface to my users and I think they would
expect that they can give two statements at once (especially if they can
do it on the same line).

I can work around this to get what I want by building up my buffer one
line at a time and testing to see if it is a complete statement and
executing that, then continuing to feed in the next line, etc. But, if I
don't have to, I don't want to do that.

Any ideas?

Thanks
Jeff

___
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


Re: [IronPython] Calling functions in IronPython that don't really exist

2008-12-04 Thread Jeff Slutter
Ok, now I feel a little stupid. Thanks :)

I think it would be wise to drop the params object[] args and go with
just actually specifying the parameters to the constructor so I can take
advantage of type checking, etc. from Python.

In the constructor I'll have to save the instance of the class created
to my internal History list (for the undo, redo).

Thanks again!

Dino Viehland wrote:
 Do you mean you'd call it like AddNumbers().Do(3, 4)?

 This is really easy.  Make AddNumbers public, then do:

 import clr
 clr.AddReference('MyAssembly')
 import AddNumbers
 AddNumbers().Do(3, 4)

 If you really want to do AddNumbers(3, 4) then you'd just write it as:

 public class AddNumbers
 {
 public AddNumbers(params object[] args) {
  Do(args);
 }
 public string Do( params object[] args )
 {
 ..check args..
 ..add the two arguments..
 ..return the result as a string..
 }
 }

 And do the same:

 import clr
 clr.AddReference('MyAssembly')
 import AddNumbers
 AddNumbers(3, 4)

   

___
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


[IronPython] Merlin tutorial not working

2008-10-12 Thread Jeff Slutter
I'm going through the IronPython tutorial and I'm at the COM interop
part. I'm trying to get the Merlin example working but I can't get it
past a certain part.

First of all, I had to make some changes to get the example to work, as
it doesn't appear the tuple/out param stuff works the same as it does in
the tutorial.

Here is my code:

agentServer = AgentServerClass();
characterId = clr.Reference[int]();
reqId = clr.Reference[int]();   

agentServer.Load(Merlin.acs, characterId, reqId); 
myMerlinCharacter = clr.Reference[object]();

agentServer.GetCharacter(characterId.Value,myMerlinCharacter);
merlinCharacter = myMerlinCharacter.Value;

merlinCharacter.Show(0);

The exception happens when calling .GetCharacter. The error is:
An unhandled exception of type 'System.ArgumentException' occurred in
Microsoft.Scripting.Core.dll

Additional information: Could not convert argument 4294967295 for call
to GetCharacter.


Any ideas?
-Jeff
___
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com