Page 118
exception.py
total=0
class MyNumberError(ArithmeticError):
def __init__(self, message,):
super(MyNumberError, self).__init__(message)
def myFunc(x,y):
global total
total=total+1
print("in myFunc")
raise MyNumberError("Number not 42")
return 1
result=myFunc(1,1)
try:
result=myFunc(1,1)
except MyNumberError:
print("Can't divide by zero")
total=total-1
# result=myFunc(1,1)
#print(result)
print(total)
Page 123
example.c
This is the combined code from the chapter - the json files are as for example given ealier
#define PY_SSIZE_T_CLEAN
#include <Python.h>
static PyObject *exception1(PyObject *self, PyObject *args)
{
PyObject *x = PyLong_FromLong(1);
PyObject *y = PyLong_FromLong(0);
PyObject *ans = PyNumber_TrueDivide(x, y);
if (ans == NULL)
{
Py_DECREF(x);
Py_DECREF(y);
return NULL;
}
PyObject *z = PyLong_FromLong(1);
PyObject *ans2 = PyNumber_TrueDivide(x, z);
Py_DECREF(x);
Py_DECREF(y);
Py_DECREF(z);
return ans;
}
static PyObject *exception2(PyObject *self, PyObject *args)
{
PyErr_SetString(PyExc_ZeroDivisionError, "A Dummy divide error has occurred");
return NULL;
}
static PyObject *myCustomException;
static PyObject *exception3(PyObject *self, PyObject *args)
{
PyErr_SetString(myCustomException, "A Custom error has occurred");
return NULL;
}
static PyObject *exception4(PyObject *self, PyObject *args)
{
PyObject *x = PyLong_FromLong(1);
PyObject *y = PyLong_FromLong(0);
PyObject *ans = PyNumber_TrueDivide(x, y);
if (ans == NULL)
{
PyErr_Clear();
PyObject *z = PyLong_FromLong(1);
ans = PyNumber_TrueDivide(x, z);
Py_DECREF(z);
}
Py_DECREF(x);
Py_DECREF(y);
return ans;
}
static PyObject *exception5(PyObject *self, PyObject *args)
{
PyObject *x = PyLong_FromLong(1);
PyObject *y = PyLong_FromLong(0);
PyObject *ans = PyNumber_TrueDivide(x, y);
if (PyErr_Occurred() != NULL)
{
if(PyErr_ExceptionMatches(PyExc_ZeroDivisionError)){
PyErr_Clear();
PyObject *z = PyLong_FromLong(1);
ans = PyNumber_TrueDivide(x, z);
Py_DECREF(z);
}
}
Py_DECREF(x);
Py_DECREF(y);
return ans;
}
static PyObject *list1(PyObject *self, PyObject *args)
{
PyObject *myList;
if (!PyArg_ParseTuple(args, "O", &myList))
return NULL;
printf("myList %ld\n", Py_REFCNT(myList));
PyObject *temp = PyList_GetItem(myList, 0);
printf("item zero %ld\n", Py_REFCNT(temp));
PyObject *myList2 = PySequence_InPlaceConcat(myList, myList);
temp = PyList_GetItem(myList, 0);
temp = PyList_GetItem(myList, 0);
printf("myList %ld\n", Py_REFCNT(myList));
printf("item zero %ld\n", Py_REFCNT(temp));
Py_RETURN_NONE;
}
static PyMethodDef AddMethods[] = {
{"exception1", exception1, METH_VARARGS, "an example"},
{"exception2", exception2, METH_VARARGS, "an example"},
{"exception3", exception3, METH_VARARGS, "an example"},
{"exception4", exception4, METH_VARARGS, "an example"},
{"exception5", exception5, METH_VARARGS, "an example"},
{"list1", list1, METH_VARARGS, "an example"},
{NULL, NULL, 0, NULL} // sentinel
};
static struct PyModuleDef addmodule = {
PyModuleDef_HEAD_INIT,
"example",
"C library to test API",
-1,
AddMethods};
PyMODINIT_FUNC PyInit_example(void)
{
PyObject *m;
m = PyModule_Create(&addmodule);
if (m == NULL)
return NULL;
myCustomException = PyErr_NewException("example.myCustomException", NULL, NULL);
if (PyModule_AddObjectRef(m, "myCustomException", myCustomException) < 0)
{
Py_CLEAR(myCustomException);
Py_DECREF(m);
return NULL;
}
return m;
}
test.py
import example
import gc
#ans=example.exception1()
#print(ans)
#try:
# ans=example.exception2()
#except ZeroDivisionError as err:
# print(err)
try:
ans=example.exception3()
except example.myCustomException as err:
print(err)
try:
ans=example.exception4()
except ZeroDivisionError as err:
print(err)
print(ans)
try:
ans=example.exception5()
except ZeroDivisionError as err:
print(err)
print(ans)
print(gc.get_objects())
gc.collect()
myList=[257,1,2,3,4,5,6,7,8,9]
gc.collect()
example.list1(myList)
print(myList)