Title: Take `&Py
Open Graph Title: Take `&Py
X Title: Take `&Py
Description: Summary VirtualMachine::invoke_exception currently takes its class argument by owned value: // crates/vm/src/exceptions.rs pub fn invoke_exception( &self, cls: PyTypeRef, // PyRef Open Graph Description: Summary VirtualMachine::invoke_exception currently takes its class argument by owned value: // crates/vm/src/exceptions.rs pub fn invoke_exception( &self, cls: PyTypeRef, // PyRef X Description: Summary VirtualMachine::invoke_exception currently takes its class argument by owned value: // crates/vm/src/exceptions.rs pub fn invoke_exception( &self, cls: PyTypeRef, // PyRef<PyType>...
Opengraph URL: https://github.com/RustPython/RustPython/issues/8320
X: @github
Domain: github.com
Links:
Viewport: width=device-width
Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Take `\u0026Py\u003cPyType\u003e` instead of `PyTypeRef` in `vm.invoke_exception()`","articleBody":"## Summary\n\n`VirtualMachine::invoke_exception` currently takes its class argument by owned value:\n\n```rust\n// crates/vm/src/exceptions.rs\npub fn invoke_exception(\n \u0026self,\n cls: PyTypeRef, // PyRef\u003cPyType\u003e\n args: Vec\u003cPyObjectRef\u003e,\n) -\u003e PyResult\u003cPyBaseExceptionRef\u003e {\n let res = PyType::call(\u0026cls, args.into_args(self), self)?;\n res.downcast::\u003cPyBaseException\u003e().map_err(|obj| {\n self.new_type_error(format!(\n \"calling {} should have returned an instance of BaseException, not {}\",\n cls,\n obj.class()\n ))\n })\n}\n```\n\nIt should take a borrow instead:\n\n```rust\npub fn invoke_exception(\n \u0026self,\n cls: \u0026Py\u003cPyType\u003e,\n args: Vec\u003cPyObjectRef\u003e,\n) -\u003e PyResult\u003cPyBaseExceptionRef\u003e { ... }\n```\n\n## Why the change is justified\n\n**1. `invoke_exception` never takes ownership of `cls`.**\nThe body only ever *borrows* it:\n- `PyType::call(\u0026cls, ...)` — `PyType::call` is defined as `fn call(zelf: \u0026Py\u003cSelf\u003e, ...)`, i.e. it already takes a borrow.\n- `cls` in the `new_type_error` format string is used via `Display`, by reference.\n\nThere is no point where `cls` is moved, stored, or otherwise consumed. The owned `PyTypeRef` parameter is therefore strictly stronger than what the function needs. `PyTypeRef` (`= PyRef\u003cPyType\u003e`) derefs to `Py\u003cPyType\u003e`, so `\u0026Py\u003cPyType\u003e` is the exact borrowed form.\n\n**2. The owned parameter forces needless refcount churn at call sites.**\nBecause the signature demands ownership, most callers only holding a borrow must `.to_owned()` — an atomic refcount increment (and later decrement) — purely to satisfy the type, even though the reference is dropped immediately after the call returns:\n\n| Call site | Current argument |\n|-----------|------------------|\n| `crates/vm/src/stdlib/_io.rs:945` | `vm.ctx.exceptions.blocking_io_error.to_owned()` |\n| `crates/vm/src/stdlib/_io.rs:1156` | `vm.ctx.exceptions.blocking_io_error.to_owned()` |\n| `crates/vm/src/stdlib/_io.rs:1202` | `vm.ctx.exceptions.blocking_io_error.to_owned()` |\n| `crates/vm/src/stdlib/sys.rs:779` | `vm.ctx.exceptions.system_exit.to_owned()` |\n| `crates/vm/src/exception_group.rs:74` | `vm.ctx.exceptions.base_exception_group.to_owned()` |\n| `crates/vm/src/exceptions.rs:2010` | `typ.to_owned()` |\n| `crates/capi/src/pyerrors.rs:152` | `exc_type.to_owned()` |\n\nWith `\u0026Py\u003cPyType\u003e` every one of these drops the `.to_owned()` and passes the borrow directly.\n\n**3. It matches the idiomatic borrow used elsewhere.**\n`\u0026Py\u003cT\u003e` is the conventional \"borrowed typed object\" throughout the VM, and specifically the method `invoke_exception` delegates to — `PyType::call(zelf: \u0026Py\u003cSelf\u003e, ...)` — already takes exactly that. The change makes the helper consistent with what it wraps.\n\n**4. Callers that legitimately own a `PyTypeRef` are unaffected in cost.**\n`ExceptionCtor::instantiate` / `instantiate_value` (`crates/vm/src/exceptions.rs:447` and `:475`) own their `cls` via the `self` match; they just pass `\u0026cls` instead of moving it — a trivial edit and no clone either way.\n\n## Proposed change\n\n- Change the parameter to `cls: \u0026Py\u003cPyType\u003e`.\n- Update the ~9 call sites: drop `.to_owned()` where a borrow is already in hand, and pass `\u0026cls` from the `ExceptionCtor` sites.\n\nNo behavior change; this is a pure API/ergonomics cleanup that removes several unnecessary refcount bumps.","author":{"url":"https://github.com/youknowone","@type":"Person","name":"youknowone"},"datePublished":"2026-07-19T04:48:13.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/8320/RustPython/issues/8320"}
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:9d295278-6a1f-687a-fc63-97873e3471e7 current-catalog-service-hash 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 request-id CF78:5CA28:1172FB:17929F:6A5FE824 html-safe-nonce b512857b306586213613e8a1aac64422f2db5868eca77f08336da4ce300f663b visitor-payload eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDRjc4OjVDQTI4OjExNzJGQjoxNzkyOUY6NkE1RkU4MjQiLCJ2aXNpdG9yX2lkIjoiNTQ2NTI4NzQwMTM3NTAwMDYxMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 visitor-hmac e9b98fb760b6dca87b58976a4a46b2cb7ba1f9add4e1eeb33e288d5b44d58c49 hovercard-subject-tag issue:4921354533 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/RustPython/RustPython/8320/issue_layout twitter:image https://opengraph.githubassets.com/9db3b7b8b015f6008ab1128cd46f8c14c936384e66c10a85ad055c3651d81703/RustPython/RustPython/issues/8320 twitter:card summary_large_image og:image https://opengraph.githubassets.com/9db3b7b8b015f6008ab1128cd46f8c14c936384e66c10a85ad055c3651d81703/RustPython/RustPython/issues/8320 og:image:alt Summary VirtualMachine::invoke_exception currently takes its class argument by owned value: // crates/vm/src/exceptions.rs pub fn invoke_exception( &self, cls: PyTypeRef, // PyRef og:image:width 1200 og:image:height 600 og:site_name GitHub og:type object og:author:username youknowone hostname github.com expected-hostname github.com None 78938542173c0d8e4ad1aefc7954896e81c10ce2620286603ef14cf593bd5a11 turbo-cache-control no-preview go-import github.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git octolytics-dimension-user_id 39710557 octolytics-dimension-user_login RustPython octolytics-dimension-repository_id 135201145 octolytics-dimension-repository_nwo RustPython/RustPython octolytics-dimension-repository_public true octolytics-dimension-repository_is_fork false octolytics-dimension-repository_network_root_id 135201145 octolytics-dimension-repository_network_root_nwo RustPython/RustPython 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 1a427601e09953033b696253e754ebc7328096a7 ui-target full theme-color #1e2327 color-scheme light dark
URLs of crawlers that visited me.