Skip to content

Commit 2cda7a0

Browse files
committed
Use PyType_GenericNew everywhere
Any non-zero initialization is done in the _init methods. This also allowed removing some code which is nice. Refs #498
1 parent fd8f674 commit 2cda7a0

7 files changed

Lines changed: 48 additions & 164 deletions

File tree

src/backup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,5 +399,5 @@ static PyTypeObject APSWBackupType = {
399399
.tp_methods = backup_methods,
400400
.tp_members = backup_members,
401401
.tp_getset = backup_getset,
402-
.tp_str = (reprfunc)APSWBackup_tp_str,
402+
.tp_str = APSWBackup_tp_str,
403403
};

src/blob.c

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,6 @@ There are `benchmarks <https://www.sqlite.org/fasterthanfs.html>`__.
4242

4343
/* ZeroBlobBind is defined in apsw.c because of forward references */
4444

45-
static PyObject *
46-
ZeroBlobBind_new(PyTypeObject *type, PyObject *Py_UNUSED(args), PyObject *Py_UNUSED(kwargs))
47-
{
48-
ZeroBlobBind *self;
49-
self = (ZeroBlobBind *)type->tp_alloc(type, 0);
50-
if (self)
51-
{
52-
self->blobsize = 0;
53-
self->init_was_called = 0;
54-
}
55-
return (PyObject *)self;
56-
}
57-
5845
/** .. method:: __init__(size: int)
5946
6047
:param size: Number of zeroed bytes to create
@@ -110,9 +97,9 @@ static PyTypeObject ZeroBlobBindType = {
11097
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
11198
.tp_doc = Zeroblob_class_DOC,
11299
.tp_methods = ZeroBlobBind_methods,
113-
.tp_init = (initproc)ZeroBlobBind_init,
114-
.tp_new = ZeroBlobBind_new,
115-
.tp_str = (reprfunc)ZeroBlobBind_tp_str,
100+
.tp_init = ZeroBlobBind_init,
101+
.tp_new = PyType_GenericNew,
102+
.tp_str = ZeroBlobBind_tp_str,
116103
};
117104

118105
/* BLOB TYPE */
@@ -707,5 +694,5 @@ static PyTypeObject APSWBlobType = {
707694
.tp_doc = Blob_class_DOC,
708695
.tp_weaklistoffset = offsetof(APSWBlob, weakreflist),
709696
.tp_methods = APSWBlob_methods,
710-
.tp_str = (reprfunc)APSWBlob_tp_str,
697+
.tp_str = APSWBlob_tp_str,
711698
};

src/connection.c

Lines changed: 23 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -409,53 +409,6 @@ Connection_dealloc(PyObject *self_)
409409
Py_TpFree(self_);
410410
}
411411

412-
static PyObject *
413-
Connection_new(PyTypeObject *type, PyObject *Py_UNUSED(args), PyObject *Py_UNUSED(kwds))
414-
{
415-
Connection *self;
416-
417-
self = (Connection *)type->tp_alloc(type, 0);
418-
if (self != NULL)
419-
{
420-
self->db = 0;
421-
self->dbmutex = 0;
422-
self->cursor_factory = Py_NewRef((PyObject *)&APSWCursorType);
423-
self->dependents = PyList_New(0);
424-
self->stmtcache = 0;
425-
self->fts5_api_cached = 0;
426-
self->busyhandler = 0;
427-
self->rollbackhook = 0;
428-
self->updatehook = 0;
429-
self->commithook = 0;
430-
self->walhook = 0;
431-
self->authorizer = 0;
432-
self->collationneeded = 0;
433-
self->exectrace = 0;
434-
self->rowtrace = 0;
435-
self->vfs = 0;
436-
self->savepointlevel = 0;
437-
self->open_flags = 0;
438-
self->open_vfs = 0;
439-
self->weakreflist = 0;
440-
self->tracehooks = PyMem_Malloc(sizeof(struct tracehook) * 1);
441-
self->tracehooks_count = 0;
442-
if (self->tracehooks)
443-
{
444-
self->tracehooks[0].callback = 0;
445-
self->tracehooks[0].id = 0;
446-
self->tracehooks[0].mask = 0;
447-
self->tracehooks_count = 1;
448-
}
449-
self->progresshandler = 0;
450-
self->progresshandler_count = 0;
451-
CALL_TRACK_INIT(xConnect);
452-
if (self->dependents && self->tracehooks)
453-
return (PyObject *)self;
454-
}
455-
456-
return NULL;
457-
}
458-
459412
/** .. method:: __init__(filename: str, flags: int = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, vfs: Optional[str] = None, statementcachesize: int = 100)
460413
461414
Opens the named database. You can use ``:memory:`` to get a private temporary
@@ -506,6 +459,21 @@ Connection_init(PyObject *self_, PyObject *args, PyObject *kwargs)
506459
}
507460
flags |= SQLITE_OPEN_EXRESCODE;
508461

462+
self->cursor_factory = Py_NewRef((PyObject *)&APSWCursorType);
463+
self->tracehooks = PyMem_Malloc(sizeof(struct tracehook) * 1);
464+
if (!self->tracehooks)
465+
return -1;
466+
self->tracehooks[0].callback = 0;
467+
self->tracehooks[0].id = 0;
468+
self->tracehooks[0].mask = 0;
469+
self->tracehooks_count = 1;
470+
471+
self->dependents = PyList_New(0);
472+
if (!self->dependents)
473+
return -1;
474+
475+
CALL_TRACK_INIT(xConnect);
476+
509477
/* clamp cache size */
510478
if (statementcachesize < 0)
511479
statementcachesize = 0;
@@ -5868,15 +5836,12 @@ static PyGetSetDef Connection_getseters[] = {
58685836
{ "filename", Connection_getmainfilename, NULL, Connection_filename_DOC, NULL },
58695837
{ "filename_journal", Connection_getjournalfilename, NULL, Connection_filename_journal_DOC, NULL },
58705838
{ "filename_wal", Connection_getwalfilename, NULL, Connection_filename_wal_DOC, NULL },
5871-
{ "cursor_factory", Connection_get_cursor_factory, Connection_set_cursor_factory,
5872-
Connection_cursor_factory_DOC, NULL },
5839+
{ "cursor_factory", Connection_get_cursor_factory, Connection_set_cursor_factory, Connection_cursor_factory_DOC,
5840+
NULL },
58735841
{ "in_transaction", Connection_get_in_transaction, NULL, Connection_in_transaction_DOC },
5874-
{ "exec_trace", Connection_get_exec_trace_attr, Connection_set_exec_trace_attr,
5875-
Connection_exec_trace_DOC },
5876-
{ "row_trace", Connection_get_row_trace_attr, Connection_set_row_trace_attr,
5877-
Connection_row_trace_DOC },
5878-
{ "authorizer", Connection_get_authorizer_attr, Connection_set_authorizer_attr,
5879-
Connection_authorizer_DOC },
5842+
{ "exec_trace", Connection_get_exec_trace_attr, Connection_set_exec_trace_attr, Connection_exec_trace_DOC },
5843+
{ "row_trace", Connection_get_row_trace_attr, Connection_set_row_trace_attr, Connection_row_trace_DOC },
5844+
{ "authorizer", Connection_get_authorizer_attr, Connection_set_authorizer_attr, Connection_authorizer_DOC },
58805845
{ "system_errno", Connection_get_system_errno, NULL, Connection_system_errno_DOC },
58815846
{ "is_interrupted", Connection_is_interrupted, NULL, Connection_is_interrupted_DOC },
58825847
#ifndef APSW_OMIT_OLD_NAMES
@@ -6120,7 +6085,7 @@ static PyTypeObject ConnectionType = {
61206085
.tp_methods = Connection_methods,
61216086
.tp_members = Connection_members,
61226087
.tp_getset = Connection_getseters,
6123-
.tp_init = (initproc)Connection_init,
6124-
.tp_new = Connection_new,
6125-
.tp_str = (reprfunc)Connection_tp_str,
6088+
.tp_init = Connection_init,
6089+
.tp_new = PyType_GenericNew,
6090+
.tp_str = Connection_tp_str,
61266091
};

src/cursor.c

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ struct APSWCursor
8787
/* what state we are in */
8888
enum
8989
{
90+
C_DONE = 0,
9091
C_BEGIN,
9192
C_ROW,
92-
C_DONE
9393
} status;
9494
};
9595

@@ -265,34 +265,6 @@ APSWCursor_dealloc(PyObject *self_)
265265
Py_TpFree(self_);
266266
}
267267

268-
static PyObject *
269-
APSWCursor_new(PyTypeObject *type, PyObject *Py_UNUSED(args), PyObject *Py_UNUSED(kwds))
270-
{
271-
APSWCursor *self;
272-
273-
self = (APSWCursor *)type->tp_alloc(type, 0);
274-
if (self != NULL)
275-
{
276-
self->connection = NULL;
277-
self->statement = 0;
278-
self->status = C_DONE;
279-
self->bindings = 0;
280-
self->bindingsoffset = 0;
281-
self->emiter = 0;
282-
self->emoriginalquery = 0;
283-
self->exectrace = 0;
284-
self->rowtrace = 0;
285-
self->weakreflist = NULL;
286-
self->description_cache[0] = 0;
287-
self->description_cache[1] = 0;
288-
self->description_cache[2] = 0;
289-
self->init_was_called = 0;
290-
self->in_query = 0;
291-
}
292-
293-
return (PyObject *)self;
294-
}
295-
296268
/** .. method:: __init__(connection: Connection)
297269
298270
Use :meth:`Connection.cursor` to make a new cursor.
@@ -1961,9 +1933,9 @@ static PyTypeObject APSWCursorType = {
19611933
.tp_iternext = (iternextfunc)APSWCursor_next,
19621934
.tp_methods = APSWCursor_methods,
19631935
.tp_getset = APSWCursor_getset,
1964-
.tp_init = (initproc)APSWCursor_init,
1965-
.tp_new = APSWCursor_new,
1966-
.tp_str = (reprfunc)APSWCursor_tp_str,
1936+
.tp_init = APSWCursor_init,
1937+
.tp_new = PyType_GenericNew,
1938+
.tp_str = APSWCursor_tp_str,
19671939
};
19681940

19691941
static int
@@ -1991,7 +1963,7 @@ static PyTypeObject PyObjectBindType = {
19911963
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "apsw.pyobject",
19921964
.tp_basicsize = sizeof(PyObjectBind),
19931965
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1994-
.tp_init = (initproc)PyObjectBind_init,
1966+
.tp_init = PyObjectBind_init,
19951967
.tp_finalize = PyObjectBind_finalize,
19961968
.tp_new = PyType_GenericNew,
19971969
.tp_doc = Apsw_pyobject_DOC,

src/fts.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ static PyTypeObject APSWFTS5TokenizerType = {
380380
.tp_call = PyVectorcall_Call,
381381
.tp_vectorcall_offset = offsetof(APSWFTS5Tokenizer, vectorcall),
382382
.tp_getset = APSWFTS5Tokenizer_getset,
383-
.tp_str = (reprfunc)APSWFTS5Tokenizer_tp_str,
383+
.tp_str = APSWFTS5Tokenizer_tp_str,
384384
};
385385

386386
typedef struct

src/session.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,7 +2284,7 @@ static PyTypeObject APSWSessionType = {
22842284
.tp_basicsize = sizeof(APSWSession),
22852285
.tp_doc = Session_class_DOC,
22862286
.tp_new = PyType_GenericNew,
2287-
.tp_init = (initproc)APSWSession_init,
2287+
.tp_init = APSWSession_init,
22882288
.tp_dealloc = APSWSession_dealloc,
22892289
.tp_methods = APSWSession_methods,
22902290
.tp_getset = APSWSession_getset,
@@ -2334,7 +2334,7 @@ static PyTypeObject APSWChangesetBuilderType = {
23342334
.tp_basicsize = sizeof(APSWChangesetBuilder),
23352335
.tp_methods = APSWChangesetBuilder_methods,
23362336
.tp_new = PyType_GenericNew,
2337-
.tp_init = (initproc)APSWChangesetBuilder_init,
2337+
.tp_init = APSWChangesetBuilder_init,
23382338
.tp_dealloc = APSWChangesetBuilder_dealloc,
23392339
.tp_doc = ChangesetBuilder_class_DOC,
23402340
.tp_weaklistoffset = offsetof(APSWChangesetBuilder, weakreflist),
@@ -2360,7 +2360,7 @@ static PyTypeObject APSWTableChangeType = {
23602360
.tp_getset = APSWTableChange_getset,
23612361
.tp_doc = TableChange_class_DOC,
23622362
.tp_dealloc = APSWTableChange_dealloc,
2363-
.tp_str = (reprfunc)APSWTableChange_tp_str,
2363+
.tp_str = APSWTableChange_tp_str,
23642364
};
23652365

23662366
static PyMethodDef APSWRebaser_methods[] = {
@@ -2377,6 +2377,6 @@ static PyTypeObject APSWRebaserType = {
23772377
.tp_doc = Rebaser_class_DOC,
23782378
.tp_methods = APSWRebaser_methods,
23792379
.tp_new = PyType_GenericNew,
2380-
.tp_init = (initproc)APSWRebaser_init,
2380+
.tp_init = APSWRebaser_init,
23812381
.tp_dealloc = APSWRebaser_dealloc,
23822382
};

src/vfs.c

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,6 @@ typedef struct apswfcntl_pragma
8484
int init_was_called;
8585
} apswfcntl_pragma;
8686

87-
static PyObject *
88-
apswfcntl_pragma_new(PyTypeObject *type, PyObject *Py_UNUSED(args), PyObject *Py_UNUSED(kwds))
89-
{
90-
apswfcntl_pragma *self = (apswfcntl_pragma *)type->tp_alloc(type, 0);
91-
if (self != NULL)
92-
{
93-
self->strings = NULL;
94-
self->init_was_called = 0;
95-
}
96-
return (PyObject *)self;
97-
}
98-
9987
/** .. method:: __init__(pointer: int)
10088
10189
The pointer must be what your xFileControl method received.
@@ -195,8 +183,8 @@ static PyTypeObject apswfcntl_pragma_Type = {
195183
.tp_basicsize = sizeof(apswfcntl_pragma),
196184
.tp_itemsize = 0,
197185
.tp_flags = Py_TPFLAGS_DEFAULT,
198-
.tp_new = apswfcntl_pragma_new,
199-
.tp_init = (initproc)apswfcntl_pragma_init,
186+
.tp_new = PyType_GenericNew,
187+
.tp_init = apswfcntl_pragma_init,
200188
.tp_getset = apswfcntl_pragma_getsetters,
201189
};
202190

@@ -1765,20 +1753,6 @@ APSWVFS_dealloc(PyObject *self_)
17651753
Py_TpFree(self_);
17661754
}
17671755

1768-
static PyObject *
1769-
APSWVFS_new(PyTypeObject *type, PyObject *Py_UNUSED(args), PyObject *Py_UNUSED(kwds))
1770-
{
1771-
APSWVFS *self;
1772-
self = (APSWVFS *)type->tp_alloc(type, 0);
1773-
if (self)
1774-
{
1775-
self->basevfs = NULL;
1776-
self->containingvfs = NULL;
1777-
self->registered = 0;
1778-
}
1779-
return (PyObject *)self;
1780-
}
1781-
17821756
/** .. method:: __init__(name: str, base: Optional[str] = None, makedefault: bool = False, maxpathname: int = 1024, *, iVersion: int = 3, exclude: Optional[set[str]] = None)
17831757
17841758
:param name: The name to register this vfs under. If the name
@@ -1967,9 +1941,9 @@ static PyTypeObject APSWVFSType = {
19671941
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
19681942
.tp_doc = VFS_class_DOC,
19691943
.tp_methods = APSWVFS_methods,
1970-
.tp_init = (initproc)APSWVFS_init,
1971-
.tp_new = APSWVFS_new,
1972-
.tp_str = (reprfunc)APSWVFS_tp_str,
1944+
.tp_init = APSWVFS_init,
1945+
.tp_new = PyType_GenericNew,
1946+
.tp_str = APSWVFS_tp_str,
19731947
};
19741948

19751949
static int
@@ -2027,21 +2001,6 @@ APSWVFSFile_dealloc(PyObject *self_)
20272001
PY_ERR_RESTORE(exc_save);
20282002
}
20292003

2030-
static PyObject *
2031-
APSWVFSFile_new(PyTypeObject *type, PyObject *Py_UNUSED(args), PyObject *Py_UNUSED(kwds))
2032-
{
2033-
APSWVFSFile *self;
2034-
self = (APSWVFSFile *)type->tp_alloc(type, 0);
2035-
if (self)
2036-
{
2037-
self->base = NULL;
2038-
self->filename = NULL;
2039-
self->free_filename = 1;
2040-
}
2041-
2042-
return (PyObject *)self;
2043-
}
2044-
20452004
/** .. method:: __init__(vfs: str, filename: str | URIFilename | None, flags: list[int,int])
20462005
20472006
:param vfs: The vfs you want to inherit behaviour from. You can
@@ -2099,6 +2058,7 @@ APSWVFSFile_init(PyObject *self_, PyObject *args, PyObject *kwargs)
20992058
self->filename = apsw_strdup(text);
21002059
if (!self->filename)
21012060
return -1;
2061+
self->free_filename = 1;
21022062
}
21032063
else if (Py_IsNone(filename))
21042064
{
@@ -3139,9 +3099,9 @@ static PyTypeObject APSWVFSFileType = {
31393099
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
31403100
.tp_doc = VFSFile_class_DOC,
31413101
.tp_methods = APSWVFSFile_methods,
3142-
.tp_init = (initproc)APSWVFSFile_init,
3143-
.tp_new = APSWVFSFile_new,
3144-
.tp_str = (reprfunc)APSWVFSFile_tp_str,
3102+
.tp_init = APSWVFSFile_init,
3103+
.tp_new = PyType_GenericNew,
3104+
.tp_str = APSWVFSFile_tp_str,
31453105
};
31463106

31473107
/** .. class:: URIFilename
@@ -3328,7 +3288,7 @@ static PyTypeObject APSWURIFilenameType = {
33283288
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
33293289
.tp_doc = URIFilename_class_DOC,
33303290
.tp_methods = APSWURIFilenameMethods,
3331-
.tp_str = (reprfunc)apswurifilename_tp_str,
3291+
.tp_str = apswurifilename_tp_str,
33323292
.tp_getset = APSWURIFilename_getset,
33333293
};
33343294

0 commit comments

Comments
 (0)