Skip to content

Commit

Permalink
Switch METH_VARGS to METH_O
Browse files Browse the repository at this point in the history
Much better performance
  • Loading branch information
movermeyer committed Nov 25, 2022
1 parent 46b27c1 commit b88c933
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions pendulum/parsing/_iso8601.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,15 +204,15 @@ static int FixedOffset_init(FixedOffset *self, PyObject *args, PyObject *kwargs)
* def utcoffset(self, dt):
* return timedelta(seconds=self.offset * 60)
*/
static PyObject *FixedOffset_utcoffset(FixedOffset *self, PyObject *args) {
static PyObject *FixedOffset_utcoffset(FixedOffset *self, PyObject *dt) {
return PyDelta_FromDSU(0, self->offset, 0);
}

/*
* def dst(self, dt):
* return timedelta(seconds=self.offset * 60)
*/
static PyObject *FixedOffset_dst(FixedOffset *self, PyObject *args) {
static PyObject *FixedOffset_dst(FixedOffset *self, PyObject *dt) {
return PyDelta_FromDSU(0, self->offset, 0);
}

Expand All @@ -223,7 +223,7 @@ static PyObject *FixedOffset_dst(FixedOffset *self, PyObject *args) {
* sign = '-'
* return f"{sign}{self.offset / 60}:{self.offset % 60}"
*/
static PyObject *FixedOffset_tzname(FixedOffset *self, PyObject *args) {
static PyObject *FixedOffset_tzname(FixedOffset *self, PyObject *dt) {
if (self->tzname != NULL) {
return PyUnicode_FromString(self->tzname);
}
Expand Down Expand Up @@ -264,9 +264,9 @@ static PyMemberDef FixedOffset_members[] = {
* Class methods
*/
static PyMethodDef FixedOffset_methods[] = {
{"utcoffset", (PyCFunction)FixedOffset_utcoffset, METH_VARARGS, ""},
{"dst", (PyCFunction)FixedOffset_dst, METH_VARARGS, ""},
{"tzname", (PyCFunction)FixedOffset_tzname, METH_VARARGS, ""},
{"utcoffset", (PyCFunction)FixedOffset_utcoffset, METH_O, ""},
{"dst", (PyCFunction)FixedOffset_dst, METH_O, ""},
{"tzname", (PyCFunction)FixedOffset_tzname, METH_O, ""},
{NULL}
};

Expand Down Expand Up @@ -1205,20 +1205,23 @@ Parsed* _parse_iso8601_duration(char *str, Parsed *parsed) {
}


PyObject* parse_iso8601(PyObject *self, PyObject *args) {
PyObject* parse_iso8601(PyObject *self, PyObject *dtstr) {
char* str;
PyObject *obj;
PyObject *tzinfo;
Py_ssize_t len;

Parsed *parsed = new_parsed();

if (!PyArg_ParseTuple(args, "s", &str)) {
PyErr_SetString(
PyExc_ValueError, "Invalid parameters"
);
if (!PyUnicode_Check(dtstr)) {
PyErr_SetString(PyExc_ValueError,
"Invalid parameters");
free(parsed);
return NULL;
}

str = PyUnicode_AsUTF8AndSize(dtstr, &len);

if (*str == 'P') {
// Duration (or interval)
if (_parse_iso8601_duration(str, parsed) == NULL) {
Expand Down Expand Up @@ -1300,7 +1303,7 @@ static PyMethodDef helpers_methods[] = {
{
"parse_iso8601",
(PyCFunction) parse_iso8601,
METH_VARARGS,
METH_O,
PyDoc_STR("Parses a ISO8601 string into a tuple.")
},
{NULL}
Expand Down

0 comments on commit b88c933

Please sign in to comment.