Skip to content

Commit 0dd39fc

Browse files
committed
Merge pull request #15 from tonyroberts/develop
Don't default the docstring for classes with a ctor if a docstring was added using DocStringAttribute. Add unit tests for class and method docstrings. fixes #14
2 parents bb8200e + 85abf72 commit 0dd39fc

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed

pythonnet/src/runtime/classmanager.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private static ClassBase CreateClass(Type type) {
132132

133133
// If class has constructors, generate an __doc__ attribute.
134134

135-
IntPtr doc;
135+
IntPtr doc = IntPtr.Zero;
136136
Type marker = typeof(DocStringAttribute);
137137
Attribute[] attrs = (Attribute[])type.GetCustomAttributes(marker, false);
138138
if (attrs.Length == 0) {
@@ -160,7 +160,9 @@ private static ClassBase CreateClass(Type type) {
160160
Runtime.PyDict_SetItemString(dict, "__overloads__", ctors.pyHandle);
161161
Runtime.PyDict_SetItemString(dict, "Overloads", ctors.pyHandle);
162162
}
163-
if (!CLRModule._SuppressDocs)
163+
164+
// don't generate the docstring if one was already set from a DocStringAttribute.
165+
if (!CLRModule._SuppressDocs && doc == IntPtr.Zero)
164166
{
165167
doc = co.GetDocString();
166168
Runtime.PyDict_SetItemString(dict, "__doc__", doc);

pythonnet/src/testing/Python.Test.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
<Compile Include="methodtest.cs" />
120120
<Compile Include="propertytest.cs" />
121121
<Compile Include="threadtest.cs" />
122+
<Compile Include="doctest.cs" />
122123
</ItemGroup>
123124
<ItemGroup>
124125
<Reference Include="System" />

pythonnet/src/testing/doctest.cs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// ==========================================================================
2+
// This software is subject to the provisions of the Zope Public License,
3+
// Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
4+
// THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
5+
// WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
6+
// WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
7+
// FOR A PARTICULAR PURPOSE.
8+
// ==========================================================================
9+
10+
using Python.Runtime;
11+
12+
namespace Python.Test {
13+
14+
//========================================================================
15+
// Supports units tests for exposing docstrings from C# to Python
16+
//========================================================================
17+
18+
// Classes with a constructor have their docstring set to the ctor signature.
19+
// Test if a class has an explicit doc string it gets set correctly.
20+
[DocStringAttribute("DocWithCtorTest Class")]
21+
public class DocWithCtorTest {
22+
23+
public DocWithCtorTest() {
24+
}
25+
26+
[DocStringAttribute("DocWithCtorTest TestMethod")]
27+
public void TestMethod() {
28+
}
29+
30+
[DocStringAttribute("DocWithCtorTest StaticTestMethod")]
31+
public static void StaticTestMethod() {
32+
}
33+
34+
}
35+
36+
public class DocWithCtorNoDocTest
37+
{
38+
public DocWithCtorNoDocTest(bool x) {
39+
}
40+
41+
public void TestMethod(double a, int b) {
42+
}
43+
44+
public static void StaticTestMethod(double a, int b) {
45+
}
46+
}
47+
48+
[DocStringAttribute("DocWithoutCtorTest Class")]
49+
public class DocWithoutCtorTest {
50+
51+
[DocStringAttribute("DocWithoutCtorTest TestMethod")]
52+
public void TestMethod() {
53+
}
54+
55+
[DocStringAttribute("DocWithoutCtorTest StaticTestMethod")]
56+
public static void StaticTestMethod() {
57+
}
58+
59+
}
60+
61+
}

pythonnet/src/tests/test_docstring.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# ===========================================================================
2+
# This software is subject to the provisions of the Zope Public License,
3+
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
4+
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
5+
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
6+
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
7+
# FOR A PARTICULAR PURPOSE.
8+
# ===========================================================================
9+
import unittest
10+
import clr
11+
clr.AddReference('Python.Test')
12+
13+
from Python.Test import DocWithCtorTest, DocWithoutCtorTest, DocWithCtorNoDocTest
14+
15+
16+
class DocStringTests(unittest.TestCase):
17+
"""Test doc strings support."""
18+
19+
def testDocWithCtor(self):
20+
self.assertEqual(DocWithCtorTest.__doc__, 'DocWithCtorTest Class')
21+
self.assertEqual(DocWithCtorTest.TestMethod.__doc__, 'DocWithCtorTest TestMethod')
22+
self.assertEqual(DocWithCtorTest.StaticTestMethod.__doc__, 'DocWithCtorTest StaticTestMethod')
23+
24+
25+
def testDocWithCtorNoDoc(self):
26+
self.assertEqual(DocWithCtorNoDocTest.__doc__, 'Void .ctor(Boolean)')
27+
self.assertEqual(DocWithCtorNoDocTest.TestMethod.__doc__, 'Void TestMethod(Double, Int32)')
28+
self.assertEqual(DocWithCtorNoDocTest.StaticTestMethod.__doc__, 'Void StaticTestMethod(Double, Int32)')
29+
30+
31+
def testDocWithoutCtor(self):
32+
self.assertEqual(DocWithoutCtorTest.__doc__, 'DocWithoutCtorTest Class')
33+
self.assertEqual(DocWithoutCtorTest.TestMethod.__doc__, 'DocWithoutCtorTest TestMethod')
34+
self.assertEqual(DocWithoutCtorTest.StaticTestMethod.__doc__, 'DocWithoutCtorTest StaticTestMethod')
35+
36+
37+
def test_suite():
38+
return unittest.makeSuite(DocStringTests)
39+
40+
41+
def main():
42+
unittest.TextTestRunner().run(test_suite())
43+
44+
45+
if __name__ == '__main__':
46+
main()

0 commit comments

Comments
 (0)