[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", 
namespace IronPyCrypto.Hash
    public class MD4: IHash

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
    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!



-----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 

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. 
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 
> forgotten how .pyd files were used. 
> I don't know if IronPython supports this technique; I've only used 
> 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 
>> that I can create modules in c# for use in IronPython using the 
>> attribute, but these seem to be limited to __builtin__ modules, and 
also do 
>> not seem to support nested or heirarchical modules (like 
>> Any comments or pointers would be welcome.  See 
>> for more information on what I'm asking. 
>> Thanks, 
>> David 
>> -- 
>> View this message in context: 
>> 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/ 
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 
Users mailing list 
Users at lists.ironpython.com 


More information about the Ironpython-users mailing list