[IronPython] Programming a package in c# (heirarchical modules)

djlawler at aol.com djlawler at aol.com
Mon Mar 8 20:56:03 CET 2010


Thanks for taking the time to reply!  Let me see if I can be more clear:

If you create a standard class in c# to be used in IronPython....when 
you clr.AddReference and import it.  It will look like a Python class 
(not a module).  For instance, it will have __class__ defined.  
PyCrypto is a bit unusual in that it is a bunch of Python modules.  It 
has classes yes....but for instance, you call MD4.new() where MD4 is a 
module and new is a function and and it gives you back a MD4 class 
which is actually created in a pyd.

For example:
>>> from Crypto.Hash import MD4
>>> dir(MD4)
['__doc__', '__file__', '__name__', '__package__', 'digest_size', 'new']
>>> m = MD4.new()
>>> dir(m)
['copy', 'digest', 'hexdigest', 'update']
>>> type(MD4)
<type 'module'>
>>> type(m)
<type 'MD4'>
>>>

Sooooo.  If you want to make a module in c# (to be used in IronPython), 
then you need to do something like this:
using System;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
using Microsoft.Scripting.Runtime;
using IronPyCrypto.Util;

[assembly: PythonModule("IronPyCrypto_Hash_MD4", 
typeof(IronPyCrypto.Hash.MD4))]
namespace IronPyCrypto.Hash
{
    public class MD4: IHash
etc.

Setting the PythonModule attribute means that the c# class now shows up 
in IronPython as if it were a module!  All is wonderful....except....
any module that you create this way shows up in __builtins__ not as an 
external module.  Also, you cannot simulate a package (or at least I 
have not found a way to do so in c# only).  By that, I mean you can't 
include a '.' in the module name.  I have 'gotten around this' by doing 
an import in the __init__.py of PyCrypto.Hash like this:

import sys
if sys.platform == "cli":
    import clr
    clr.AddReference("IronPyCrypto.dll")
    import IronPyCrypto_Hash_MD2 as MD2
    import IronPyCrypto_Hash_MD4 as MD4

but this looks like a kludge to me.  I'm just wondering if I've run 
into something that was not considered by the developers?  Or, is there 
a way to do what I want but I'm not clever enough?  Or, do I just need 
to drink more wine and not worry about all these funny details :)
I have to say that even without the PythonModule attribute, everything 
still worked.  It's just if you do dir() and or help() on the objects 
generated by my dll things look very....odd!

Thanks!

David

-----Original Message-----
From: Michael Foord <fuzzyman at voidspace.org.uk>
To: Discussion of IronPython <users at lists.ironpython.com>
Sent: Mon, Mar 8, 2010 12:10 pm
Subject: Re: [IronPython] Programming a package in c# (heirarchical 
modules)


Hey guys, 
 
I'm probably missing the point - but why not just use .NET namespaces? 
 
Or even (did someone suggest this) write the raw classes in C# and then 
compose them into the correct package heirarchy with a pure Python 
wrapper. This latter approach is probably the one I would take. 
 
Michael 
 
On 08/03/2010 17:05, Jeff Hardy wrote: 
> Hi David, 
> I looked into this some more, and I made some errors in my SO answer 
> that I'll correct later (sorry!). 
> 
> When building PyCrypto, it builds&  installs .pyd files into the 
> necessary folders; I assumed that it built one monolithic DLL (a 
> strategy used by other modules), but it doesn't. When looking for 
> Crypto.Hash.MD4, CPython will find and load Crypto\Hash\MD4.pyd. I 
had 
> forgotten how .pyd files were used. 
> 
> I don't know if IronPython supports this technique; I've only used 
the 
> monolithic DLL approach, so your current solution may be the correct 
> one. 
> 
> - Jeff 
> 
> On Sun, Mar 7, 2010 at 10:22 AM, djlawler<djlawler at aol.com>  wrote: 
>    >> I've been working on a port of PyCrypto to IronPython.  Are 
there any 
>> pointers on how to properly program an external 'package' that has 
>> heirarchical modules in it?  I know that this is easy in python.  I 
know 
>> that I can create modules in c# for use in IronPython using the 
PythonModule 
>> attribute, but these seem to be limited to __builtin__ modules, and 
also do 
>> not seem to support nested or heirarchical modules (like 
Crypto.Hash.MD4). 
>> 
>> Any comments or pointers would be welcome.  See 
>> 
http://stackoverflow.com/questions/2365661/how-to-create-a-package-in-c-for-use-in-ironpython 
>> for more information on what I'm asking. 
>> 
>> Thanks, 
>> David 
>> -- 
>> View this message in context: 
http://old.nabble.com/Programming-a-package-in-c--%28heirarchical-modules%29-tp27809285p27809285.html 
>> Sent from the IronPython mailing list archive at Nabble.com. 
>> 
>> _______________________________________________ 
>> Users mailing list 
>> Users at lists.ironpython.com 
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com 
>> 
>>      > _______________________________________________ 
> Users mailing list 
> Users at lists.ironpython.com 
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com 
>     
-- http://www.ironpythoninaction.com/ 
http://www.voidspace.org.uk/blog 
 
READ CAREFULLY. By accepting and reading this email you agree, on 
behalf of your employer, to release me from all obligations and waivers 
arising from any and all NON-NEGOTIATED agreements, licenses, 
terms-of-service, shrinkwrap, clickwrap, browsewrap, confidentiality, 
non-disclosure, non-compete and acceptable use policies (”BOGUS 
AGREEMENTS”) that I have entered into with your employer, its partners, 
licensors, agents and assigns, in perpetuity, without prejudice to my 
ongoing rights and privileges. You further represent that you have the 
authority to release me from any BOGUS AGREEMENTS on behalf of your 
employer. 
 
_______________________________________________ 
Users mailing list 
Users at lists.ironpython.com 
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com 

  



More information about the Ironpython-users mailing list