Changeset 59
- Timestamp:
- 12/16/06 01:54:02 (2 years ago)
- Files:
-
- trunk/html_doc/basics.html (modified) (1 diff)
- trunk/html_doc/class_wrapping.html (modified) (4 diffs)
- trunk/html_doc/func_wrapping.html (modified) (2 diffs)
- trunk/html_doc/struct_wrapping.html (modified) (3 diffs)
- trunk/infrastructure/pyd/class_wrap.d (modified) (10 diffs)
- trunk/infrastructure/pyd/def.d (modified) (4 diffs)
- trunk/infrastructure/pyd/struct_wrap.d (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/html_doc/basics.html
r57 r59 49 49 <p>The <code>module_init</code> function has the following form:</p> 50 50 51 <p><code>PyObject* module_init(char[] <span class="arg">name</span> );</code></p>51 <p><code>PyObject* module_init(char[] <span class="arg">name</span>, char[] <span class="arg">docstring</span>="");</code></p> 52 52 53 53 <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> trunk/html_doc/class_wrapping.html
r58 r59 39 39 40 40 <dl> 41 <dt><code>static 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)) ( );</code></dt>41 <dt><code>static 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> 42 42 <dd>This wraps a method of the class. It functions exactly 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> 43 43 44 <dt><code>static 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)) ( );</code></dt>44 <dt><code>static 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> 45 45 <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> 46 46 47 <dt><code>static 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) ( );</code></dt>47 <dt><code>static 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> 48 48 <dd>This wraps a property. See the examples below for more details. 49 49 <ul> … … 51 51 <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> 52 52 <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> 53 <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> 53 54 </ul> 54 55 </dd> … … 60 61 <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!</dd> 61 62 62 <dt><code>static 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>) ( );</code></dt>63 <dt><code>static 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> 63 64 <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. 64 65 </dd> … … 67 68 <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> 68 69 69 <p><code>void finalize_class(<span class="t_arg">CLS</span>) (<span class="t_arg">CLS</span> <span class="arg">cls</span> );</code></p>70 <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> 70 71 71 72 <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> trunk/html_doc/func_wrapping.html
r57 r59 30 30 <p>Exposing D functions to Python is easy! The heart of Pyd's function wrapping features is the <code>def</code> template function:</p> 31 31 32 <p><code>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), uint <span class="t_arg">MIN_ARGS</span> = minArgs!(fn)) ( );</code></p>32 <p><code>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), uint <span class="t_arg">MIN_ARGS</span> = minArgs!(fn)) (char[] <span class="arg">docstring</span>="");</code></p> 33 33 <ul> 34 34 <li><span class="t_arg">fn</span> is the function to wrap. Note that this is an alias parameter. <code>def</code> is only capable of wrapping full-fledged functions, not function pointers, function literals, or delegates.</li> … … 36 36 <li><span class="t_arg">fn_t</span> is the function type of the function. Typically, you won't have to specify this. It is used to wrap overloaded functions. (See below.)</li> 37 37 <li><span class="t_arg">MIN_ARGS</span> is the minimum number of arguments this function can accept. It is used when a function has default arguments. The <code>minArgs</code> template can derive the correct number automatically, so you typically won't specify this manually.</li> 38 <li><span class="arg">docstring</span> is the function's docstring. Note that this is a regular function argument!</li> 38 39 </ul> 39 40 trunk/html_doc/struct_wrapping.html
r57 r59 39 39 40 40 <dl> 41 <dt><code>static void member(<span class="t_arg">M</span>, size_t <span class="t_arg">offset</span>, char[] <span class="t_arg">name</span>) ( );</code></dt>41 <dt><code>static 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> 42 42 <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> 43 43 44 <dt><code>static 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)) ( );</code></dt>44 <dt><code>static 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> 45 45 <dd>This wraps a member function of the struct. It functions exactly like the <code>def</code> function used to <a href="class_wrapping.html">wrap class methods</a>, including the lack of support for default arguments.</dd> 46 46 47 <dt><code>static 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)) ( );</code></dt>47 <dt><code>static 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 48 <dd>This wraps a static member function of the struct. It functions exactly like the <code>static_def</code> function used to wrap static class member functions, and also includes support for default arguments.</dd> 49 49 50 <dt><code>static 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) ( );</code></dt>50 <dt><code>static 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 51 <dd>This wraps a property. It is identical to the <code>prop</code> function used to <a href="class_wrapping.html">wrap class properties</a>.</dd> 52 52 … … 54 54 <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> function used in <a href="class_wrapping.html">class wrapping</a>.</dd> 55 55 56 <dt><code>static 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>) ( );</code></dt>56 <dt><code>static 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> 57 57 <dd>This wraps alternate iterator methods as Python methods that return iterator objects. It is is identical to the <code>alt_iter</code> function used in <a href="class_wrapping.html">class wrapping</a>.</dd> 58 58 </dl> … … 62 62 <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> 63 63 64 <p><code>void finalize_struct(<span class="t_arg">S</span>) (<span class="t_arg">S</span> <span class="arg">s</span> );</code></p>64 <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> 65 65 66 66 <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> trunk/infrastructure/pyd/class_wrap.d
r57 r59 251 251 * if more than one function has the same name as this one. 252 252 */ 253 static void def(alias fn, char[] name = symbolnameof!(fn), fn_t=typeof(&fn)) ( ) {253 static void def(alias fn, char[] name = symbolnameof!(fn), fn_t=typeof(&fn)) (char[] docstring="") { 254 254 pragma(msg, "class.def: " ~ name); 255 255 static PyMethodDef empty = { null, null, 0, null }; … … 258 258 list[length-1].ml_meth = &method_wrap!(T, fn, fn_t).func; 259 259 list[length-1].ml_flags = METH_VARARGS; 260 list[length-1].ml_doc = "";260 list[length-1].ml_doc = (docstring ~ \0).ptr; 261 261 list ~= empty; 262 262 // It's possible that appending the empty item invalidated the … … 268 268 * Wraps a static member function of the class. Identical to pyd.def.def 269 269 */ 270 static void static_def(alias fn, char[] name = symbolnameof!(fn), fn_t=typeof(&fn), uint MIN_ARGS=minArgs!(fn)) ( ) {270 static void static_def(alias fn, char[] name = symbolnameof!(fn), fn_t=typeof(&fn), uint MIN_ARGS=minArgs!(fn)) (char[] docstring="") { 271 271 pragma(msg, "class.static_def: " ~ name); 272 272 static PyMethodDef empty = { null, null, 0, null }; … … 275 275 list[length-1].ml_meth = &function_wrap!(fn, MIN_ARGS, fn_t).func; 276 276 list[length-1].ml_flags = METH_VARARGS | METH_STATIC; 277 list[length-1].ml_doc = "";277 list[length-1].ml_doc = (docstring ~ \0).ptr; 278 278 list ~= empty; 279 279 wrapped_class_type!(T).tp_methods = list; … … 288 288 * RO = Whether this is a read-only property. 289 289 */ 290 static void prop(alias fn, char[] name = symbolnameof!(fn), bool RO=false) ( ) {290 static void prop(alias fn, char[] name = symbolnameof!(fn), bool RO=false) (char[] docstring="") { 291 291 pragma(msg, "class.prop: " ~ name); 292 292 static PyGetSetDef empty = { null, null, null, null, null }; … … 298 298 &wrapped_set!(T, fn).func; 299 299 } 300 wrapped_prop_list!(T)[length-1].doc = "";300 wrapped_prop_list!(T)[length-1].doc = (docstring ~ \0).ptr; 301 301 wrapped_prop_list!(T)[length-1].closure = null; 302 302 wrapped_prop_list!(T) ~= empty; … … 342 342 * iterator. 343 343 */ 344 static void alt_iter(alias fn, char[] name = symbolnameof!(fn), iter_t = funcDelegInfoT!(typeof(&fn)).Meta.ArgType!(0)) ( ) {344 static void alt_iter(alias fn, char[] name = symbolnameof!(fn), iter_t = funcDelegInfoT!(typeof(&fn)).Meta.ArgType!(0)) (char[] docstring="") { 345 345 static PyMethodDef empty = { null, null, 0, null }; 346 346 alias wrapped_method_list!(T) list; … … 349 349 list[length-1].ml_meth = cast(PyCFunction)&wrapped_iter!(T, fn, int function(iter_t)).iter; 350 350 list[length-1].ml_flags = METH_VARARGS; 351 list[length-1].ml_doc = "";351 list[length-1].ml_doc = (docstring ~ \0).ptr; 352 352 list ~= empty; 353 353 // It's possible that appending the empty item invalidated the … … 363 363 * calls to the wrapped_class member functions. 364 364 */ 365 void finalize_class(CLS) (CLS cls, char[] modulename="") {365 void finalize_class(CLS) (CLS cls, char[] docstring="", char[] modulename="") { 366 366 alias CLS.wrapped_type T; 367 367 alias wrapped_class_type!(T) type; … … 379 379 type.ob_type = PyType_Type_p(); 380 380 type.tp_basicsize = (wrapped_class_object!(T)).sizeof; 381 type.tp_doc = ( name ~ " objects"~ \0).ptr;381 type.tp_doc = (docstring ~ \0).ptr; 382 382 type.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE; 383 383 //type.tp_repr = &wrapped_repr!(T).repr; trunk/infrastructure/pyd/def.d
r55 r59 82 82 *It's greater than 10!) 83 83 */ 84 void def(alias fn, char[] name = symbolnameof!(fn), fn_t=typeof(&fn), uint MIN_ARGS = minArgs!(fn, fn_t)) ( ) {85 def!("", fn, name, fn_t, MIN_ARGS)( );84 void def(alias fn, char[] name = symbolnameof!(fn), fn_t=typeof(&fn), uint MIN_ARGS = minArgs!(fn, fn_t)) (char[] docstring="") { 85 def!("", fn, name, fn_t, MIN_ARGS)(docstring); 86 86 } 87 87 88 void def(char[] modulename, alias fn, char[] name = symbolnameof!(fn), fn_t=typeof(&fn), uint MIN_ARGS = minArgs!(fn, fn_t)) ( ) {88 void def(char[] modulename, alias fn, char[] name = symbolnameof!(fn), fn_t=typeof(&fn), uint MIN_ARGS = minArgs!(fn, fn_t)) (char[] docstring) { 89 89 pragma(msg, "def: " ~ name); 90 90 PyMethodDef empty; … … 95 95 (*list)[length-1].ml_meth = &function_wrap!(fn, MIN_ARGS, fn_t).func; 96 96 (*list)[length-1].ml_flags = METH_VARARGS; 97 (*list)[length-1].ml_doc = "";97 (*list)[length-1].ml_doc = (docstring ~ \0).ptr; 98 98 (*list) ~= empty; 99 99 } … … 102 102 * Module initialization function. Should be called after the last call to def. 103 103 */ 104 PyObject* module_init(char[] name ) {104 PyObject* module_init(char[] name, char[] docstring="") { 105 105 //_loadPythonSupport(); 106 106 ready_module_methods(""); 107 pyd_modules[""] = Py_InitModule ((name ~ \0).ptr, module_methods[""].ptr);107 pyd_modules[""] = Py_InitModule3((name ~ \0).ptr, module_methods[""].ptr, (docstring ~ \0).ptr); 108 108 return pyd_modules[""]; 109 109 } … … 112 112 * Module initialization function. Should be called after the last call to def. 113 113 */ 114 PyObject* add_module(char[] name ) {114 PyObject* add_module(char[] name, char[] docstring="") { 115 115 ready_module_methods(name); 116 pyd_modules[name] = Py_InitModule ((name ~ \0).ptr, module_methods[name].ptr);116 pyd_modules[name] = Py_InitModule3((name ~ \0).ptr, module_methods[name].ptr, (docstring ~ \0).ptr); 117 117 return pyd_modules[name]; 118 118 } trunk/infrastructure/pyd/struct_wrap.d
r54 r59 67 67 alias T* wrapped_type; 68 68 69 static void member(M, size_t offset, char[] name) () {69 static void member(M, size_t offset, char[] name) (char[] docstring="") { 70 70 pragma(msg, "struct.member: " ~ name); 71 71 static PyGetSetDef empty = {null, null, null, null, null}; … … 74 74 list[length-1].get = &wrapped_member!(T*, M, offset).get; 75 75 list[length-1].set = &wrapped_member!(T*, M, offset).set; 76 list[length-1].doc = "";76 list[length-1].doc = (docstring ~ \0).ptr; 77 77 list[length-1].closure = null; 78 78 list ~= empty;
