Title: Running doctest examples on a class results in OSError in REPL · Issue #113329 · python/cpython · GitHub
Open Graph Title: Running doctest examples on a class results in OSError in REPL · Issue #113329 · python/cpython
X Title: Running doctest examples on a class results in OSError in REPL · Issue #113329 · python/cpython
Description: Bug report Bug description: In the REPL, running doctests contained in the docstring of a class is broken since version 3.10. Example: import doctest class A: """Docstring of A, with tests. >>> 1 + 2 3 """ pass doctest.run_docstring_exam...
Open Graph Description: Bug report Bug description: In the REPL, running doctests contained in the docstring of a class is broken since version 3.10. Example: import doctest class A: """Docstring of A, with tests. >>> 1 +...
X Description: Bug report Bug description: In the REPL, running doctests contained in the docstring of a class is broken since version 3.10. Example: import doctest class A: """Docstring of A, with...
Opengraph URL: https://github.com/python/cpython/issues/113329
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Running doctest examples on a class results in OSError in REPL","articleBody":"# Bug report\n\n### Bug description:\n\nIn the REPL, running doctests contained in the docstring of a class is broken since version 3.10. Example:\n\n```python\nimport doctest\n\nclass A:\n \"\"\"Docstring of A, with tests.\n >>> 1 + 2\n 3\n \"\"\"\n pass\n\ndoctest.run_docstring_examples(A, globals(), name='A', verbose=True)\n```\n\nIn version 3.9, this code works and executes the doctest in the docstring of the class `A`. In version 3.10 (and onward), the call to `doctest.run_docstring_examples` fails with `OSError: source code not available`.\n\nThis only fails in the REPL and not in regular Python files. In particular, this also fails in (Jupyter) notebook environments.\n\n### Cause\nThis change in behavior is caused by https://github.com/python/cpython/commit/48a62559dfaf775e4f1cc56b19379c799e8e2587 as a fix for #88814. Previously, `inspect.getsourcefile()` could only fail with `TypeError`, but this can now also be `OSError`. The new exception type is not being handled by doctest, only `TypeError` is caught: https://github.com/python/cpython/blob/713e42822f5c74d8420f641968c3bef4b10a513a/Lib/doctest.py#L924-L927\n\nThe call to `inspect.getsourcefile()` is only used by doctest to determine line numbers, if available. Thus, failing to find the source file can safely fail silently in environments where line numbers are not available (such as the REPL). \n\n### Proposed changes\n- Also catch `OSError` in the doctest finder (in the linked snippet).\n- Update the documentation to reflect that `inspect.getsourcefile()` can also fail with `OSError`.\n\nAn alternative would be to change the exception type in https://github.com/python/cpython/commit/48a62559dfaf775e4f1cc56b19379c799e8e2587 to also be `TypeError` in the case the source file is not available. I do not think this a desired change.\n\nI am happy to contribute a pull request with these changes.\n\n### CPython versions tested on:\n\n3.9, 3.10, 3.11\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-113335\n* gh-155658\n* gh-155659\n* gh-155660\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/stenwessel","@type":"Person","name":"stenwessel"},"datePublished":"2023-12-20T20:11:01Z","url":"https://github.com/python/cpython/issues/113329"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f0d4f10c-2886-9760-d2a5-80d74a4bb4c3 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 93E6:299992:1922108:212B3AF:6B020F64 |
| html-safe-nonce | 9c80b82a6efefd09af6f0f73dec23f44fe8df15c87b307b7b2669d0010460c7c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5M0U2OjI5OTk5MjoxOTIyMTA4OjIxMkIzQUY6NkIwMjBGNjQiLCJ2aXNpdG9yX2lkIjoiMzc3NTU0NTE0MTQwNjg2MzIwNCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ebe68b95eca17c3c5432a83c78c57f58b100a0a9aa9f6c6d5926748d1719750e |
| hovercard-subject-tag | issue:2051196127 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/113329/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6412c3554d48f30f4843ed91272ac41c8767a1e32d7ec471229d0f906065ba76/python/cpython/issues/113329 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6412c3554d48f30f4843ed91272ac41c8767a1e32d7ec471229d0f906065ba76/python/cpython/issues/113329 |
| og:image:alt | Bug report Bug description: In the REPL, running doctests contained in the docstring of a class is broken since version 3.10. Example: import doctest class A: """Docstring of A, with tests. >>> 1 +... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | stenwessel |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width