
Hi,
what am I doing wrong?
C code:
static PyObject *is_prime(PyObject *self, PyObject *value) { if (!PyInt_Check(value)) { PyErr_SetString(PyExc_TypeError, "only integers are acceptable"); return NULL; }
Test (I'm using integer 3):
Thanks

On Sun, May 23, 2010 at 7:40 AM, Vojtěch Rylko <vojta.rylko@seznam.cz> wrote:
The second argument ('value', in your code) is actually a tuple containing the arguments passed to your is_prime() function. You'll need to unpack the individual values from that tuple.
http://docs.python.org/extending/extending.html#extracting-parameters-in-ext...
-- Jon Parise (jon of indelible.org) :: "Scientia potentia est"

But after unpacking, I still cannot detect no-integer type object.
================================= static PyObject *is_prime(PyObject *self, PyObject *arg) { PyObject *value; if (!PyArg_ParseTuple(arg, "O", value)) return NULL;
if (PyInt_Check(value)) {
// never raise (test case - in real its if(!PyInt_Check(value)
which always raise) PyErr_SetString(PyExc_ValueError, "only integers are acceptable"); return NULL; } int number; if (!PyArg_ParseTuple(value, "i", &number)) return NULL;
int result = is_prime_solve(number);
Dne 24.5.2010 8:56, Jon Parise napsal(a):

Vojtěch Rylko, 24.05.2010 13:31:
You should consider giving Cython a try, where the above spells
cdef extern from "yourcode.h":
bint is_prime_solve(int number)
def is_prime(int number):
"""
>>> is_prime(7)
True
"""
return is_prime_solve(number)
It will raise a TypeError for you if a user passes something that can't coerce to an int, or an OverflowError for something that is too large for a C int. However, note that coercion to a C int means calling int() in Python, which also works for float values. If you *really* want to prevent non-int values, you can do this:
def is_prime(number):
if not isinstance(number, int):
raise TypeError("please pass an int")
return is_prime_solve(number)
Stefan

On Mon, May 24, 2010 at 6:31 AM, Vojtěch Rylko <vojta.rylko@seznam.cz>wrote:
PyObject *value; if (!PyArg_ParseTuple(arg, "O", value))
Shouldn't that be "&value"?
Daniel Stutzbach, Ph.D. President, Stutzbach Enterprises, LLC <http://stutzbachenterprises.com>

Vojtěch Rylko, 24.05.2010 13:31:
You should consider giving Cython a try, where the above spells
cdef extern from "yourcode.h":
bint is_prime_solve(int number)
def is_prime(int number):
"""
>>> is_prime(7)
True
"""
return is_prime_solve(number)
It will raise a TypeError for you if a user passes something that can't coerce to an int, or an OverflowError for something that is too large for a C int. However, note that coercion to a C int means calling int() in Python, which also works for float values. If you *really* want to prevent non-int values, you can do this:
def is_prime(number):
if not isinstance(number, int):
raise TypeError("please pass an int")
return is_prime_solve(number)
Stefan

On Sun, May 23, 2010 at 7:40 AM, Vojtěch Rylko <vojta.rylko@seznam.cz> wrote:
The second argument ('value', in your code) is actually a tuple containing the arguments passed to your is_prime() function. You'll need to unpack the individual values from that tuple.
http://docs.python.org/extending/extending.html#extracting-parameters-in-ext...
-- Jon Parise (jon of indelible.org) :: "Scientia potentia est"

But after unpacking, I still cannot detect no-integer type object.
================================= static PyObject *is_prime(PyObject *self, PyObject *arg) { PyObject *value; if (!PyArg_ParseTuple(arg, "O", value)) return NULL;
if (PyInt_Check(value)) {
// never raise (test case - in real its if(!PyInt_Check(value)
which always raise) PyErr_SetString(PyExc_ValueError, "only integers are acceptable"); return NULL; } int number; if (!PyArg_ParseTuple(value, "i", &number)) return NULL;
int result = is_prime_solve(number);
Dne 24.5.2010 8:56, Jon Parise napsal(a):

Vojtěch Rylko, 24.05.2010 13:31:
You should consider giving Cython a try, where the above spells
cdef extern from "yourcode.h":
bint is_prime_solve(int number)
def is_prime(int number):
"""
>>> is_prime(7)
True
"""
return is_prime_solve(number)
It will raise a TypeError for you if a user passes something that can't coerce to an int, or an OverflowError for something that is too large for a C int. However, note that coercion to a C int means calling int() in Python, which also works for float values. If you *really* want to prevent non-int values, you can do this:
def is_prime(number):
if not isinstance(number, int):
raise TypeError("please pass an int")
return is_prime_solve(number)
Stefan

On Mon, May 24, 2010 at 6:31 AM, Vojtěch Rylko <vojta.rylko@seznam.cz>wrote:
PyObject *value; if (!PyArg_ParseTuple(arg, "O", value))
Shouldn't that be "&value"?
Daniel Stutzbach, Ph.D. President, Stutzbach Enterprises, LLC <http://stutzbachenterprises.com>

Vojtěch Rylko, 24.05.2010 13:31:
You should consider giving Cython a try, where the above spells
cdef extern from "yourcode.h":
bint is_prime_solve(int number)
def is_prime(int number):
"""
>>> is_prime(7)
True
"""
return is_prime_solve(number)
It will raise a TypeError for you if a user passes something that can't coerce to an int, or an OverflowError for something that is too large for a C int. However, note that coercion to a C int means calling int() in Python, which also works for float values. If you *really* want to prevent non-int values, you can do this:
def is_prime(number):
if not isinstance(number, int):
raise TypeError("please pass an int")
return is_prime_solve(number)
Stefan
participants (5)
-
Daniel Stutzbach
-
Jon Parise
-
Stefan Behnel
-
Stefan Behnel
-
Vojtěch Rylko