Changeset 102
- Timestamp:
- 02/22/07 18:29:52 (2 years ago)
- Files:
-
- trunk/html_doc/basics.html (modified) (1 diff)
- trunk/html_doc/celerid.html (modified) (1 diff)
- trunk/html_doc/class_wrapping.html (modified) (5 diffs)
- trunk/html_doc/inherit.html (modified) (3 diffs)
- trunk/html_doc/struct_wrapping.html (modified) (2 diffs)
- trunk/raw_html/basics.html (modified) (1 diff)
- trunk/raw_html/celerid.html (modified) (1 diff)
- trunk/raw_html/class_wrapping.html (modified) (5 diffs)
- trunk/raw_html/inherit.html (modified) (3 diffs)
- trunk/raw_html/struct_wrapping.html (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/html_doc/basics.html
r86 r102 51 51 <p>It does little more than call <a href="http://docs.python.org/api/allocating-objects.html">Py_InitModule</a> and return the new module object. This object is also available via the <code>Pyd_Module_p</code> property once you've called <code>module_init</code>.</p> 52 52 53 <p>Due to the way in which Pyd implements function and class wrapping, any calls to <code>def</code> must occur <em>before</em> the call to <code>module_init</code>, and any calls to <code> finalize_class</code> must occur <em>after</em> the call. I know this seems like a rather arbitrary rule, but it is important. Calls to <code>def</code> in the wrong place will simply be ignored, and calls to <code>finalize_class</code> in the wrong place will throw an assert.</p>53 <p>Due to the way in which Pyd implements function and class wrapping, any calls to <code>def</code> must occur <em>before</em> the call to <code>module_init</code>, and any calls to <code>wrap_class</code> must occur <em>after</em> the call. I know this seems like a rather arbitrary rule, but it is important. Calls to <code>def</code> in the wrong place will simply be ignored, and calls to <code>wrap_class</code> in the wrong place will throw an assert.</p> 54 54 55 55 <p><code>PydMain</code> will catch any D exception that is thrown from inside it, and <a href="except_wrapping.html">safely pass that exception to Python</a>.</p> trunk/html_doc/celerid.html
r86 r102 58 58 <dt><code>raw_only</code></dt> <dd>This flag defaults to <code>False</code>. When <code>True</code>, it supresses the compilation and linkage of Pyd, StackThreads, and meta. This is useful if you only want to write a raw Python/C extension without the overhead of Pyd and its auxiliary packages. This is equivalent to specifying <code>False</code> to the next four flags.</dd> 59 59 <dt><code>with_pyd</code></dt> <dd>This flag defaults to <code>True</code>. When <code>False</code>, it supresses the compilation and linkage of Pyd. This is useful if you want to write a raw Python/C extension and don't want the overhead of compiling Pyd.</dd> 60 <dt><code>with_st</code></dt> <dd>This flag defaults to <code>True</code>. When <code>False</code>, it supresses the compilation and linkage of StackThreads. Pyd uses StackThreads for its iteration wrapping support. By setting this to <code>False</code>, opApply wrapping, <code> wrapped_class.iter</code>, and <code>wrapped_class.alt_iter</code> will be unavailable. If <code>with_pyd</code> and this are <code>True</code>, then the <code>Pyd_with_StackThreads</code> version flag will be defined (which is used internally by Pyd). <b>Important note:</b> StackThreads does not currently work with GDC! CeleriD will always set this flag to <code>False</code> when using GDC! This means that opApply wrapping is not available on Linux at this time.</dd>60 <dt><code>with_st</code></dt> <dd>This flag defaults to <code>True</code>. When <code>False</code>, it supresses the compilation and linkage of StackThreads. Pyd uses StackThreads for its iteration wrapping support. By setting this to <code>False</code>, opApply wrapping, <code>Iter</code>, and <code>AltIter</code> will be unavailable. If <code>with_pyd</code> and this are <code>True</code>, then the <code>Pyd_with_StackThreads</code> version flag will be defined (which is used internally by Pyd). <b>Important note:</b> StackThreads does not currently work with GDC! CeleriD will always set this flag to <code>False</code> when using GDC! This means that opApply wrapping is not available on Linux at this time.</dd> 61 61 <dt><code>with_meta</code></dt> <dd>This flag defaults to <code>True</code>. When <code>False</code>, it supresses the compilation and linkage of <code>meta</code> (Pyd's metaprogramming package). Because Pyd depends on meta, an exception will be raised if <code>with_pyd</code> is <code>True</code> and this is not.</dd> 62 62 <dt><code>with_main</code></dt> <dd>This flag defaults to <code>True</code>. When <code>False</code>, it supresses the use of the "magic" <code>PydMain</code> function. (Instead, users must manually declare a C-style <code>init</code> function.) Do not use this unless you know what you are doing. If <code>with_pyd</code> is <code>False</code>, this will silently be set to <code>False</code> as well. <code>PydMain</code> can only be used if Pyd itself is in use.</dd> trunk/html_doc/class_wrapping.html
r91 r102 29 29 <h1>Class wrapping</h1> 30 30 31 <p>Exposing D classes to Python is easy! The heart of Pyd's class wrapping features is the <code>wrap ped_class</code> template struct:</p>31 <p>Exposing D classes to Python is easy! The heart of Pyd's class wrapping features is the <code>wrap_class</code> function template:</p> 32 32 33 <p><code> struct wrapped_class(<span class="t_arg">T</span>, char[] <span class="t_arg">classname</span> = symbolnameof!(T));</code></p>33 <p><code>void wrap_class(<span class="t_arg">T</span>, char[] <span class="t_arg">classname</span> = symbolnameof!(T), <span class="t_arg">Params</span>...) ();</code></p> 34 34 <ul> 35 35 <li><span class="t_arg">T</span> is the class being wrapped.</li> 36 <li><span class="t_arg">classname</span> is the name of the class as it will appear in Python.</li> 36 <li><span class="t_arg">classname</span> is the name of the class as it will appear in Python. It defaults to the name of the D class. If you are wrapping an instance of a class template, you will have to provide this explicitly.</li> 37 <li><span class="t_arg">Params</span> is a series of struct types (defined below), which define the various members of the class.</li> 37 38 </ul> 38 39 39 <p>To expose the constructors, methods, and properties of the class, <code>wrapped_class</code> provides a series of template member functions.</p> 40 <p>Calls to <code>wrap_class</code> must occur <em>after</em> calling <code>module_init</code>.</p> 41 42 <p>To expose the constructors, methods, and properties of the class, you must pass <code>wrap_class</code> instantiations of these struct templates.</p> 40 43 41 44 <dl> 42 <dt><code>st atic void def(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">fn_t</span> = typeof(&fn)) (char[] <span class="arg">docstring</span>="");</code></dt>43 <dd>This wraps a method of the class. It functions exactlylike the <code>def</code> function used to <a href="func_wrapping.html">wrap regular functions</a>, with one very important difference: There is no support for default arguments. (This is a side-effect of the fact that you cannot call an alias of a method in D, and delegates do not understand default arguments.)</dd>45 <dt><code>struct Def(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">fn_t</span> = typeof(&fn));</code></dt> 46 <dd>This wraps a method of the class. It functions very much like the <code>def</code> function used to <a href="func_wrapping.html">wrap regular functions</a>, with one very important difference: There is no support for default arguments. (This is a side-effect of the fact that you cannot call an alias of a method in D, and delegates do not understand default arguments.)</dd> 44 47 45 <dt><code>st atic void static_def(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">fn_t</span> = typeof(&fn), uint <span class="t_arg">MIN_ARGS</span> = minArgs!(fn)) (char[] <span class="arg">docstring</span>="");</code></dt>48 <dt><code>struct StaticDef(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">fn_t</span> = typeof(&fn), uint <span class="t_arg">MIN_ARGS</span> = minArgs!(fn));</code></dt> 46 49 <dd>This wraps a static member function of the class. It also functions exactly like the <code>def</code> function used to wrap regular functions, and even includes support for default arguments.</dd> 47 50 48 <dt><code>st atic void prop(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), bool <span class="t_arg">RO</span> = false) (char[] <span class="arg">docstring</span>="");</code></dt>51 <dt><code>struct Property(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), bool <span class="t_arg">RO</span> = false);</code></dt> 49 52 <dd>This wraps a property. See the examples below for more details. 50 53 <ul> … … 52 55 <li><span class="t_arg">name</span> is the name of the property as it will appear in Python. As with <code>def</code>, <code>prop</code> will attempt to derive this automatically.</li> 53 56 <li><span class="t_arg">RO</span> specifies whether this is a <i>read-only</i> property. If true, it will only wrap the "get" form of the property. If false, it will wrap both the "get" and "set" forms. <i>(This is a little hackish, and I will probably try to make this detection more automatic in the future. It also means it cannot support a property that only has a "set" form.)</i></li> 54 <li><span class="arg">docstring</span> is the property's docstring. As usual, note that this is a regular function argument, and not a template argument.</li>55 57 </ul> 56 58 </dd> 57 59 58 <dt><code>st atic void init(<span class="t_arg">C</span> ...) ();</code></dt>59 <dd>This allows you to expose the class's constructors to Python. If the class provides a zero-argument constructor, there is no need to specify it; it is always available. Each element of <span class="t_arg">C</span> should be a function type. Each function type should correspond to a constructor. (That is, the arguments to the function should be the same asarguments to the class constructor. The return type is ignored.) There is an additional limitation at this time: No two constructors may have the same number of arguments. Pyd will always attempt to call the first constructor with the right number of arguments. If you wish to support a constructor with default arguments, you must specify each possible constructor call as a different template argument to this function. The examples show a few uses of the <code>init</code> function.</dd>60 <dt><code>struct Init(<span class="t_arg">C</span> ...);</code></dt> 61 <dd>This allows you to expose the class's constructors to Python. If the class provides a zero-argument constructor, there is no need to specify it; it is always available. Each element of <span class="t_arg">C</span> should be a function type. Each function type should correspond to a constructor. (That is, the arguments to the function type should be the same as the arguments to the class constructor. The return type is ignored.) There is an additional limitation at this time: No two constructors may have the same number of arguments. Pyd will always attempt to call the first constructor with the right number of arguments. If you wish to support a constructor with default arguments, you must specify each possible constructor call as a different template argument to this function. The examples show a few uses of the <code>init</code> function.</dd> 60 62 61 <dt><code>static void parent(<span class="t_arg">Parent</span>) ();</code></dt> 62 <dd>This allows the user to manually specify a class as this class's parent. This is intended for a very specific purpose (related to how Pyd handles <a href="inherit.html">inheritance</a>), and should not be used heedlessly. If a class's parent was previously wrapped, then Pyd will detect this and set up a parent-child relationship automatically, in which case it is not neccessary to specify this.</dd> 63 64 <dt><code>static void hide();</code></dt> 65 <dd>Causes this class to be wrapped, but not actually directly exposed to Python. This can be useful if you want to return instances of a class without allowing Python code to instantiate them. This is mainly used when handling <a href="inherit.html">inheritance</a>.</dd> 66 67 <dt><code>static void iter(<span class="t_arg">iter_t</span>) ();</code></dt> 63 <dt><code>struct Iter(<span class="t_arg">iter_t</span>);</code></dt> 68 64 <dd>This allows the user to specify a different overload of opApply than the default. (The default is always the one that is lexically first.) The <span class="t_arg">iter_t</span> argument should be the type of the delegate that forms the argument to opApply. This might be e.g. <code>int delegate(inout int)</code>. Don't forget the <code>inout</code> modifiers! (This is not available in Linux; see the note below on opApply wrapping.)</dd> 69 65 70 <dt><code>st atic void alt_iter(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">iter_t</span> = <i>implementationDetail</i>) (char[] <span class="arg">docstring</span>="");</code></dt>66 <dt><code>struct AltIter(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">iter_t</span> = <i>implementationDetail</i>);</code></dt> 71 67 <dd>This wraps alternate iterator methods as Python methods that return iterator objects. The wrapped methods should have a signature like that of opApply. (In other words, they should be methods intended to be used with D's ability to iterate over delgates.) The <span class="t_arg">iter_t</span> argument should be the type of the delegate argument to the method. This will usually be derived automatically. (This is not available in Linux; see the note below on opApply wrapping.) 72 68 </dd> 73 69 </dl> 74 75 <p>Once you have called all of the member functions of <code>wrapped_class</code> that you wish to, you must issue a call to <code>finalize_class</code>.</p>76 77 <p><code>void finalize_class(<span class="t_arg">CLS</span>) (<span class="t_arg">CLS</span> <span class="arg">cls</span>, char[] <span class="arg">docstring</span>="");</code></p>78 79 <p>This does some final initialization of the class and then registers it with Python. Unlike calls to <a href="func_wrapping.html"><code>def</code></a>, calls to <code>finalize_class</code> must occur <em>after</em> calling <code>module_init</code>. The <span class="arg">cls</span> function argument should be an instance of <code>wrapped_class</code>.</p>80 70 81 71 <p>If you ever wish to check whether a given class has been wrapped, Pyd helpfully registers all wrapped classes with the <code>is_wrapped</code> template, which is just a templated <code>bool</code>:</p> … … 96 86 <p>At the moment, only the following operator overloads are supported:</p> 97 87 98 <p><code>op Add, opSub, opMul, opDiv, opMod, opAnd, opOr, opXor, opShl, opShr, opCat, opAddAssign, opSubAssign, opMulAssign, opDivAssign, opModAssign, opAndAssign, opOrAssign, opXorAssign, opShlAssign, opShrAssign, opCatAssign, opIn_r, opCmp, opCall, opApply, opIndex, opIndexAssign, opSlice, opSliceAssign</code></p>88 <p><code>opNeg, opPos, opCom, opAdd, opSub, opMul, opDiv, opMod, opAnd, opOr, opXor, opShl, opShr, opCat, opAddAssign, opSubAssign, opMulAssign, opDivAssign, opModAssign, opAndAssign, opOrAssign, opXorAssign, opShlAssign, opShrAssign, opCatAssign, opIn_r, opCmp, opCall, opApply, opIndex, opIndexAssign, opSlice, opSliceAssign</code></p> 99 89 100 <p>Missing from this list are <code>opUShr</code> and <code>opUShrAssign</code>. Python does not have an unsigned right-shift operator, so these operator overloads are not supported. (You may still wrap them with a normal method using <code> wrapped_class.def</code>, of course.) Also missing from the list is <code>opApplyReverse</code>. This must be wrapped explicitly with <code>wrapped_class.alt_iter</code>.</p>90 <p>Missing from this list are <code>opUShr</code> and <code>opUShrAssign</code>. Python does not have an unsigned right-shift operator, so these operator overloads are not supported. (You may still wrap them with a normal method using <code>Def</code>, of course.) Also missing from the list is <code>opApplyReverse</code>. This must be wrapped explicitly with <code>AltIter</code>.</p> 101 91 102 92 <p>Also missing from the list is <code>opAssign</code>. Python has strict reference semantics for its objects, so overloading the assignment operator is not possible. You must explicitly wrap <code>opAssign</code> with a regular method.</p> 103 93 104 <p>Additionally, if a class provides a <code>length</code> property, Pyd will automatically make it available via Python's built-in function <code>len</code> and the special <code>__len__</code> method. You may still wrap it with <code> prop</code> or <code>def</code> if you wish it to be available as a normal property or method.</p>94 <p>Additionally, if a class provides a <code>length</code> property, Pyd will automatically make it available via Python's built-in function <code>len</code> and the special <code>__len__</code> method. You may still wrap it with <code>Property</code> or <code>Def</code> if you wish it to be available as a normal property or method.</p> 105 95 106 96 <p><b>Notes on wrapped operators</b></p> … … 140 130 <p>We would expose this class to Python by putting this code in <code>PydMain</code> after the call to <code>module_init</code>:</p> 141 131 142 <pre class="code"><span class="comment">// Make an instance of wrapped_class</span> 143 wrapped_class!(Foo) f; 144 <span class="comment">// Wrap the "foo" method</span> 145 f.def!(Foo.foo); 146 <span class="comment">// Wrap the "i" property</span> 147 f.prop!(Foo.i); 148 <span class="comment">// Wrap the constructors.</span> 149 f.init!(<span class="keyword">void function</span>(<span class="keyword">int</span>), <span class="keyword">void function</span>(<span class="keyword">int</span>, <span class="keyword">int</span>)); 150 finalize_class(f);</pre> 132 <pre class="code"><span class="comment">// Call wrap_class</span> 133 wrap_class!( 134 Foo, 135 <span class="comment">// Wrap the "foo" method</span> 136 Def!(Foo.foo), 137 <span class="comment">// Wrap the "i" property</span> 138 Property!(Foo.i), 139 <span class="comment">// Wrap the constructors.</span> 140 Init!(<span class="keyword">void function</span>(<span class="keyword">int</span>), <span class="keyword">void function</span>(<span class="keyword">int</span>, <span class="keyword">int</span>)) 141 );</pre> 151 142 152 143 <p>Now we can use this type from within Python like any other type.</p> 153 144 154 <pre class="code">>>> from testmodule importFoo145 <pre class="code">>>> <span class="keyword">from</span> testmodule <span class="keyword">import</span> Foo 155 146 >>> f = Foo() 156 147 >>> f.i 157 148 0 158 149 >>> f.i = 20 159 >>> f.foo( "Hello! i is ")150 >>> f.foo(<span class="string">"Hello! i is "</span>) 160 151 Hello! i is 20 161 152 >>> f = Foo(10, 10) … … 168 159 >>> e.i 169 160 50 170 >>> # We can even subclass our D type171 >>> classMyFoo(Foo):172 ... defbar(self):173 ... print "Hey, i+3 is", self.i + 3161 >>> <span class="comment"># We can even subclass our D type</span> 162 >>> <span class="keyword">class</span> MyFoo(Foo): 163 ... <span class="keyword">def</span> bar(self): 164 ... <span class="keyword">print</span> <span class="string">"Hey, i+3 is"</span>, self.i + 3 174 165 ... 175 166 >>> h = MyFoo(3) trunk/html_doc/inherit.html
r91 r102 44 44 <p>These would be exposed to Python by putting this code in <code>PydMain</code> after the call to <code>module_init</code>:</p> 45 45 46 <pre class="code">wrapped_class!(Base) b; 47 b.def!(Base.foo); 48 b.def!(Base.bar); 49 finalize_class(b); 46 <pre class="code">wrap_class!( 47 Base, 48 Def!(Base.foo), 49 Def!(Base.bar), 50 ); 50 51 51 wrapped_class!(Derived) d; 52 d.def!(Derived.foo); 53 finalize_class(d);</pre> 52 wrap_class!( 53 Derived, 54 Def!(Derived.foo), 55 );</pre> 54 56 55 57 <p>When used in Python, we get the expected behavior:</p> … … 68 70 Base.bar</pre> 69 71 70 <p> There is one weakness in the default behavior. Take a function like the following:</p>72 <p>Polymorphic behavior is also automatically taken care of. Take a function like the following:</p> 71 73 72 74 <pre class="code"><span class="keyword">void</span> polymorphic_call(Base b) { … … 76 78 <p>And in Python:</p> 77 79 78 <pre class="code"> <span class="keyword">class</span> PyClass(Base):79 <span class="keyword">def</span> foo(self):80 <span class="keyword">print</span> <span class="string">"PyClass.foo"</span>81 80 <pre class="code">>>> <span class="keyword">class</span> PyClass(Base): 81 ... <span class="keyword">def</span> foo(self): 82 ... <span class="keyword">print</span> <span class="string">"PyClass.foo"</span> 83 ... 82 84 >>> p = PyClass() 83 85 >>> polymorphic_call(p) 84 Base.foo</pre>85 86 <p>Optimally, we would want <code>polymorphic_call</code> to call PyClass.foo. This requires some additional work on the D side of things. To get this behavior, then rather than expose Base directly, we must expose a wrapper class:</p>87 88 <pre class="code"><span class="keyword">class</span> BaseWrap : Base {89 <span class="keyword">mixin</span> OverloadShim;90 <span class="keyword">void</span> foo() {91 get_overload(&<span class="keyword">super</span>.foo, <span class="string">"foo"</span>);92 }93 <span class="keyword">void</span> bar() {94 get_overload(&<span class="keyword">super</span>.bar, <span class="string">"bar"</span>);95 }96 }</pre>97 98 <p>The <code>OverloadShim</code> template has but a single member, the <code>get_overload</code> function.</p>99 100 <dl>101 <dt><code>ReturnType!(dg_t) get_overload(<span class="t_arg">dg_t</span>, <span class="t_arg">T ...</span>) (dg_t <span class="arg">dg</span>, char[] <span class="arg">name</span>, T <span class="arg">t</span>);</code></dt>102 <dd><ul>103 <li><span class="arg">dg</span> should be a delegate to the parent class's method.</li>104 <li><span class="arg">name</span> should be the name of the method as Python understands it to be. <i>(There's no efficient way to derive this automatically based on only the delegate.)</i></li>105 <li><span class="arg">t</span> is a tuple argument. These arguments will be passed on to the actual function call, be it the parent class's implementation or a Python subclass's implementation.</li>106 </ul></dd>107 </dl>108 109 <p><code>get_overload</code> returns whatever the method does.</p>110 111 <p>Now, we must replace the old wrapping of Base with this:</p>112 113 <pre class="code">wrapped_class!(BaseWrap, <span class="string">"Base"</span>) w;114 w.def!(BaseWrap.foo);115 w.def!(BaseWrap.bar);116 finalize_class(w);</pre>117 118 <p>Now our subclass will perform just like we expect:</p>119 120 <pre class="code">>>> p = PyClass()121 >>> polymorphic_call(p)122 86 PyClass.foo</pre> 123 124 <p>However, BaseWrap has no particular relationship to Derived. You may remember that Derived overloads <tt>bar</tt> but not <tt>foo</tt>. When we wrapped Derived in <tt>PydMain</tt>, we specified the <tt>foo</tt> overload but not the <tt>bar</tt> overload. Because Derived's parent class is no longer wrapped, Pyd no longer has any way to know about the <tt>bar</tt> method of the Derived class.</tt></p>125 126 <p>The solution is to explicitly tell Pyd that Derived's parent is BaseWrap. Furthermore, it is probably best to go the extra mile, by wrapping an <tt>OverloadShim</tt> subclass of Derived (call it DerivedWrap), and telling Pyd that BaseWrap is <em>its</em> parent. Additionally, the original <tt>Base</tt> and <tt>Derived</tt> classes should still be wrapped, in the event that functions return instances of them to Python, but should not actually be exposed to Python. The complete solution ends up looking like this:</p>127 128 <pre class="code"><span class="keyword">import</span> pyd.pyd;129 <span class="keyword">import</span> std.stdio;130 131 <span class="keyword">class</span> Base {132 <span class="keyword">void</span> foo() { writefln(<span class="string">"Base.foo"</span>); }133 <span class="keyword">void</span> bar() { writefln(<span class="string">"Base.bar"</span>); }134 }135 136 <span class="keyword">class</span> Derived : Base {137 <span class="keyword">void</span> foo() { writefln(<span class="string">"Derived.foo"</span>); }138 }139 140 <span class="keyword">class</span> BaseWrap : Base {141 <span class="keyword">mixin</span> OverloadShim;142 <span class="keyword">void</span> foo() {143 get_overload(&<span class="keyword">super</span>.foo, <span class="string">"foo"</span>);144 }145 <span class="keyword">void</span> bar() {146 get_overload(&<span class="keyword">super</span>.bar, <span class="string">"bar"</span>);147 }148 }149 150 <span class="keyword">class</span> DerivedWrap : Derived {151 <span class="keyword">mixin</span> OverloadShim;152 <span class="keyword">void</span> foo() {153 get_overload(&<span class="keyword">super</span>.foo, <span class="string">"foo"</span>);154 }155 }156 157 <span class="keyword">extern</span> (C) <span class="keyword">void</span> PydMain() {158 module_init();159 160 wrapped_class!(Base) b;161 w.hide();162 w.def!(Base.foo);163 w.def!(Base.bar);164 finalize_class(w);165 166 wrapped_class!(Derived) d;167 d.hide();168 d.def!(Derived.foo);169 finalize_class(d);170 171 wrapped_class!(BaseWrap, <span class="string">"Base"</span>) bw;172 bw.def!(BaseWrap.foo);173 bw.def!(BaseWrap.bar);174 finalize_class(bw);175 176 wrapped_class!(DerivedWrap, <span class="string">"Derived"</span>) dw;177 dw.parent!(BaseWrap);178 dw.def!(DerivedWrap.foo);179 finalize_class(dw);180 }</pre>181 182 <p><i>(I recognize that this is astoundingly ugly. However, it is the best solution I can come up with without resorting to code generation.)</i></p>183 184 <p>The <a href="http://dsource.org/projects/pyd/browser/trunk/examples/inherit/inherit.d"><tt>inherit</tt> example</a> in the Pyd distribution provides a more complete version of this example, including how wrapper code should handle constructors.</p>185 87 186 88 <p><i>(TODO: Add support for interfaces and abstract classes.)</i></p> trunk/html_doc/struct_wrapping.html
r86 r102 31 31 <p>Wrapping D's structs is similar to wrapping classes. In fact, many of the operations are identical.</p> 32 32 33 <p><code> struct wrapped_struct(<span class="t_arg">T</span>, char[] <span class="t_arg">structname</span> = symbolnameof!(T));</code></p>33 <p><code>void wrap_struct(<span class="t_arg">T</span>, char[] <span class="t_arg">structname</span> = symbolnameof!(T), <span class="t_arg">Params</span>...) ();</code></p> 34 34 <ul> 35 35 <li><span class="t_arg">T</span> is the struct being wrapped.</li> 36 36 <li><span class="t_arg">structname</span> is the name of the struct as it will appear in Python.</li> 37 <li><span class="t_arg">Params</span> is a series of struct types (defined below), which define the various members of the struct.</li> 37 38 </ul> 38 39 39 <p>To expose the data members, member functions, and properties of the class, <code>wrapped_struct</code> provides a series of template member functions.</p> 40 <p>As with calls to <a href="class_wrapping.html"><code>wrap_class</code></a>, calls to <code>wrap_struct</code> must occur <em>after</em> calling <code>module_init</code>.</p> 41 42 <p>To expose the data members, member functions, and properties of the struct, you must pass a series of struct template instantiations to <code>wrap_struct</code>.</p> 40 43 41 44 <dl> 42 <dt><code>st atic void member(<span class="t_arg">M</span>, size_t <span class="t_arg">offset</span>, char[] <span class="t_arg">name</span>) (char[] <span class="arg">docstring</span>="");</code></dt>43 <dd>This exposes a data member of the struct to Python. <span class="t_arg">M</span> is the type of the member, and must be a <a href="conversion.html">convertible type</a>. <span class="t_arg">offset</span> is the offset (in bytes) of the member in the struct. <span class="t_arg">name</span> is the name of the data member as it will be used in Python. <i>(Optimally, one would simply be able to pass an alias to the member, or at worst an alias and a name, but DMD currently has some issues with this.)</i></dd>45 <dt><code>struct Member(char[] <span class="t_arg">realname</span>, char[] <span class="t_arg">name</span>=realname);</code></dt> 46 <dd>This exposes a data member of the struct to Python. The member must be a <a href="conversion.html">convertible type</a>. The <span class="t_arg">realname</span> is the member's actual name. <span class="t_arg">name</span> is the name of the data member as it will be used in Python. This defaults to <span class="t_arg">realname</span>.</dd> 44 47 45 <dt><code>st atic void def(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">fn_t</span> = typeof(&fn)) (char[] <span class="arg">docstring</span>="");</code></dt>46 <dd>This wraps a member function of the struct. It functions exactly like the <code>def</code> functionused to <a href="class_wrapping.html">wrap class methods</a>, including the lack of support for default arguments.</dd>48 <dt><code>struct Def(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">fn_t</span> = typeof(&fn));</code></dt> 49 <dd>This wraps a member function of the struct. It is in fact exactly the same <code>Def</code> struct template used to <a href="class_wrapping.html">wrap class methods</a>, including the lack of support for default arguments.</dd> 47 50 48 <dt><code>st atic void static_def(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">fn_t</span> = typeof(&fn), uint <span class="t_arg">MIN_ARGS</span> = minArgs!(fn)) (char[] <span class="arg">docstring</span>="");</code></dt>49 <dd>This wraps a static member function of the struct. It functions exactly like the <code>static_def</code> functionused to wrap static class member functions, and also includes support for default arguments.</dd>51 <dt><code>struct StaticDef(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">fn_t</span> = typeof(&fn), uint <span class="t_arg">MIN_ARGS</span> = minArgs!(fn));</code></dt> 52 <dd>This wraps a static member function of the struct. It is the same <code>StaticDef</code> struct template used to wrap static class member functions, and also includes support for default arguments.</dd> 50 53 51 <dt><code>st atic void prop(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), bool <span class="t_arg">RO</span> = false) (char[] <span class="arg">docstring</span>="");</code></dt>52 <dd>This wraps a property. It is identical to the <code>prop</code> functionused to <a href="class_wrapping.html">wrap class properties</a>.</dd>54 <dt><code>struct Property(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), bool <span class="t_arg">RO</span> = false);</code></dt> 55 <dd>This wraps a property. It is the same <code>Property</code> struct template used to <a href="class_wrapping.html">wrap class properties</a>.</dd> 53 56 54 <dt><code>st atic void iter(<span class="t_arg">iter_t</span>) ();</code></dt>55 <dd>This allows the user to specify a different overload of opApply than the default. (The default is always the one that is lexically first.) It is identical to the <code>iter</code> functionused in <a href="class_wrapping.html">class wrapping</a>.</dd>57 <dt><code>struct Iter(<span class="t_arg">iter_t</span>);</code></dt> 58 <dd>This allows the user to specify a different overload of opApply than the default. (The default is always the one that is lexically first.) It is the same <code>Iter</code> struct template used in <a href="class_wrapping.html">class wrapping</a>.</dd> 56 59 57 <dt><code>st atic void alt_iter(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">iter_t</span> = <i>implementationDetail</i>) (char[] <span class="arg">docstring</span>="");</code></dt>58 <dd>This wraps alternate iterator methods as Python methods that return iterator objects. It is is identical to the <code>alt_iter</code> functionused in <a href="class_wrapping.html">class wrapping</a>.</dd>60 <dt><code>struct AltIter(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">iter_t</span> = <i>implementationDetail</i>);</code></dt> 61 <dd>This wraps alternate iterator methods as Python methods that return iterator objects. It is the same <code>AltIter</code> struct template used in <a href="class_wrapping.html">class wrapping</a>.</dd> 59 62 </dl> 60 63 61 <p><i>(Future enhancements: Support for struct ctors.)</i></p> 62 63 <p>Once you have called all of the member functions of <code>wrapped_struct</code> that you wish to, you must issue a call to <code>finalize_struct</code>.</p> 64 65 <p><code>void finalize_struct(<span class="t_arg">S</span>) (<span class="t_arg">S</span> <span class="arg">s</span>, char[] <span class="arg">docstring</span>="");</code></p> 66 67 <p>This does some final initialization of the type and then registers it with Python. As with calls to <a href="class_wrapping.html"><code>finalize_class</code></a>, calls to <code>finalize_struct</code> must occur <em>after</em> calling <code>module_init</code>. The <span class="arg">s</span> function argument should be an instance of <code>wrapped_struct</code>.</p> 64 <!--<p><i>(Future enhancements: Support for struct ctors.)</i></p>--> 68 65 69 66 <p>The <code>is_wrapped</code> template is available for wrapped structs, just like it is for wrapped classes.</p> … … 75 72 <p>Support for operator overloading in structs is identical to that available for classes.</p> 76 73 74 <h3><a class="anchor" name="inherit">Inheritance</a></h3> 75 76 <p>D does not support struct inheritance. Therefore, Pyd does not provide any support for struct inheritance. However, the Python type wrapping the D struct can be subclassed from within Python. Users should not expect polymorphic behavior if they attempt to pass instances of any subclasses back to D.</p> 77 77 78 <h3><a class="anchor" name="examples">Examples</a></h3> 78 79 trunk/raw_html/basics.html
r64 r102 35 35 <p>It does little more than call <a href="http://docs.python.org/api/allocating-objects.html">Py_InitModule</a> and return the new module object. This object is also available via the <code>Pyd_Module_p</code> property once you've called <code>module_init</code>.</p> 36 36 37 <p>Due to the way in which Pyd implements function and class wrapping, any calls to <code>def</code> must occur <em>before</em> the call to <code>module_init</code>, and any calls to <code> finalize_class</code> must occur <em>after</em> the call. I know this seems like a rather arbitrary rule, but it is important. Calls to <code>def</code> in the wrong place will simply be ignored, and calls to <code>finalize_class</code> in the wrong place will throw an assert.</p>37 <p>Due to the way in which Pyd implements function and class wrapping, any calls to <code>def</code> must occur <em>before</em> the call to <code>module_init</code>, and any calls to <code>wrap_class</code> must occur <em>after</em> the call. I know this seems like a rather arbitrary rule, but it is important. Calls to <code>def</code> in the wrong place will simply be ignored, and calls to <code>wrap_class</code> in the wrong place will throw an assert.</p> 38 38 39 39 <p><code>PydMain</code> will catch any D exception that is thrown from inside it, and <a href="except_wrapping.html">safely pass that exception to Python</a>.</p> trunk/raw_html/celerid.html
r64 r102 42 42 <dt><code>raw_only</code></dt> <dd>This flag defaults to <code>False</code>. When <code>True</code>, it supresses the compilation and linkage of Pyd, StackThreads, and meta. This is useful if you only want to write a raw Python/C extension without the overhead of Pyd and its auxiliary packages. This is equivalent to specifying <code>False</code> to the next four flags.</dd> 43 43 <dt><code>with_pyd</code></dt> <dd>This flag defaults to <code>True</code>. When <code>False</code>, it supresses the compilation and linkage of Pyd. This is useful if you want to write a raw Python/C extension and don't want the overhead of compiling Pyd.</dd> 44 <dt><code>with_st</code></dt> <dd>This flag defaults to <code>True</code>. When <code>False</code>, it supresses the compilation and linkage of StackThreads. Pyd uses StackThreads for its iteration wrapping support. By setting this to <code>False</code>, opApply wrapping, <code> wrapped_class.iter</code>, and <code>wrapped_class.alt_iter</code> will be unavailable. If <code>with_pyd</code> and this are <code>True</code>, then the <code>Pyd_with_StackThreads</code> version flag will be defined (which is used internally by Pyd). <b>Important note:</b> StackThreads does not currently work with GDC! CeleriD will always set this flag to <code>False</code> when using GDC! This means that opApply wrapping is not available on Linux at this time.</dd>44 <dt><code>with_st</code></dt> <dd>This flag defaults to <code>True</code>. When <code>False</code>, it supresses the compilation and linkage of StackThreads. Pyd uses StackThreads for its iteration wrapping support. By setting this to <code>False</code>, opApply wrapping, <code>Iter</code>, and <code>AltIter</code> will be unavailable. If <code>with_pyd</code> and this are <code>True</code>, then the <code>Pyd_with_StackThreads</code> version flag will be defined (which is used internally by Pyd). <b>Important note:</b> StackThreads does not currently work with GDC! CeleriD will always set this flag to <code>False</code> when using GDC! This means that opApply wrapping is not available on Linux at this time.</dd> 45 45 <dt><code>with_meta</code></dt> <dd>This flag defaults to <code>True</code>. When <code>False</code>, it supresses the compilation and linkage of <code>meta</code> (Pyd's metaprogramming package). Because Pyd depends on meta, an exception will be raised if <code>with_pyd</code> is <code>True</code> and this is not.</dd> 46 46 <dt><code>with_main</code></dt> <dd>This flag defaults to <code>True</code>. When <code>False</code>, it supresses the use of the "magic" <code>PydMain</code> function. (Instead, users must manually declare a C-style <code>init</code> function.) Do not use this unless you know what you are doing. If <code>with_pyd</code> is <code>False</code>, this will silently be set to <code>False</code> as well. <code>PydMain</code> can only be used if Pyd itself is in use.</dd> trunk/raw_html/class_wrapping.html
r91 r102 13 13 <h1>Class wrapping</h1> 14 14 15 <p>Exposing D classes to Python is easy! The heart of Pyd's class wrapping features is the <code>wrap ped_class</code> template struct:</p>15 <p>Exposing D classes to Python is easy! The heart of Pyd's class wrapping features is the <code>wrap_class</code> function template:</p> 16 16 17 <p><code> struct wrapped_class(<span class="t_arg">T</span>, char[] <span class="t_arg">classname</span> = symbolnameof!(T));</code></p>17 <p><code>void wrap_class(<span class="t_arg">T</span>, char[] <span class="t_arg">classname</span> = symbolnameof!(T), <span class="t_arg">Params</span>...) ();</code></p> 18 18 <ul> 19 19 <li><span class="t_arg">T</span> is the class being wrapped.</li> 20 <li><span class="t_arg">classname</span> is the name of the class as it will appear in Python.</li> 20 <li><span class="t_arg">classname</span> is the name of the class as it will appear in Python. It defaults to the name of the D class. If you are wrapping an instance of a class template, you will have to provide this explicitly.</li> 21 <li><span class="t_arg">Params</span> is a series of struct types (defined below), which define the various members of the class.</li> 21 22 </ul> 22 23 23 <p>To expose the constructors, methods, and properties of the class, <code>wrapped_class</code> provides a series of template member functions.</p> 24 <p>Calls to <code>wrap_class</code> must occur <em>after</em> calling <code>module_init</code>.</p> 25 26 <p>To expose the constructors, methods, and properties of the class, you must pass <code>wrap_class</code> instantiations of these struct templates.</p> 24 27 25 28 <dl> 26 <dt><code>st atic void def(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">fn_t</span> = typeof(&fn)) (char[] <span class="arg">docstring</span>="");</code></dt>27 <dd>This wraps a method of the class. It functions exactlylike the <code>def</code> function used to <a href="func_wrapping.html">wrap regular functions</a>, with one very important difference: There is no support for default arguments. (This is a side-effect of the fact that you cannot call an alias of a method in D, and delegates do not understand default arguments.)</dd>29 <dt><code>struct Def(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">fn_t</span> = typeof(&fn));</code></dt> 30 <dd>This wraps a method of the class. It functions very much like the <code>def</code> function used to <a href="func_wrapping.html">wrap regular functions</a>, with one very important difference: There is no support for default arguments. (This is a side-effect of the fact that you cannot call an alias of a method in D, and delegates do not understand default arguments.)</dd> 28 31 29 <dt><code>st atic void static_def(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">fn_t</span> = typeof(&fn), uint <span class="t_arg">MIN_ARGS</span> = minArgs!(fn)) (char[] <span class="arg">docstring</span>="");</code></dt>32 <dt><code>struct StaticDef(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">fn_t</span> = typeof(&fn), uint <span class="t_arg">MIN_ARGS</span> = minArgs!(fn));</code></dt> 30 33 <dd>This wraps a static member function of the class. It also functions exactly like the <code>def</code> function used to wrap regular functions, and even includes support for default arguments.</dd> 31 34 32 <dt><code>st atic void prop(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), bool <span class="t_arg">RO</span> = false) (char[] <span class="arg">docstring</span>="");</code></dt>35 <dt><code>struct Property(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), bool <span class="t_arg">RO</span> = false);</code></dt> 33 36 <dd>This wraps a property. See the examples below for more details. 34 37 <ul> … … 36 39 <li><span class="t_arg">name</span> is the name of the property as it will appear in Python. As with <code>def</code>, <code>prop</code> will attempt to derive this automatically.</li> 37 40 <li><span class="t_arg">RO</span> specifies whether this is a <i>read-only</i> property. If true, it will only wrap the "get" form of the property. If false, it will wrap both the "get" and "set" forms. <i>(This is a little hackish, and I will probably try to make this detection more automatic in the future. It also means it cannot support a property that only has a "set" form.)</i></li> 38 <li><span class="arg">docstring</span> is the property's docstring. As usual, note that this is a regular function argument, and not a template argument.</li>39 41 </ul> 40 42 </dd> 41 43 42 <dt><code>st atic void init(<span class="t_arg">C</span> ...) ();</code></dt>43 <dd>This allows you to expose the class's constructors to Python. If the class provides a zero-argument constructor, there is no need to specify it; it is always available. Each element of <span class="t_arg">C</span> should be a function type. Each function type should correspond to a constructor. (That is, the arguments to the function should be the same asarguments to the class constructor. The return type is ignored.) There is an additional limitation at this time: No two constructors may have the same number of arguments. Pyd will always attempt to call the first constructor with the right number of arguments. If you wish to support a constructor with default arguments, you must specify each possible constructor call as a different template argument to this function. The examples show a few uses of the <code>init</code> function.</dd>44 <dt><code>struct Init(<span class="t_arg">C</span> ...);</code></dt> 45 <dd>This allows you to expose the class's constructors to Python. If the class provides a zero-argument constructor, there is no need to specify it; it is always available. Each element of <span class="t_arg">C</span> should be a function type. Each function type should correspond to a constructor. (That is, the arguments to the function type should be the same as the arguments to the class constructor. The return type is ignored.) There is an additional limitation at this time: No two constructors may have the same number of arguments. Pyd will always attempt to call the first constructor with the right number of arguments. If you wish to support a constructor with default arguments, you must specify each possible constructor call as a different template argument to this function. The examples show a few uses of the <code>init</code> function.</dd> 44 46 45 <dt><code>static void parent(<span class="t_arg">Parent</span>) ();</code></dt> 46 <dd>This allows the user to manually specify a class as this class's parent. This is intended for a very specific purpose (related to how Pyd handles <a href="inherit.html">inheritance</a>), and should not be used heedlessly. If a class's parent was previously wrapped, then Pyd will detect this and set up a parent-child relationship automatically, in which case it is not neccessary to specify this.</dd> 47 48 <dt><code>static void hide();</code></dt> 49 <dd>Causes this class to be wrapped, but not actually directly exposed to Python. This can be useful if you want to return instances of a class without allowing Python code to instantiate them. This is mainly used when handling <a href="inherit.html">inheritance</a>.</dd> 50 51 <dt><code>static void iter(<span class="t_arg">iter_t</span>) ();</code></dt> 47 <dt><code>struct Iter(<span class="t_arg">iter_t</span>);</code></dt> 52 48 <dd>This allows the user to specify a different overload of opApply than the default. (The default is always the one that is lexically first.) The <span class="t_arg">iter_t</span> argument should be the type of the delegate that forms the argument to opApply. This might be e.g. <code>int delegate(inout int)</code>. Don't forget the <code>inout</code> modifiers! (This is not available in Linux; see the note below on opApply wrapping.)</dd> 53 49 54 <dt><code>st atic void alt_iter(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">iter_t</span> = <i>implementationDetail</i>) (char[] <span class="arg">docstring</span>="");</code></dt>50 <dt><code>struct AltIter(alias <span class="t_arg">fn</span>, char[] <span class="t_arg">name</span> = symbolnameof!(fn), <span class="t_arg">iter_t</span> = <i>implementationDetail</i>);</code></dt> 55 51 <dd>This wraps alternate iterator methods as Python methods that return iterator objects. The wrapped methods should have a signature like that of opApply. (In other words, they should be methods intended to be used with D's ability to iterate over delgates.) The <span class="t_arg">iter_t</span> argument should be the type of the delegate argument to the method. This will usually be derived automatically. (This is not available in Linux; see the note below on opApply wrapping.) 56 52 </dd> 57 53 </dl> 58 59 <p>Once you have called all of the member functions of <code>wrapped_class</code> that you wish to, you must issue a call to <code>finalize_class</code>.</p>60 61 <p><code>void finalize_class(<span class="t_arg">CLS</span>) (<span class="t_arg">CLS</span> <span class="arg">cls</span>, char[] <span class="arg">docstring</span>="");</code></p>62 63 <p>This does some final initialization of the class and then registers it with Python. Unlike calls to <a href="func_wrapping.html"><code>def</code></a>, calls to <code>finalize_class</code> must occur <em>after</em> calling <code>module_init</code>. The <span class="arg">cls</span> function argument should be an instance of <code>wrapped_class</code>.</p>64 54 65 55 <p>If you ever wish to check whether a given class has been wrapped, Pyd helpfully registers all wrapped classes with the <code>is_wrapped</code> template, which is just a templated <code>bool</code>:</p> … … 80 70 <p>At the moment, only the following operator overloads are supported:</p> 81 71 82 <p><code>op Add, opSub, opMul, opDiv, opMod, opAnd, opOr, opXor, opShl, opShr, opCat, opAddAssign, opSubAssign, opMulAssign, opDivAssign, opModAssign, opAndAssign, opOrAssign, opXorAssign, opShlAssign, opShrAssign, opCatAssign, opIn_r, opCmp, opCall, opApply, opIndex, opIndexAssign, opSlice, opSliceAssign</code></p>72 <p><code>opNeg, opPos, opCom, opAdd, opSub, opMul, opDiv, opMod, opAnd, opOr, opXor, opShl, opShr, opCat, opAddAssign, opSubAssign, opMulAssign, opDivAssign, opModAssign, opAndAssign, opOrAssign, opXorAssign, opShlAssign, opShrAssign, opCatAssign, opIn_r, opCmp, opCall, opApply, opIndex, opIndexAssign, opSlice, opSliceAssign</code></p> 83 73 84 <p>Missing from this list are <code>opUShr</code> and <code>opUShrAssign</code>. Python does not have an unsigned right-shift operator, so these operator overloads are not supported. (You may still wrap them with a normal method using <code> wrapped_class.def</code>, of course.) Also missing from the list is <code>opApplyReverse</code>. This must be wrapped explicitly with <code>wrapped_class.alt_iter</code>.</p>74 <p>Missing from this list are <code>opUShr</code> and <code>opUShrAssign</code>. Python does not have an unsigned right-shift operator, so these operator overloads are not supported. (You may still wrap them with a normal method using <code>Def</code>, of course.) Also missing from the list is <code>opApplyReverse</code>. This must be wrapped explicitly with <code>AltIter</code>.</p> 85 75 86 76 <p>Also missing from the list is <code>opAssign</code>. Python has strict reference semantics for its objects, so overloading the assignment operator is not possible. You must explicitly wrap <code>opAssign</code> with a regular method.</p> 87 77 88 <p>Additionally, if a class provides a <code>length</code> property, Pyd will automatically make it available via Python's built-in function <code>len</code> and the special <code>__len__</code> method. You may still wrap it with <code> prop</code> or <code>def</code> if you wish it to be available as a normal property or method.</p>78 <p>Additionally, if a class provides a <code>length</code> property, Pyd will automatically make it available via Python's built-in function <code>len</code> and the special <code>__len__</code> method. You may still wrap it with <code>Property</code> or <code>Def</code> if you wish it to be available as a normal property or method.</p> 89 79 90 80 <p><b>Notes on wrapped operators</b></p> … … 124 114 <p>We would expose this class to Python by putting this code in <code>PydMain</code> after the call to <code>module_init</code>:</p> 125 115 126 <pre class="code"><span class="comment">// Make an instance of wrapped_class</span> 127 wrapped_class!(Foo) f; 128 <span class="comment">// Wrap the "foo" method</span> 129 f.def!(Foo.foo); 130 <span class="comment">// Wrap the "i" property</span> 131 f.prop!(Foo.i); 132 <span class="comment">// Wrap the constructors.</span> 133 f.init!(<span class="keyword">void function</span>(<span class="keyword">int</span>), <span class="keyword">void function</span>(<span class="keyword">int</span>, <span class="keyword">int</span>)); 134 finalize_class(f);</pre> 116 <pre class="code"><span class="comment">// Call wrap_class</span> 117 wrap_class!( 118 Foo, 119 <span class="comment">// Wrap the "foo" method</span> 120 Def!(Foo.foo), 121 <span class="comment">// Wrap the "i" property</span> 122 Property!(Foo.i), 123 <span class="comment">// Wrap the constructors.</span> 124 Init!(<span class="keyword">void function</span>(<span class="keyword">int</span>), <span class="keyword">void function</span>(<span class="keyword">int</span>, <span class="keyword">int</span>)) 125 );</pre> 135 126 136 127 <p>Now we can use this type from within Python like any other type.</p> 137 128 138 <pre class="code">>>> from testmodule importFoo129 <pre class="code">>>> <span class="keyword">from</span> testmodule <span class="keyword">import</span> Foo 139 130 >>> f = Foo() 140 131 >>> f.i 141 132 0 142 133 >>> f.i = 20 143 >>> f.foo( "Hello! i is ")134 >>> f.foo(<span class="string">"Hello! i is "</span>) 144 135 Hello! i is 20 145 136 >>> f = Foo(10, 10) … … 152 143 >>> e.i 153 144 50 154 >>> # We can even subclass our D type155 >>> classMyFoo(Foo):156 ... defbar(self):157 ... print "Hey, i+3 is", self.i + 3145 >>> <span class="comment"># We can even subclass our D type</span> 146 >>> <span class="keyword">class</span> MyFoo(Foo): 147 ... <span class="keyword">def</span> bar(self): 148 ... <span class="keyword">print</span> <span class="string">"Hey, i+3 is"</span>, self.i + 3 158 149 ... 159 150 >>> h = MyFoo(3) trunk/raw_html/inherit.html
r91 r102 28 28 <p>These would be exposed to Python by putting this code in <code>PydMain</code> after the call to <code>module_init</code>:</p> 29 29 30 <pre class="code">wrapped_class!(Base) b; 31 b.def!(Base.foo); 32 b.def!(Base.bar); 33 finalize_class(b); 30 <pre class="code">wrap_class!( 31 Base, 32 Def!(Base.foo), 33 Def!(Base.bar), 34 ); 34 35 35 wrapped_class!(Derived) d; 36 d.def!(Derived.foo); 37 finalize_class(d);</pre> 36 wrap_class!( 37 Derived, 38 Def!(Derived.foo), 39 );</pre> 38 40 39 41 <p>When used in Python, we get the expected behavior:</p> … … 52 54 Base.bar</pre> 53 55 54 <p> There is one weakness in the default behavior. Take a function like the following:</p>56 <p>Polymorphic behavior is also automatically taken care of. Take a function like the following:</p> 55 57 56 58 <pre class="code"><span class="keyword">void</span> polymorphic_call(Base b) { … … 60 62 <p>And in Python:</p> 61 63 62 <pre class="code"> <span class="keyword">class</span> PyClass(Base):63 <span class="keyword">def</span> foo(self):64 <span class="keyword">print</span> <span class="string">"PyClass.foo"</span>65 64 <pre class="code">>>> <span class="keyword">class</span> PyClass(Base): 65 ... <span class="keyword">def</span> foo(self): 66 ... <span class="keyword">print</span> <span class="string">"PyClass.foo"</span> 67 ... 66 68 >>> p = PyClass() 67 69 >>> polymorphic_call(p) 68 Base.foo</pre>69 70 <p>Optimally, we would want <code>polymorphic_call</code> to call PyClass.foo. This requires some additional work on the D side of things. To get this behavior, then rather than expose Base directly, we must expose a wrapper class:</p>71 72 <pre class="code"><span class="keyword">class</span> BaseWrap : Base {73 <span class="keyword">mixin</span> OverloadShim;74 <span class="keyword">void</span> foo() {75 get_overload(&<span class="keyword">super</span>.foo, <span class="string">"foo"</span>);76 }77 <span class="keyword">void</span> bar() {78 get_overload(&<span class="keyword">super</span>.bar, <span class="string">"bar"</span>);79 }80 }</pre>81 82 <p>The <code>OverloadShim</code> template has but a single member, the <code>get_overload</code> function.</p>83 84 <dl>85 <dt><code>ReturnType!(dg_t) get_overload(<span class="t_arg">dg_t</span>, <span class="t_arg">T ...</span>) (dg_t <span class="arg">dg</span>, char[] <span class="arg">name</span>, T <span class="arg">t</span>);</code></dt>86 <dd><ul>87 <li><span class="arg">dg</span> should be a delegate to the parent class's method.</li>88 <li><span class="arg">name</span> should be the name of the method as Python understands it to be. <i>(There's no efficient way to derive this automatically based on only the delegate.)</i></li>89 <li><span class="arg">t</span> is a tuple argument. These arguments will be passed on to the actual function call, be it the parent class's implementation or a Python subclass's implementation.</li>90 </ul></dd>91 </dl>92 93 <p><code>get_overload</code> returns whatever the method does.</p>94 95 <p>Now, we must replace the old wrapping of Base with this:</p>96 97 <pre class="code">wrapped_class!(BaseWrap, <span class="string">"Base"</span>) w;98 w.def!(BaseWrap.foo);99 w.def!(BaseWrap.bar);100 finalize_class(w);</pre>101 102 <p>Now our subclass will perform just like we expect:</p>103 104 <pre class="code">>>> p = PyClass()105 >>> polymorphic_call(p)106 70 PyClass.foo</pre> 107 108 <p>However, BaseWrap has no particular relationship to Derived. You may remember that Derived overloads <tt>bar</tt> but not <tt>foo</tt>. When we wrapped Derived in <tt>PydMain</tt>, we specified the <tt>foo</tt> overload but not the <tt>bar</tt> overload. Because Derived's parent class is no longer wrapped, Pyd no longer has any way to know about the <tt>bar</tt> method of the Derived class.</tt></p>109 110 <p>The solution is to explicitly tell Pyd that Derived's parent is BaseWrap. Furthermore, it is probably best to go the extra mile, by wrapping an <tt>OverloadShim</tt> subclass of Derived (call it DerivedWrap), and telling Pyd that BaseWrap is <em>its</em> parent. Additionally, the original <tt>Base</tt> and <tt>Derived</tt> classes should still be wrapped, in the event that functions return instances of them to Python, but should not actually be exposed to Python. The complete solution ends up looking like this:</p>111 112 <pre class="code"><span class="keyword">import</span> pyd.pyd;113 <span class="keyword">import
