Repository: RustPython/RustPython
File: crates/stdlib/src/sqlite.rs (Rust)

Code before:

        setup_module_exceptions(module, vm);

        let _ = CONVERTERS.set(vm.ctx.new_dict());
        let _ = ADAPTERS.set(vm.ctx.new_dict());
        let _ = USER_FUNCTION_EXCEPTION.set(PyAtomicRef::from(None));
        let _ = ENABLE_TRACEBACK.set(Radium::new(false));

        module
            .set_attr("converters", converters().to_owned(), vm)
            .unwrap();
        module
            .set_attr("adapters", adapters().to_owned(), vm)
            .unwrap();
    }

    #[pyattr]
    #[pyclass(name)]
    #[derive(PyPayload)]
    struct Connection {
        db: PyMutex<Option<Sqlite>>,
        initialized: PyAtomic<bool>,
        detect_types: PyAtomic<c_int>,
        isolation_level: PyAtomicRef<Option<PyStr>>,
        check_same_thread: PyAtomic<bool>,
        thread_ident: PyMutex<ThreadId>,
        row_factory: PyAtomicRef<Option<PyObject>>,
        text_factory: PyAtomicRef<PyObject>,
    }

    impl Debug for Connection {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
            write!(f, "Sqlite3 Connection")
        }
    }

    impl Constructor for Connection {
        type Args = ConnectArgs;

        fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
            let text_factory = PyStr::class(&vm.ctx).to_owned().into_object();

            // For non-subclassed Connection, initialize in __new__
            // For subclassed Connection, leave db as None and require __init__ to be called
            let is_base_class = cls.is(Connection::class(&vm.ctx).as_object());

            let db = if is_base_class {
                // Initialize immediately for base class
                Some(Connection::initialize_db(&args, vm)?)
            } else {
                // For subclasses, require __init__ to be called

Review comment (suggestion):
```suggestion
        thread_ident: PyMutex<ThreadId>,  // TODO: Use atomic
```
ThreadId is representable in u64. But not convenient yet.

Code after:

        setup_module_exceptions(module, vm);

        let _ = CONVERTERS.set(vm.ctx.new_dict());
        let _ = ADAPTERS.set(vm.ctx.new_dict());
        let _ = USER_FUNCTION_EXCEPTION.set(PyAtomicRef::from(None));
        let _ = ENABLE_TRACEBACK.set(Radium::new(false));

        module
            .set_attr("converters", converters().to_owned(), vm)
            .unwrap();
        module
            .set_attr("adapters", adapters().to_owned(), vm)
            .unwrap();
    }

    #[pyattr]
    #[pyclass(name)]
    #[derive(PyPayload)]
    struct Connection {
        db: PyMutex<Option<Sqlite>>,
        initialized: PyAtomic<bool>,
        detect_types: PyAtomic<c_int>,
        isolation_level: PyAtomicRef<Option<PyStr>>,
        check_same_thread: PyAtomic<bool>,
        thread_ident: PyMutex<ThreadId>, // TODO: Use atomic
        row_factory: PyAtomicRef<Option<PyObject>>,
        text_factory: PyAtomicRef<PyObject>,
    }

    impl Debug for Connection {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
            write!(f, "Sqlite3 Connection")
        }
    }

    impl Constructor for Connection {
        type Args = ConnectArgs;

        fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
            let text_factory = PyStr::class(&vm.ctx).to_owned().into_object();

            // For non-subclassed Connection, initialize in __new__
            // For subclassed Connection, leave db as None and require __init__ to be called
            let is_base_class = cls.is(Connection::class(&vm.ctx).as_object());

            let db = if is_base_class {
                // Initialize immediately for base class
                Some(Connection::initialize_db(&args, vm)?)
            } else {
                // For subclasses, require __init__ to be called