|
| 1 | +#define PY_SSIZE_T_CLEAN |
| 2 | +#include <Python.h> |
| 3 | + |
| 4 | +static PyObject * |
| 5 | +temperature_celsius_to_fahrenheit(PyObject *self, PyObject *args) |
| 6 | +{ |
| 7 | + long celsius; |
| 8 | + long fahrenheit; |
| 9 | + PyObject *ret; |
| 10 | + |
| 11 | + if (!PyArg_ParseTuple(args, "l", &celsius)) |
| 12 | + return NULL; |
| 13 | + |
| 14 | + fahrenheit = (celsius * 9/5) + 32; |
| 15 | + |
| 16 | + ret = PyLong_FromLong(fahrenheit); |
| 17 | + Py_INCREF(ret); |
| 18 | + return ret; |
| 19 | +} |
| 20 | + |
| 21 | +static PyObject * |
| 22 | +temperature_fahrenheit_to_celsius(PyObject *self, PyObject *args) |
| 23 | +{ |
| 24 | + long fahrenheit; |
| 25 | + long celsius; |
| 26 | + PyObject *ret; |
| 27 | + |
| 28 | + if (!PyArg_ParseTuple(args, "l", &fahrenheit)) |
| 29 | + return NULL; |
| 30 | + |
| 31 | + celsius = (fahrenheit - 32) * 9/5; |
| 32 | + |
| 33 | + ret = PyLong_FromLong(celsius); |
| 34 | + Py_INCREF(ret); |
| 35 | + return ret; |
| 36 | +} |
| 37 | + |
| 38 | +static PyMethodDef CoreMethods[] = { |
| 39 | + {"celsius_to_fahrenheit", temperature_celsius_to_fahrenheit, METH_VARARGS, "Convert temperature from Celsius to Fahrenheit"}, |
| 40 | + {"fahrenheit_to_celsius", temperature_fahrenheit_to_celsius, METH_VARARGS, "Convert temperature from Fahrenheit to Celsius"}, |
| 41 | + {NULL, NULL, 0, NULL} /* Sentinel */ |
| 42 | +}; |
| 43 | + |
| 44 | +static struct PyModuleDef temperaturemodule = { |
| 45 | + PyModuleDef_HEAD_INIT, |
| 46 | + "temperature", /* name of module */ |
| 47 | + NULL, /* module documentation, may be NULL */ |
| 48 | + -1, /* size of per-interpreter state of the module, |
| 49 | + or -1 if the module keeps state in global variables. */ |
| 50 | + CoreMethods |
| 51 | +}; |
| 52 | + |
| 53 | +PyMODINIT_FUNC |
| 54 | +PyInit_temperature(void) |
| 55 | +{ |
| 56 | + PyObject *m; |
| 57 | + |
| 58 | + m = PyModule_Create(&temperaturemodule); |
| 59 | + if (m == NULL) |
| 60 | + return NULL; |
| 61 | + |
| 62 | + return m; |
| 63 | +} |
0 commit comments