| 941 | type Args = ConnectArgs; |
| 942 | |
| 943 | fn init(zelf: PyRef<Self>, args: Self::Args, vm: &VirtualMachine) -> PyResult<()> { |
| 944 | let was_initialized = Radium::swap(&zelf.initialized, false, Ordering::AcqRel); |
| 945 | |
| 946 | // Reset factories to their defaults, matching CPython's behavior. |
| 947 | zelf.reset_factories(vm); |
| 948 | |
| 949 | if was_initialized { |
| 950 | zelf.drop_db(); |
| 951 | } |
| 952 | |
| 953 | // Attempt to open the new database before mutating other state so failures leave |
| 954 | // the connection uninitialized (and subsequent operations raise ProgrammingError). |
| 955 | let db = Self::initialize_db(&args, vm)?; |
| 956 | |
| 957 | let ConnectArgs { |
| 958 | detect_types, |
| 959 | isolation_level, |
| 960 | check_same_thread, |
| 961 | autocommit, |
| 962 | .. |
| 963 | } = args; |
| 964 | |
| 965 | zelf.detect_types.store(detect_types, Ordering::Relaxed); |
| 966 | zelf.check_same_thread |
| 967 | .store(check_same_thread, Ordering::Relaxed); |
| 968 | *zelf.autocommit.lock() = autocommit; |
| 969 | *zelf.thread_ident.lock() = std::thread::current().id(); |
| 970 | let _ = unsafe { zelf.isolation_level.swap(isolation_level) }; |
| 971 | |
| 972 | let mut guard = zelf.db.lock(); |
| 973 | *guard = Some(db); |
| 974 | Radium::store(&zelf.initialized, true, Ordering::Release); |
| 975 | Ok(()) |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | #[pyclass(with(Constructor, Callable, Initializer), flags(BASETYPE, HAS_WEAKREF))] |