Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 015a679

Browse files
author
Barton Cline
committed
* Python.Runtime now exposes a public class called DocStringAttribute with usage = AttributeTargets.All
* Decorating .NET classes and methods has passed initial tests.
1 parent b393ee5 commit 015a679

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

pythonnet/src/runtime/classmanager.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,20 @@ private static ClassBase CreateClass(Type type) {
131131
}
132132

133133
// If class has constructors, generate an __doc__ attribute.
134+
135+
IntPtr doc;
136+
Type marker = typeof(DocStringAttribute);
137+
Attribute[] attrs = (Attribute[])type.GetCustomAttributes(marker, false);
138+
if (attrs.Length == 0) {
139+
doc = IntPtr.Zero;
140+
}
141+
else {
142+
DocStringAttribute attr = (DocStringAttribute)attrs[0];
143+
string docStr = attr.DocString;
144+
doc = Runtime.PyString_FromString(docStr);
145+
Runtime.PyDict_SetItemString(dict, "__doc__", doc);
146+
Runtime.Decref(doc);
147+
}
134148

135149
ClassObject co = impl as ClassObject;
136150
// If this is a ClassObject AND it has constructors, generate a __doc__ attribute.
@@ -143,12 +157,14 @@ private static ClassBase CreateClass(Type type) {
143157
// XXX deprecate __overloads__ soon...
144158
Runtime.PyDict_SetItemString(dict, "__overloads__", ctors.pyHandle);
145159
Runtime.PyDict_SetItemString(dict, "Overloads", ctors.pyHandle);
146-
147-
IntPtr doc = co.GetDocString();
148-
Runtime.PyDict_SetItemString(dict, "__doc__", doc);
149-
Runtime.Decref(doc);
150-
}
151-
}
160+
161+
if (doc == IntPtr.Zero) {
162+
doc = co.GetDocString();
163+
Runtime.PyDict_SetItemString(dict, "__doc__", doc);
164+
Runtime.Decref(doc);
165+
}
166+
}
167+
}
152168

153169
return impl;
154170
}

pythonnet/src/runtime/interop.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Python.Runtime {
2222
//=======================================================================
2323

2424
[Serializable()]
25-
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Delegate | AttributeTargets.Property)]
25+
[AttributeUsage(AttributeTargets.All)]
2626
public class DocStringAttribute : Attribute {
2727
public DocStringAttribute(string docStr) {
2828
DocString = docStr;

pythonnet/src/runtime/methodobject.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,21 @@ internal IntPtr GetDocString() {
7171
if (doc != IntPtr.Zero) {
7272
return doc;
7373
}
74-
MethodBase[] methods = binder.GetMethods();
7574
string str = "";
76-
for (int i = 0; i < methods.Length; i++) {
75+
Type marker = typeof(DocStringAttribute);
76+
MethodBase[] methods = binder.GetMethods();
77+
foreach (MethodBase method in methods) {
7778
if (str.Length > 0)
7879
str += Environment.NewLine;
79-
str += methods[i].ToString();
80-
}
80+
Attribute[] attrs = (Attribute[]) method.GetCustomAttributes(marker, false);
81+
if (attrs.Length == 0) {
82+
str += method.ToString();
83+
}
84+
else {
85+
DocStringAttribute attr = (DocStringAttribute)attrs[0];
86+
str += attr.DocString;
87+
}
88+
}
8189
doc = Runtime.PyString_FromString(str);
8290
return doc;
8391
}

0 commit comments

Comments
 (0)