automatically raise MemoryError on exceptional C return values
Hi, given how ubiquitous manual memory management is in C, I think it would be nice to let Cython generate the exception raising also for C, not only for C++. The difference is this: cdef extern from "...": char* make_new_buffer() except NULL as MemoryError int append_to_buffer(char* buffer, char* value) \ except -1 as MemoryError c_buffer = make_new_buffer() # raises MemoryError on NULL append_to_buffer(c_buffer, "testdata") # raises MemoryError on -1 append_to_buffer(c_buffer, "moredata") # raises MemoryError on -1 versus this: cdef extern from "...": char* make_new_buffer() int append_to_buffer(char* buffer, char* value) c_buffer = make_new_buffer() if c_buffer is NULL: raise MemoryError() if append_to_buffer(c_buffer, "testdata") == -1: raise MemoryError() if append_to_buffer(c_buffer, "moredata") == -1: raise MemoryError() I don't think it's necessary to support this for anything but MemoryError, since you'd normally want a formatted error message for everything else. Alternative colours for the bike shed: char* make_new_buffer() except NULL raise MemoryError char* make_new_buffer() raise MemoryError for NULL char* make_new_buffer() raise MemoryError from NULL What do you think? Stefan
On 7 August 2012 10:53, Stefan Behnel <stefan_ml@behnel.de> wrote:
Hi,
given how ubiquitous manual memory management is in C, I think it would be nice to let Cython generate the exception raising also for C, not only for C++. The difference is this:
cdef extern from "...": char* make_new_buffer() except NULL as MemoryError int append_to_buffer(char* buffer, char* value) \ except -1 as MemoryError
c_buffer = make_new_buffer() # raises MemoryError on NULL append_to_buffer(c_buffer, "testdata") # raises MemoryError on -1 append_to_buffer(c_buffer, "moredata") # raises MemoryError on -1
versus this:
cdef extern from "...": char* make_new_buffer() int append_to_buffer(char* buffer, char* value)
c_buffer = make_new_buffer() if c_buffer is NULL: raise MemoryError() if append_to_buffer(c_buffer, "testdata") == -1: raise MemoryError() if append_to_buffer(c_buffer, "moredata") == -1: raise MemoryError()
I don't think it's necessary to support this for anything but MemoryError, since you'd normally want a formatted error message for everything else.
Alternative colours for the bike shed:
char* make_new_buffer() except NULL raise MemoryError
char* make_new_buffer() raise MemoryError for NULL
char* make_new_buffer() raise MemoryError from NULL
What do you think?
Stefan _______________________________________________ cython-devel mailing list cython-devel@python.org http://mail.python.org/mailman/listinfo/cython-devel
I'd say write a wrapper function that does the check, or use an object that encapsulates the buffer allocation and functions. I'm not sure we'd want extra syntax for this. Calling functions is already complicated, this will complicate it even further.
mark florisson, 07.08.2012 12:33:
I'd say write a wrapper function that does the check, or use an object that encapsulates the buffer allocation and functions. I'm not sure we'd want extra syntax for this. Calling functions is already complicated, this will complicate it even further.
Funny to see this written by someone who bloats the entire type system with buffers, memory views and slice types, just to keep people from calculating index offsets into arrays. Stefan ;o)
On 7 August 2012 11:44, Stefan Behnel <stefan_ml@behnel.de> wrote:
mark florisson, 07.08.2012 12:33:
I'd say write a wrapper function that does the check, or use an object that encapsulates the buffer allocation and functions. I'm not sure we'd want extra syntax for this. Calling functions is already complicated, this will complicate it even further.
Funny to see this written by someone who bloats the entire type system with buffers, memory views and slice types, just to keep people from calculating index offsets into arrays.
The only type that was added was the memoryview type, which is supposed to *replace* the buffer type. And it does much more than index calculation. So... yeah. Let's not make a technical discussion personal.
Stefan ;o)
_______________________________________________ cython-devel mailing list cython-devel@python.org http://mail.python.org/mailman/listinfo/cython-devel
On 7 August 2012 11:59, mark florisson <markflorisson88@gmail.com> wrote:
On 7 August 2012 11:44, Stefan Behnel <stefan_ml@behnel.de> wrote:
mark florisson, 07.08.2012 12:33:
I'd say write a wrapper function that does the check, or use an object that encapsulates the buffer allocation and functions. I'm not sure we'd want extra syntax for this. Calling functions is already complicated, this will complicate it even further.
Funny to see this written by someone who bloats the entire type system with buffers, memory views and slice types, just to keep people from calculating index offsets into arrays.
The only type that was added was the memoryview type, which is supposed to *replace* the buffer type. And it does much more than index calculation. So... yeah. Let's not make a technical discussion personal.
Besides, feedback seems to suggest people are happy with this "bloat".
Stefan ;o)
_______________________________________________ cython-devel mailing list cython-devel@python.org http://mail.python.org/mailman/listinfo/cython-devel
mark florisson, 07.08.2012 13:00:
On 7 August 2012 11:59, mark florisson wrote:
On 7 August 2012 11:44, Stefan Behnel wrote:
mark florisson, 07.08.2012 12:33:
I'd say write a wrapper function that does the check, or use an object that encapsulates the buffer allocation and functions. I'm not sure we'd want extra syntax for this. Calling functions is already complicated, this will complicate it even further.
Funny to see this written by someone who bloats the entire type system with buffers, memory views and slice types, just to keep people from calculating index offsets into arrays.
The only type that was added was the memoryview type, which is supposed to *replace* the buffer type. And it does much more than index calculation. So... yeah. Let's not make a technical discussion personal.
Besides, feedback seems to suggest people are happy with this "bloat".
Sorry if you took my comment any serious. It wasn't meant that way. I was only sarcastically suggesting that there are different kinds of users, and to those who don't care about one of the bigger features (in terms of code), it will look like serious code bloat for no good reason. The whole buffer related code is just too excellent an example for that to let the opportunity pass silently. C++ is just another, and I'm sure there are other areas that add a lot of code to the compiler that a substantial subset of our users won't trigger in their whole life. And I'm not saying that's a bad thing.
Stefan ;o)
Stefan ^^^
On Tue, Aug 7, 2012 at 2:53 AM, Stefan Behnel <stefan_ml@behnel.de> wrote:
Hi,
given how ubiquitous manual memory management is in C, I think it would be nice to let Cython generate the exception raising also for C, not only for C++. The difference is this:
cdef extern from "...": char* make_new_buffer() except NULL as MemoryError int append_to_buffer(char* buffer, char* value) \ except -1 as MemoryError
c_buffer = make_new_buffer() # raises MemoryError on NULL append_to_buffer(c_buffer, "testdata") # raises MemoryError on -1 append_to_buffer(c_buffer, "moredata") # raises MemoryError on -1
versus this:
cdef extern from "...": char* make_new_buffer() int append_to_buffer(char* buffer, char* value)
c_buffer = make_new_buffer() if c_buffer is NULL: raise MemoryError() if append_to_buffer(c_buffer, "testdata") == -1: raise MemoryError() if append_to_buffer(c_buffer, "moredata") == -1: raise MemoryError()
I don't think it's necessary to support this for anything but MemoryError, since you'd normally want a formatted error message for everything else.
Alternative colours for the bike shed:
char* make_new_buffer() except NULL raise MemoryError
char* make_new_buffer() raise MemoryError for NULL
char* make_new_buffer() raise MemoryError from NULL
What do you think?
I'm +0.5 (with a preference to the "except NULL as MemoryError" flavor). Basically, it is new syntax to make up for the lack of proper meta programming. Alternatively (just throwing it out there, not sure if it's a good idea), could this be done as a decorator of some sort? That's how one would write such a concept in Python. The need to fix types does constrain things. - Robert
Robert Bradshaw, 09.08.2012 01:45:
On Tue, Aug 7, 2012 at 2:53 AM, Stefan Behnel wrote:
given how ubiquitous manual memory management is in C, I think it would be nice to let Cython generate the exception raising also for C, not only for C++. The difference is this:
cdef extern from "...": char* make_new_buffer() except NULL as MemoryError int append_to_buffer(char* buffer, char* value) \ except -1 as MemoryError
c_buffer = make_new_buffer() # raises MemoryError on NULL append_to_buffer(c_buffer, "testdata") # raises MemoryError on -1 append_to_buffer(c_buffer, "moredata") # raises MemoryError on -1
versus this:
cdef extern from "...": char* make_new_buffer() int append_to_buffer(char* buffer, char* value)
c_buffer = make_new_buffer() if c_buffer is NULL: raise MemoryError() if append_to_buffer(c_buffer, "testdata") == -1: raise MemoryError() if append_to_buffer(c_buffer, "moredata") == -1: raise MemoryError()
I don't think it's necessary to support this for anything but MemoryError, since you'd normally want a formatted error message for everything else.
Alternative colours for the bike shed:
char* make_new_buffer() except NULL raise MemoryError
char* make_new_buffer() raise MemoryError for NULL
char* make_new_buffer() raise MemoryError from NULL
What do you think?
I'm +0.5 (with a preference to the "except NULL as MemoryError" flavor). Basically, it is new syntax to make up for the lack of proper meta programming.
In a way, yes.
Alternatively (just throwing it out there, not sure if it's a good idea), could this be done as a decorator of some sort? That's how one would write such a concept in Python. The need to fix types does constrain things.
A decorator on the external function declaration, you mean? Something like this? @autoraise(MemoryError) char* make_new_buffer() except NULL Or like this? @autoraise(MemoryError, error_value="NULL") char* make_new_buffer() I wouldn't mind. Although there isn't currently a pure Python syntax for external declarations that would benefit from this ... Or did you mean that it should be a user implemented decorator? That would be nice, but even fused types would make a simple case like this rather code heavy, I guess. Stefan
On Wed, Aug 8, 2012 at 10:12 PM, Stefan Behnel <stefan_ml@behnel.de> wrote:
Robert Bradshaw, 09.08.2012 01:45:
On Tue, Aug 7, 2012 at 2:53 AM, Stefan Behnel wrote:
given how ubiquitous manual memory management is in C, I think it would be nice to let Cython generate the exception raising also for C, not only for C++. The difference is this:
cdef extern from "...": char* make_new_buffer() except NULL as MemoryError int append_to_buffer(char* buffer, char* value) \ except -1 as MemoryError
c_buffer = make_new_buffer() # raises MemoryError on NULL append_to_buffer(c_buffer, "testdata") # raises MemoryError on -1 append_to_buffer(c_buffer, "moredata") # raises MemoryError on -1
versus this:
cdef extern from "...": char* make_new_buffer() int append_to_buffer(char* buffer, char* value)
c_buffer = make_new_buffer() if c_buffer is NULL: raise MemoryError() if append_to_buffer(c_buffer, "testdata") == -1: raise MemoryError() if append_to_buffer(c_buffer, "moredata") == -1: raise MemoryError()
I don't think it's necessary to support this for anything but MemoryError, since you'd normally want a formatted error message for everything else.
Alternative colours for the bike shed:
char* make_new_buffer() except NULL raise MemoryError
char* make_new_buffer() raise MemoryError for NULL
char* make_new_buffer() raise MemoryError from NULL
What do you think?
I'm +0.5 (with a preference to the "except NULL as MemoryError" flavor). Basically, it is new syntax to make up for the lack of proper meta programming.
In a way, yes.
Alternatively (just throwing it out there, not sure if it's a good idea), could this be done as a decorator of some sort? That's how one would write such a concept in Python. The need to fix types does constrain things.
A decorator on the external function declaration, you mean? Something like this?
@autoraise(MemoryError) char* make_new_buffer() except NULL
Or like this?
@autoraise(MemoryError, error_value="NULL") char* make_new_buffer()
I wouldn't mind. Although there isn't currently a pure Python syntax for external declarations that would benefit from this ...
I think that's too heavyweight for what is essentially magic.
Or did you mean that it should be a user implemented decorator? That would be nice, but even fused types would make a simple case like this rather code heavy, I guess.
Yeah, that was the vague hope. Even being able to write check(make_new_buffer()[, NULL, MemoryError]) could be another option, if fused types could permit this. Just exploring options, though I'm starting to leaning towards just adding a bit of syntax if this feature is desired. - Robert
participants (3)
-
mark florisson -
Robert Bradshaw -
Stefan Behnel