Title: inspect.getsource does not work for a class-defined code object · Issue #116987 · python/cpython · GitHub
Open Graph Title: inspect.getsource does not work for a class-defined code object · Issue #116987 · python/cpython
X Title: inspect.getsource does not work for a class-defined code object · Issue #116987 · python/cpython
Description: Bug report Bug description: In the following MRE, code.co_firstlineno correctly returns 4, the first line number of the class definition, but getsource(code) returns the content of the entire file: import sys from inspect import getsourc...
Open Graph Description: Bug report Bug description: In the following MRE, code.co_firstlineno correctly returns 4, the first line number of the class definition, but getsource(code) returns the content of the entire file:...
X Description: Bug report Bug description: In the following MRE, code.co_firstlineno correctly returns 4, the first line number of the class definition, but getsource(code) returns the content of the entire file:...
Opengraph URL: https://github.com/python/cpython/issues/116987
X: @github
Domain: github.com
IPv4 address: 140.82.113.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"inspect.getsource does not work for a class-defined code object","articleBody":"# Bug report\r\n\r\n### Bug description:\r\n\r\nIn the following MRE, `code.co_firstlineno` correctly returns 4, the first line number of the class definition, but `getsource(code)` returns the content of the entire file:\r\n```python\r\nimport sys\r\nfrom inspect import getsource\r\n\r\nclass A:\r\n code = sys._getframe(0).f_code\r\n print(code.co_firstlineno)\r\n print(getsource(code))\r\n```\r\nThis is because `inspect.findsource` uses a regex pattern that does not match `class`:\r\nhttps://github.com/python/cpython/blob/a8e93d3dca086896e668b88b6c5450eaf644c0e7/Lib/inspect.py#L1155\r\n\r\n### CPython versions tested on:\r\n\r\n3.12\r\n\r\n### Operating systems tested on:\r\n\r\nLinux, Windows\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-117025\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/blhsing","@type":"Person","name":"blhsing"},"datePublished":"2024-03-19T09:50:19Z","url":"https://github.com/python/cpython/issues/116987"}
| 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:51dffbb0-8823-c473-267d-ce092f2c07e9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 86EA:2D14E:DBD71A:12C2190:6B0654D6 |
| html-safe-nonce | da6f179b6b133d8374817d75e5d5e719247c271238d6b22ed30be33c73f03dfb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NkVBOjJEMTRFOkRCRDcxQToxMkMyMTkwOjZCMDY1NEQ2IiwidmlzaXRvcl9pZCI6IjI2NTY3Mzc1MzU1MzUyNDA0MDYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3145a74fbc2a77cd2df9096cd5ddc97207bd10e774ee0208a33fd8f347c8d33a |
| hovercard-subject-tag | issue:2194496251 |
| 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/116987/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7c109b221fc4890fa5d1509cdda2c20c1d52400b1401335d94e757aeff513e42/python/cpython/issues/116987 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7c109b221fc4890fa5d1509cdda2c20c1d52400b1401335d94e757aeff513e42/python/cpython/issues/116987 |
| og:image:alt | Bug report Bug description: In the following MRE, code.co_firstlineno correctly returns 4, the first line number of the class definition, but getsource(code) returns the content of the entire file:... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | blhsing |
| 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