docstring.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # Copyright David Abrahams & Ralf W. Grosse-Kunsteve 2004-2006.
  2. # Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. '''
  5. >>> from docstring_ext import *
  6. >>> def selected_doc(obj, *args):
  7. ... doc = obj.__doc__.splitlines()
  8. ... return "\\n".join(["|"+doc[i] for i in args])
  9. >>> print(selected_doc(X.__init__, 1, 2, 3, 4, 5))
  10. |__init__( (object)self, (int)value) -> None :
  11. | this is the __init__ function
  12. | its documentation has two lines.
  13. |
  14. | C++ signature :
  15. >>> print(selected_doc(X.value, 1, 2, 4, 7, 8, 10))
  16. |value( (X)self) -> int :
  17. | gets the value of the object
  18. | C++ signature :
  19. |value( (X)self) -> int :
  20. | also gets the value of the object
  21. | C++ signature :
  22. >>> print(selected_doc(create, 1, 2, 3, 4))
  23. |create( (int)value) -> X :
  24. | creates a new X object
  25. |
  26. | C++ signature :
  27. >>> print(selected_doc(fact, 1, 2, 3, 4))
  28. |fact( (int)n) -> int :
  29. | compute the factorial
  30. |
  31. | C++ signature :
  32. >>> len(fact_usr_off_1.__doc__.splitlines())
  33. 5
  34. >>> print(selected_doc(fact_usr_off_1, 1, 3))
  35. |fact_usr_off_1( (int)n) -> int :
  36. | C++ signature :
  37. >>> len(fact_usr_on_1.__doc__.splitlines())
  38. 6
  39. >>> print(selected_doc(fact_usr_on_1, 1, 2, 4))
  40. |fact_usr_on_1( (int)n) -> int :
  41. | usr on 1
  42. | C++ signature :
  43. >>> len(fact_usr_off_2.__doc__.splitlines())
  44. 5
  45. >>> print(selected_doc(fact_usr_off_2, 1, 3))
  46. |fact_usr_off_2( (int)n) -> int :
  47. | C++ signature :
  48. >>> len(fact_usr_on_2.__doc__.splitlines())
  49. 6
  50. >>> print(selected_doc(fact_usr_on_2, 1, 2, 4))
  51. |fact_usr_on_2( (int)n) -> int :
  52. | usr on 2
  53. | C++ signature :
  54. >>> len(fact_sig_off_1.__doc__.splitlines())
  55. 2
  56. >>> print(selected_doc(fact_sig_off_1, 1))
  57. |sig off 1
  58. >>> len(fact_sig_on_1.__doc__.splitlines())
  59. 6
  60. >>> print(selected_doc(fact_sig_on_1, 1, 2, 4))
  61. |fact_sig_on_1( (int)n) -> int :
  62. | sig on 1
  63. | C++ signature :
  64. >>> len(fact_sig_off_2.__doc__.splitlines())
  65. 2
  66. >>> print(selected_doc(fact_sig_off_2, 1))
  67. |sig off 2
  68. >>> len(fact_sig_on_2.__doc__.splitlines())
  69. 6
  70. >>> print(selected_doc(fact_sig_on_2, 1, 2, 4))
  71. |fact_sig_on_2( (int)n) -> int :
  72. | sig on 2
  73. | C++ signature :
  74. >>> print(fact_usr_off_sig_off_1.__doc__)
  75. None
  76. >>> len(fact_usr_on_sig_on_1.__doc__.splitlines())
  77. 6
  78. >>> print(selected_doc(fact_usr_on_sig_on_1, 1, 2, 4))
  79. |fact_usr_on_sig_on_1( (int)n) -> int :
  80. | usr on sig on 1
  81. | C++ signature :
  82. >>> len(fact_usr_on_sig_off_1.__doc__.splitlines())
  83. 2
  84. >>> print(selected_doc(fact_usr_on_sig_off_1, 1))
  85. |usr on sig off 1
  86. >>> len(fact_usr_on_sig_on_2.__doc__.splitlines())
  87. 6
  88. >>> print(selected_doc(fact_usr_on_sig_on_2, 1, 2, 4))
  89. |fact_usr_on_sig_on_2( (int)n) -> int :
  90. | usr on sig on 2
  91. | C++ signature :
  92. >>> print(selected_doc(fact_usr_on_psig_on_csig_off_1, 1, 2))
  93. |fact_usr_on_psig_on_csig_off_1( (int)n) -> int :
  94. | usr on psig on csig off 1
  95. >>> print(selected_doc(fact_usr_on_psig_off_csig_on_1, 1, 3))
  96. |usr on psig off csig on 1
  97. |C++ signature :
  98. >>> print(fact_usr_off_psig_on_csig_off_1.__doc__.splitlines()[1])
  99. fact_usr_off_psig_on_csig_off_1( (int)n) -> int
  100. >>> print(selected_doc(fact_usr_off_psig_off_csig_on_1,1))
  101. |C++ signature :
  102. '''
  103. def run(args = None):
  104. import sys
  105. import doctest
  106. if args is not None:
  107. sys.argv = args
  108. import docstring_ext
  109. result = doctest.testmod(sys.modules.get(__name__))
  110. import pydoc
  111. import re
  112. docmodule = lambda m: re.sub(".\10", "", pydoc.text.docmodule(m))
  113. try:
  114. print('printing module help:')
  115. print(docmodule(docstring_ext))
  116. except object as x:
  117. print('********* failed **********')
  118. print(x)
  119. result = list(result)
  120. result[0] += 1
  121. return tuple(result)
  122. return result
  123. if __name__ == '__main__':
  124. print("running...")
  125. import sys
  126. status = run()[0]
  127. if (status == 0): print("Done.")
  128. sys.exit(status)