Skip to content

Conversation

@ever0de
Copy link
Contributor

@ever0de ever0de commented Jan 18, 2026

fixed #6782

CPython Reference

static PyObject *
pysqlite_connection_cursor_impl(pysqlite_Connection *self, PyObject *factory)
{
    if (factory == NULL) {
        factory = (PyObject *)self->state->CursorType;
    }
    cursor = PyObject_CallOneArg(factory, (PyObject *)self);
    if (!PyObject_TypeCheck(cursor, self->state->CursorType)) {
        PyErr_Format(PyExc_TypeError,
                     "factory must return a cursor, not %.100s",
                     Py_TYPE(cursor)->tp_name);
        ...
    }
    ...
}

Summary by CodeRabbit

  • Refactor
    • Improved SQLite cursor creation method to use a more structured argument approach
    • Enhanced validation to ensure custom factory functions return proper Cursor objects
    • Better alignment of row factory settings during cursor instantiation

✏️ Tip: You can customize this high-level summary in your review settings.

Fix test_is_instance in CursorFactoryTests by properly handling the
factory argument in Connection.cursor() method. Now the factory can
be passed as both positional and keyword argument, and returns the
correct subclass type instead of always returning PyRef<Cursor>.

- Use FromArgs derive macro with CursorArgs struct for argument parsing
- Return PyObjectRef instead of PyRef<Cursor> to allow subclasses
- Use fast_issubclass to validate returned cursor is a Cursor subclass
- Properly differentiate between 'no argument' and 'None passed'
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 18, 2026

📝 Walkthrough

Walkthrough

This PR refactors the cursor() method in the SQLite Connection binding to properly handle custom factory arguments. The method signature changes to accept a CursorArgs struct and return a generic PyObjectRef instead of a concrete Cursor reference, enabling correct type validation and row_factory inheritance for custom cursor factories.

Changes

Cohort / File(s) Summary
SQLite Cursor Factory Handling
crates/stdlib/src/sqlite.rs
Updated cursor() method signature to use CursorArgs struct instead of OptionalArg<ArgCallable>. Refactored control flow to derive factory from args, invoke it with validation, and properly swap row_factory attributes. Added CursorArgs struct for optional factory parameter. Return type changed to PyObjectRef for generic cursor subclass support. (+25/-12 lines)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • RustPython#6398 — Modifies Cursor and Connection type signatures in sqlite.rs, aligning with API changes for factory argument handling.

Suggested reviewers

  • ShaharNaveh

Poem

🐰 A cursor seeks its factory gate,
With args in hand, it validates fate,
The row_factory's magic gleam,
Now flows through every custom scheme—
Fixed at last, the test's reprieve! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: fixing Connection.cursor() factory argument handling, which is the core objective of the PR.
Linked Issues check ✅ Passed The changes directly address issue #6782 by implementing proper factory argument parsing, validating returned objects as Cursor subclasses, and distinguishing between missing and None arguments.
Out of Scope Changes check ✅ Passed All changes are focused on fixing the Connection.cursor() factory argument handling; no unrelated modifications detected.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 are you also interested in upgrading sqlite python library to 3.14?

@ever0de
Copy link
Contributor Author

ever0de commented Jan 18, 2026

👍 are you also interested in upgrading sqlite python library to 3.14?

Sure! I noticed the discussion in issue #5974 regarding automated updates is still ongoing. Should I proceed with porting it directly from CPython for now?

@youknowone
Copy link
Member

Sure, that will take time. We have a lot of things to do to reach there

@youknowone
Copy link
Member

Oh! don't forget to use scripts/lib_updater.py. That will be quite automatic

@ever0de
Copy link
Contributor Author

ever0de commented Jan 18, 2026

Thanks!

@youknowone youknowone merged commit 050db47 into RustPython:main Jan 18, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sqlite3: Fix failing test_is_instance in CursorFactoryTests

2 participants