Commit c1e8b921 authored by scoder's avatar scoder Committed by GitHub

Merge pull request #2415 from gabrieldemarmiesse/test_language_basics_5

Added tests for "language basics" part 5
parents 6e6baf58 f6159408
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.stdlib cimport malloc, free
from cpython.exc cimport PyErr_SetFromErrnoWithFilenameObject
def open_file():
cdef FILE* p
p = fopen("spam.txt", "r")
if p is NULL:
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)
...@@ -421,7 +421,7 @@ compatible types.:: ...@@ -421,7 +421,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
......
...@@ -428,12 +428,9 @@ returns ``NULL``. The except clause doesn't work that way; its only purpose is ...@@ -428,12 +428,9 @@ returns ``NULL``. The except clause doesn't work that way; its only purpose is
for propagating Python exceptions that have already been raised, either by a Cython for propagating Python exceptions that have already been raised, either by a Cython
function or a C function that calls Python/C API routines. To get an exception function or a C function that calls Python/C API routines. To get an exception
from a non-Python-aware function such as :func:`fopen`, you will have to check the from a non-Python-aware function such as :func:`fopen`, you will have to check the
return value and raise it yourself, for example,:: return value and raise it yourself, for example:
cdef FILE* p .. literalinclude:: ../../examples/userguide/language_basics/open_file.pyx
p = fopen("spam.txt", "r")
if p == NULL:
raise SpamError("Couldn't open the spam file")
.. _overriding_in_extension_types: .. _overriding_in_extension_types:
......
...@@ -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