Skip to content

Instantly share code, notes, and snippets.

@markflorisson
Created August 19, 2013 20:07
Show Gist options
  • Save markflorisson/6273478 to your computer and use it in GitHub Desktop.
Save markflorisson/6273478 to your computer and use it in GitHub Desktop.
static int
context_init(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {
"prec", "rounding", "Emin", "Emax", "capitals", "clamp",
"flags", "traps", "_allcr", NULL
};
PyObject *rounding = NULL;
PyObject *traps = NULL;
PyObject *status = NULL;
mpd_context_t *ctx, t=dflt_ctx;
int capitals = 1;
int ret;
assert(PyTuple_Check(args));
ctx = CTX(self);
if (default_context_template) {
t = *CTX(default_context_template);
}
if (!PyArg_ParseTupleAndKeywords(
args, kwds,
"|" CONV_mpd_ssize_t "O" CONV_mpd_ssize_t CONV_mpd_ssize_t "ii"
"OOi", kwlist,
&t.prec, &rounding, &t.emin, &t.emax, &capitals, &t.clamp,
&status, &traps, &t.allcr
)) {
return -1;
}
if (rounding != NULL) {
if ((t.round = getround(rounding)) < 0) {
return -1;
}
}
if (!mpd_qsetprec(ctx, t.prec) ||
!mpd_qsetemin(ctx, t.emin) ||
!mpd_qsetemax(ctx, t.emax) ||
!mpd_qsetclamp(ctx, t.clamp) ||
!mpd_qsetcr(ctx, t.allcr)) {
return value_error_int("invalid context.");
}
if (!mpd_qsetround(ctx, t.round) ||
!mpd_qsettraps(ctx, t.traps) ||
!mpd_qsetstatus(ctx, t.status)) {
return type_error_int("invalid context.");
}
if (capitals != 0 && capitals != 1) {
return value_error_int("invalid context.");
}
CtxCaps(self) = capitals;
if (traps != NULL) {
if (PyInt_Check(traps) || PyLong_Check(traps)) {
ret = context_settraps(self, traps, NULL);
}
else if (PyList_Check(traps)) {
ret = context_settraps_list(self, traps);
}
else {
ret = context_settraps_dict(self, traps);
}
if (ret < 0) {
return ret;
}
}
if (status != NULL) {
if (PyInt_Check(status) || PyLong_Check(status)) {
ret = context_setstatus(self, status, NULL);
}
else if (PyList_Check(status)) {
ret = context_setstatus_list(self, status);
}
else {
ret = context_setstatus_dict(self, status);
}
if (ret < 0) {
return ret;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment