简体   繁体   中英

How do I check input variable validity from a Python extension?

I have a Python extension written in C++ that expects a bytearray as the first parameter. It works correctly but if I pass something else from Python like a string or a number, I get a segmentation fault. How do I check if the input is really a bytearray and then raise an Exception if it's something else?

This should help: http://docs.python.org/2/faq/extending.html#id8

To test the type of an object, first make sure it isn't NULL , and then use PyString_Check() , PyTuple_Check() , PyList_Check() , etc.

Specifically, it looks like you want this: http://docs.python.org/2/c-api/bytearray.html#type-check-macros

int PyByteArray_Check(PyObject *o)

Return true if the object o is a bytearray object or an instance of a subtype of the bytearray type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM