33
33
#define HAVE_TRACE_V2
34
34
#endif
35
35
36
+ static const char *
37
+ get_isolation_level (const char * level )
38
+ {
39
+ assert (level != NULL );
40
+ static const char * const allowed_levels [] = {
41
+ "" ,
42
+ "DEFERRED" ,
43
+ "IMMEDIATE" ,
44
+ "EXCLUSIVE" ,
45
+ NULL
46
+ };
47
+ for (int i = 0 ; allowed_levels [i ] != NULL ; i ++ ) {
48
+ const char * candidate = allowed_levels [i ];
49
+ if (sqlite3_stricmp (level , candidate ) == 0 ) {
50
+ return candidate ;
51
+ }
52
+ }
53
+ PyErr_SetString (PyExc_ValueError ,
54
+ "isolation_level string must be '', 'DEFERRED', "
55
+ "'IMMEDIATE', or 'EXCLUSIVE'" );
56
+ return NULL ;
57
+ }
58
+
59
+ static int
60
+ isolation_level_converter (PyObject * str_or_none , const char * * result )
61
+ {
62
+ if (Py_IsNone (str_or_none )) {
63
+ * result = NULL ;
64
+ }
65
+ else if (PyUnicode_Check (str_or_none )) {
66
+ Py_ssize_t sz ;
67
+ const char * str = PyUnicode_AsUTF8AndSize (str_or_none , & sz );
68
+ if (str == NULL ) {
69
+ return 0 ;
70
+ }
71
+ if (strlen (str ) != (size_t )sz ) {
72
+ PyErr_SetString (PyExc_ValueError , "embedded null character" );
73
+ return 0 ;
74
+ }
75
+
76
+ const char * level = get_isolation_level (str );
77
+ if (level == NULL ) {
78
+ return 0 ;
79
+ }
80
+ * result = level ;
81
+ }
82
+ else {
83
+ PyErr_SetString (PyExc_TypeError ,
84
+ "isolation_level must be str or None" );
85
+ return 0 ;
86
+ }
87
+ return 1 ;
88
+ }
89
+
36
90
static int
37
91
clinic_fsconverter (PyObject * pathlike , const char * * result )
38
92
{
@@ -100,29 +154,6 @@ new_statement_cache(pysqlite_Connection *self, pysqlite_state *state,
100
154
return res ;
101
155
}
102
156
103
- static const char *
104
- get_isolation_level (const char * level )
105
- {
106
- assert (level != NULL );
107
- static const char * const allowed_levels [] = {
108
- "" ,
109
- "DEFERRED" ,
110
- "IMMEDIATE" ,
111
- "EXCLUSIVE" ,
112
- NULL
113
- };
114
- for (int i = 0 ; allowed_levels [i ] != NULL ; i ++ ) {
115
- const char * candidate = allowed_levels [i ];
116
- if (sqlite3_stricmp (level , candidate ) == 0 ) {
117
- return candidate ;
118
- }
119
- }
120
- PyErr_SetString (PyExc_ValueError ,
121
- "isolation_level string must be '', 'DEFERRED', "
122
- "'IMMEDIATE', or 'EXCLUSIVE'" );
123
- return NULL ;
124
- }
125
-
126
157
/*[python input]
127
158
class FSConverter_converter(CConverter):
128
159
type = "const char *"
@@ -131,16 +162,21 @@ class FSConverter_converter(CConverter):
131
162
self.c_default = "NULL"
132
163
def cleanup(self):
133
164
return f"PyMem_Free((void *){self.name});\n"
165
+
166
+ class IsolationLevel_converter(CConverter):
167
+ type = "const char *"
168
+ converter = "isolation_level_converter"
169
+
134
170
[python start generated code]*/
135
- /*[python end generated code: output=da39a3ee5e6b4b0d input=7b3be538bc4058c0 ]*/
171
+ /*[python end generated code: output=da39a3ee5e6b4b0d input=be142323885672ab ]*/
136
172
137
173
/*[clinic input]
138
174
_sqlite3.Connection.__init__ as pysqlite_connection_init
139
175
140
176
database: FSConverter
141
177
timeout: double = 5.0
142
178
detect_types: int = 0
143
- isolation_level: str(accept={str, NoneType}) = ""
179
+ isolation_level: IsolationLevel = ""
144
180
check_same_thread: bool(accept={int}) = True
145
181
factory: object(c_default='(PyObject*)clinic_state()->ConnectionType') = ConnectionType
146
182
cached_statements as cache_size: int = 128
@@ -153,7 +189,7 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
153
189
int detect_types , const char * isolation_level ,
154
190
int check_same_thread , PyObject * factory ,
155
191
int cache_size , int uri )
156
- /*[clinic end generated code: output=7d640ae1d83abfd4 input=35e316f66d9f70fd ]*/
192
+ /*[clinic end generated code: output=7d640ae1d83abfd4 input=342173993434ba1e ]*/
157
193
{
158
194
if (PySys_Audit ("sqlite3.connect" , "s" , database ) < 0 ) {
159
195
return -1 ;
@@ -189,15 +225,6 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
189
225
return -1 ;
190
226
}
191
227
192
- // Convert isolation level to begin statement.
193
- const char * level = NULL ;
194
- if (isolation_level != NULL ) {
195
- level = get_isolation_level (isolation_level );
196
- if (level == NULL ) {
197
- return -1 ;
198
- }
199
- }
200
-
201
228
// Create LRU statement cache; returns a new reference.
202
229
PyObject * statement_cache = new_statement_cache (self , state , cache_size );
203
230
if (statement_cache == NULL ) {
@@ -215,7 +242,7 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
215
242
self -> db = db ;
216
243
self -> state = state ;
217
244
self -> detect_types = detect_types ;
218
- self -> isolation_level = level ;
245
+ self -> isolation_level = isolation_level ;
219
246
self -> check_same_thread = check_same_thread ;
220
247
self -> thread_ident = PyThread_get_thread_ident ();
221
248
self -> statement_cache = statement_cache ;
@@ -1375,26 +1402,9 @@ pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* iso
1375
1402
return -1 ;
1376
1403
}
1377
1404
Py_DECREF (res );
1405
+ return 0 ;
1378
1406
}
1379
- else if (PyUnicode_Check (isolation_level )) {
1380
- Py_ssize_t len ;
1381
- const char * cstr_level = PyUnicode_AsUTF8AndSize (isolation_level , & len );
1382
- if (cstr_level == NULL ) {
1383
- return -1 ;
1384
- }
1385
- if (strlen (cstr_level ) != (size_t )len ) {
1386
- PyErr_SetString (PyExc_ValueError , "embedded null character" );
1387
- return -1 ;
1388
- }
1389
- const char * level = get_isolation_level (cstr_level );
1390
- if (level == NULL ) {
1391
- return -1 ;
1392
- }
1393
- self -> isolation_level = level ;
1394
- }
1395
- else {
1396
- PyErr_SetString (PyExc_TypeError ,
1397
- "isolation_level must be str or None" );
1407
+ if (!isolation_level_converter (isolation_level , & self -> isolation_level )) {
1398
1408
return -1 ;
1399
1409
}
1400
1410
return 0 ;
0 commit comments