From f27281293479e935871d90638c9cc4893174df3e Mon Sep 17 00:00:00 2001 From: Jakob van Santen Date: Wed, 18 Sep 2024 11:48:43 +0200 Subject: [PATCH 1/2] Avoid setting __doc__ on instance methods --- src/object/function.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/object/function.cpp b/src/object/function.cpp index ec787cf15..7b6ab9f08 100644 --- a/src/object/function.cpp +++ b/src/object/function.cpp @@ -586,6 +586,13 @@ BOOST_PYTHON_DECL void add_to_namespace( BOOST_PYTHON_DECL object const& add_doc(object const& attribute, char const* doc) { +#if PY_VERSION_HEX >= 0x03000000 + if (PyInstanceMethod_Check(attribute.ptr())) { +#else + if (PyMethod_Check(attribute.ptr())) { +#endif + return attribute; + } return function::add_doc(attribute, doc); } From d3ce31d94909a8415dec96c28dfdc4773708b295 Mon Sep 17 00:00:00 2001 From: Jakob van Santen Date: Wed, 18 Sep 2024 15:13:43 +0200 Subject: [PATCH 2/2] Conditionalize nested test for py2 __qualname__ didn't exist before python 3.3. Skip checks that depend on it if running in earlier Python versions --- test/nested.py | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/test/nested.py b/test/nested.py index f53e7af50..657d100a7 100644 --- a/test/nested.py +++ b/test/nested.py @@ -13,7 +13,10 @@ >>> X.__name__ 'X' - >>> X.Y + >>> X.Y # doctest: +py2 + + + >>> X.Y # doctest: +py3 >>> X.Y.__module__ @@ -22,16 +25,22 @@ >>> X.Y.__name__ 'Y' - >>> X.color.__qualname__ + >>> getattr(X.color, "__qualname__", None) # doctest: +py3 'X.color' - >>> repr(X.color.red) + >>> repr(X.color.red) # doctest: +py2 + 'nested_ext.color.red' + + >>> repr(X.color.red) # doctest: +py3 'nested_ext.X.color.red' - >>> repr(X.color(1)) + >>> repr(X.color(1)) # doctest: +py2 + 'nested_ext.color(1)' + + >>> repr(X.color(1)) # doctest: +py3 'nested_ext.X.color(1)' - >>> test_function.__doc__.strip().split('\\n')[0] + >>> test_function.__doc__.strip().split('\\n')[0] # doctest: +py3 'test_function( (X)arg1, (X.Y)arg2) -> None :' ''' @@ -42,7 +51,23 @@ def run(args = None): if args is not None: sys.argv = args - return doctest.testmod(sys.modules.get(__name__)) + + py2 = doctest.register_optionflag("py2") + py3 = doctest.register_optionflag("py3") + + class ConditionalChecker(doctest.OutputChecker): + def check_output(self, want, got, optionflags): + if (optionflags & py3) and (sys.version_info[0] < 3): + return True + if (optionflags & py2) and (sys.version_info[0] >= 3): + return True + return doctest.OutputChecker.check_output(self, want, got, optionflags) + + runner = doctest.DocTestRunner(ConditionalChecker()) + for test in doctest.DocTestFinder().find(sys.modules.get(__name__)): + runner.run(test) + + return doctest.TestResults(runner.failures, runner.tries) if __name__ == '__main__': print("running...")