Title: 3.13 Regression: `inspect.getsource()` returns incorrect source code · Issue #122981 · python/cpython · GitHub
Open Graph Title: 3.13 Regression: `inspect.getsource()` returns incorrect source code · Issue #122981 · python/cpython
X Title: 3.13 Regression: `inspect.getsource()` returns incorrect source code · Issue #122981 · python/cpython
Description: Bug report Bug description: In Python 3.13rc1, inspect.getsource() sometimes returns a "random" line for some objects. For example, here's ssl.AlertDescription: $ docker run --rm python:3.13-rc python -c "import inspect, ssl; print(inspe...
Open Graph Description: Bug report Bug description: In Python 3.13rc1, inspect.getsource() sometimes returns a "random" line for some objects. For example, here's ssl.AlertDescription: $ docker run --rm python:3.13-rc pyt...
X Description: Bug report Bug description: In Python 3.13rc1, inspect.getsource() sometimes returns a "random" line for some objects. For example, here's ssl.AlertDescription: $ docker run --rm pyth...
Opengraph URL: https://github.com/python/cpython/issues/122981
X: @github
Domain: github.com
IPv4 address: 140.82.113.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"3.13 Regression: `inspect.getsource()` returns incorrect source code","articleBody":"# Bug report\r\n\r\n### Bug description:\r\n\r\nIn Python 3.13rc1, `inspect.getsource()` sometimes returns a \"random\" line for some objects. \r\nFor example, here's [`ssl.AlertDescription`](https://github.com/python/cpython/blob/v3.13.0rc1/Lib/ssl.py#L133-L136):\r\n\r\n```shell\r\n$ docker run --rm python:3.13-rc python -c \"import inspect, ssl; print(inspect.getsource(ssl.AlertDescription))\"\r\n super().shutdown(how)\r\n```\r\nThis line is likely https://github.com/python/cpython/blob/v3.13.0rc1/Lib/ssl.py#L1334.\r\n\r\nIn contrast, on 3.12 and below, this raises an OSError (not ideal but also not wrong):\r\n\r\n```shell\r\n$ docker run --rm python:3.12-rc python -c \"import inspect, ssl; print(inspect.getsource(ssl.AlertDescription))\"\r\nTraceback (most recent call last):\r\n File \"\u003cstring>\", line 1, in \u003cmodule>\r\n File \"/usr/local/lib/python3.12/inspect.py\", line 1285, in getsource\r\n lines, lnum = getsourcelines(object)\r\n ^^^^^^^^^^^^^^^^^^^^^^\r\n File \"/usr/local/lib/python3.12/inspect.py\", line 1267, in getsourcelines\r\n lines, lnum = findsource(object)\r\n ^^^^^^^^^^^^^^^^^^\r\n File \"/usr/local/lib/python3.12/inspect.py\", line 1112, in findsource\r\n raise OSError('could not find class definition')\r\nOSError: could not find class definition\r\n```\r\n\r\nI'm not sure why this is happening, but it also affects [pytest.PytestAssertRewriteWarning](https://github.com/pytest-dev/pytest/blob/38ad84bafd18d15ceff1960d636c693560337844/src/_pytest/warning_types.py#L20) and [pluggy.PluggyTeardownRaisedWarning](https://github.com/pytest-dev/pluggy/blob/5c16e15a963d5e66f37d05b1ccfb90adf71e8e0f/src/pluggy/_warnings.py#L11), which both extend custom classes that override `__module__`.\r\n\r\n### CPython versions tested on:\r\n\r\n3.8, 3.9, 3.10, 3.11, 3.12, 3.13\r\n\r\n### Operating systems tested on:\r\n\r\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-123001\n* gh-123182\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/mhils","@type":"Person","name":"mhils"},"datePublished":"2024-08-13T18:25:37Z","url":"https://github.com/python/cpython/issues/122981"}
| 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:2889ac06-a894-10e7-7b6a-3b5968b287e5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CD44:1188C0:13A7BD9:1A01488:6B065F5E |
| html-safe-nonce | 5878a6e4fcbb48911c50f5622f72118311d2ee25f95fe6d381b3e1eed6e31bfe |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRDQ0OjExODhDMDoxM0E3QkQ5OjFBMDE0ODg6NkIwNjVGNUUiLCJ2aXNpdG9yX2lkIjoiMjQxMzk1ODM4MzQ4NTgyNzUwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | d600f6545c4c042a29016e72b269e476565269e56f279949530a78b53bae6831 |
| hovercard-subject-tag | issue:2463957418 |
| 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/122981/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3a1c5561fa6b7c628a5ec4e772f5754b70f50c0cfa2f9560500ad6da01f2c783/python/cpython/issues/122981 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3a1c5561fa6b7c628a5ec4e772f5754b70f50c0cfa2f9560500ad6da01f2c783/python/cpython/issues/122981 |
| og:image:alt | Bug report Bug description: In Python 3.13rc1, inspect.getsource() sometimes returns a "random" line for some objects. For example, here's ssl.AlertDescription: $ docker run --rm python:3.13-rc pyt... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mhils |
| hostname | github.com |
| expected-hostname | github.com |
| None | 6324e6ac990fb6befbec840cc0e102a1071c339bfe8f9826bb12de2e3ba1d297 |
| 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 | 7f0aa75f4141d440c6110dc31af7ea7797abb6c4 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width