[Pyrex] Initialisation of cdef global arrays with constants [resend]

sjmachin at lexicon.net sjmachin at lexicon.net
Sun Jan 30 00:34:32 CET 2005


On 30 Jan 2005 at 9:45, sjmachin at lexicon.net wrote:

> 
> On 29 Jan 2005 at 23:34, Yann Vernier wrote:
> 
> > On Sun, Jan 30, 2005 at 09:28:03AM +1100, sjmachin at lexicon.net wrote:
> > > On 30 Jan 2005 at 9:58, Greg Ewing wrote:
> > > 
> > > > sjmachin at lexicon.net wrote:
> > > > > Where I'm coming from is (my belief/understanding) that a "Python function" hand 
> > > > > coded in C _should_ return NULL in this circumstance, so that the precise exception set 
> > > > > by the called function can be thrown, instead of a guess.
> > > > 
> > > > Oh, I see what you mean. Yes, that's what you would do
> > > > if you were writing an extension module in C by hand.
> > > > But in Pyrex you just use a "raise" statement, and
> > > > Pyrex takes care of returning the NULL.
> > > 
> > > Apparently not: as one might expect, it generates code to do what Python does with a 
> > > bare "raise" statement -- i.e. *RE*raise an already raised exception.
> > > 
> > > In my scenario, PyString_AsStringAndSize has *set* an exception, but it has not yet 
> > > been raised; we need to return NULL back down the call chain until the exception is 
> > > raised.
> > 
> > That's what the cdef function() except NULL form is for, isn't it? This
> > makes Pyrex check for exceptions when the function returns the indicated
> > value, if I understood the docs correctly.
> 
> I see no cdef here.
> 
> From my sample code:
> 
> def tester(s):
> 

But Yann's comment led me to think :

and aarrgghh!! it's as simple as this:

=== strargtest.pyx ===

cdef extern from "Python.h":
   # int PyString_AsStringAndSize(object obj, char **buff, int *length) # -> -1 means error
   # solution was staring me in the face
   int PyString_AsStringAndSize(object obj, char **buff, int *length) except -1


with this result:

>>> strargtest.tester3(9876)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "strargtest.pyx", line 30, in strargtest.tester3
    PyString_AsStringAndSize(s, &ucptr, &slen)
TypeError: expected string or Unicode object, int found





More information about the Pyrex mailing list