Commit f6159408 authored by gabrieldemarmiesse's avatar gabrieldemarmiesse

Removed an outdated pxd, added a declaration of exc.pxd and made two examples for exceptions.

parent 7f3987e0
This diff is collapsed.
...@@ -153,6 +153,13 @@ cdef extern from "Python.h": ...@@ -153,6 +153,13 @@ cdef extern from "Python.h":
# PyErr_SetFromErrno(type);" when the system call returns an # PyErr_SetFromErrno(type);" when the system call returns an
# error. # error.
PyObject* PyErr_SetFromErrnoWithFilenameObject(object type, object filenameObject) except NULL
# Similar to PyErr_SetFromErrno(), with the additional behavior
# that if filenameObject is not NULL, it is passed to the
# constructor of type as a third parameter.
# In the case of OSError exception, this is used to define
# the filename attribute of the exception instance.
PyObject* PyErr_SetFromErrnoWithFilename(object type, char *filename) except NULL PyObject* PyErr_SetFromErrnoWithFilename(object type, char *filename) except NULL
# Return value: Always NULL. Similar to PyErr_SetFromErrno(), # Return value: Always NULL. Similar to PyErr_SetFromErrno(),
# with the additional behavior that if filename is not NULL, it is # with the additional behavior that if filename is not NULL, it is
......
...@@ -4,4 +4,4 @@ cdef extern from "string.h": ...@@ -4,4 +4,4 @@ cdef extern from "string.h":
cdef char* data = "hfvcakdfagbcffvschvxcdfgccbcfhvgcsnfxjh" cdef char* data = "hfvcakdfagbcffvschvxcdfgccbcfhvgcsnfxjh"
cdef char* pos = strstr(needle='akd', haystack=data) cdef char* pos = strstr(needle='akd', haystack=data)
print(pos != NULL) print(pos is not NULL)
from libc.stdio cimport FILE, fopen from libc.stdio cimport FILE, fopen
from libc.stdlib cimport malloc, free
from cpython.exc cimport PyErr_SetFromErrnoWithFilenameObject
def open_file():
def main():
cdef FILE* p cdef FILE* p
p = fopen("spam.txt", "r") p = fopen("spam.txt", "r")
if p == NULL: if p is NULL:
raise OSError("Couldn't open the spam file") PyErr_SetFromErrnoWithFilenameObject(OSError, "spam.txt")
# ... ...
def allocating_memory(number=10):
cdef double *my_array = <double *> malloc(number * sizeof(double))
if not my_array:
raise MemoryError()
...
free(my_array)
...@@ -419,7 +419,7 @@ compatible types.:: ...@@ -419,7 +419,7 @@ compatible types.::
cdef void* ptr cdef void* ptr
def __dealloc__(self): def __dealloc__(self):
if self.ptr != NULL: if self.ptr is not NULL:
free(self.ptr) free(self.ptr)
@staticmethod @staticmethod
......
...@@ -131,7 +131,7 @@ It currently supports OpenMP, but later on more backends might be supported. ...@@ -131,7 +131,7 @@ It currently supports OpenMP, but later on more backends might be supported.
with nogil, parallel(): with nogil, parallel():
local_buf = <int *> malloc(sizeof(int) * size) local_buf = <int *> malloc(sizeof(int) * size)
if local_buf == NULL: if local_buf is NULL:
abort() abort()
# populate our local buffer in a sequential loop # populate our local buffer in a sequential loop
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment