Skip to content

Commit 9bd0dde

Browse files
authored
Enable more compiler warnings and fix results (#546)
Add some basic flags compatible with gcc and clang, and fix the resulting errors: ``` src/_time_machine.c:519:27: error: cast from 'PyCFunction' (aka 'struct _object *(*)(struct _object *, struct _object *)') to 'PyCFunctionFastWithKeywords' (aka 'struct _object *(*)(struct _object *, struct _object *const *, long, struct _object *)') converts to incompatible function type [-Werror,-Wcast-function-type-mismatch] 519 | state->original_now = (PyCFunctionFastWithKeywords)datetime_datetime_now->m_ml->ml_meth; | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ src/_time_machine.c:523:44: error: cast from 'PyObject *(*)(PyTypeObject *, PyObject *const *, Py_ssize_t, PyObject *)' (aka 'struct _object *(*)(struct _typeobject *, struct _object *const *, long, struct _object *)') to 'PyCFunction' (aka 'struct _object *(*)(struct _object *, struct _object *)') converts to incompatible function type [-Werror,-Wcast-function-type-mismatch] 523 | datetime_datetime_now->m_ml->ml_meth = (PyCFunction)_time_machine_now; | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ src/_time_machine.c:620:9: error: cast from 'PyObject *(*)(PyObject *, PyObject *const *, Py_ssize_t, PyObject *)' (aka 'struct _object *(*)(struct _object *, struct _object *const *, long, struct _object *)') to 'PyCFunction' (aka 'struct _object *(*)(struct _object *, struct _object *)') converts to incompatible function type [-Werror,-Wcast-function-type-mismatch] 620 | (PyCFunction)_time_machine_original_now, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ src/_time_machine.c:697:1: error: no previous prototype for function 'PyInit__time_machine' [-Werror,-Wmissing-prototypes] 697 | PyInit__time_machine(void) | ^ src/_time_machine.c:696:1: note: declare 'static' if the function is not intended to be used outside of this translation unit 696 | PyMODINIT_FUNC | ^ /Users/chainz/.local/share/uv/python/cpython-3.13.6-macos-aarch64-none/include/python3.13/exports.h:103:58: note: expanded from macro 'PyMODINIT_FUNC' 103 | # define PyMODINIT_FUNC Py_EXPORTED_SYMBOL PyObject* | ^ 4 errors generated. error: command '/usr/bin/cc' failed with exit code 1 ```
1 parent c45ad79 commit 9bd0dde

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

setup.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,25 @@
1111
)
1212

1313

14-
setup(ext_modules=[Extension(name="_time_machine", sources=["src/_time_machine.c"])])
14+
setup(
15+
ext_modules=[
16+
Extension(
17+
name="_time_machine",
18+
sources=["src/_time_machine.c"],
19+
extra_compile_args=[
20+
"-Wall",
21+
"-Wextra",
22+
"-Werror",
23+
"-Wstrict-prototypes",
24+
"-Wmissing-prototypes",
25+
"-Wunused",
26+
"-Wuninitialized",
27+
"-Wshadow",
28+
"-Wformat=2",
29+
"-Wformat-security",
30+
"-Wno-unused-parameter",
31+
"-Wno-missing-field-initializers",
32+
],
33+
)
34+
]
35+
)

src/_time_machine.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#include <limits.h>
33
#include <stdlib.h>
44

5+
// Forward declarations
6+
PyMODINIT_FUNC
7+
PyInit__time_machine(void);
8+
59
// Module state
610
typedef struct {
711
#if PY_VERSION_HEX >= 0x030d00a4
@@ -516,11 +520,13 @@ _time_machine_patch_if_needed(PyObject *module, PyObject *unused)
516520
PyCFunctionObject *datetime_datetime_now =
517521
(PyCFunctionObject *)PyObject_GetAttrString(datetime_class, "now");
518522
#if PY_VERSION_HEX >= 0x030d00a4
519-
state->original_now = (PyCFunctionFastWithKeywords)datetime_datetime_now->m_ml->ml_meth;
523+
state->original_now =
524+
(PyCFunctionFastWithKeywords)(void (*)(void))datetime_datetime_now->m_ml->ml_meth;
520525
#else
521-
state->original_now = (_PyCFunctionFastWithKeywords)datetime_datetime_now->m_ml->ml_meth;
526+
state->original_now =
527+
(_PyCFunctionFastWithKeywords)(void (*)(void))datetime_datetime_now->m_ml->ml_meth;
522528
#endif
523-
datetime_datetime_now->m_ml->ml_meth = (PyCFunction)_time_machine_now;
529+
datetime_datetime_now->m_ml->ml_meth = (PyCFunction)(void (*)(void))_time_machine_now;
524530
Py_DECREF(datetime_datetime_now);
525531

526532
PyCFunctionObject *datetime_datetime_utcnow =
@@ -617,7 +623,7 @@ PyDoc_STRVAR(module_doc, "_time_machine module");
617623

618624
static PyMethodDef module_functions[] = {
619625
{"original_now",
620-
(PyCFunction)_time_machine_original_now,
626+
(PyCFunction)(void (*)(void))_time_machine_original_now,
621627
METH_FASTCALL | METH_KEYWORDS,
622628
original_now_doc},
623629
{"original_utcnow",

0 commit comments

Comments
 (0)