Guest User

Untitled

a guest
Jul 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. import foo
  2. foo.bar([1, 2, 3])
  3. foo.bar([4, 5])
  4.  
  5. int i,j;
  6. PyArg_ParseTuple(args, "i", &i)
  7. /* or */
  8. PyArg_ParseTuple(args, "(ii)", &i, &j);
  9.  
  10. int *i;
  11. int ni;
  12. PyArg_ParseTuple(args, "(i...)", &ni, i);
  13.  
  14. int
  15. ParseArguments(unsigned long arr[],Py_ssize_t size, PyObject *args) {
  16. /* Get arbitrary number of positive numbers from Py_Tuple */
  17. Py_ssize_t i;
  18. PyObject *temp_p, *temp_p2;
  19.  
  20.  
  21. for (i=0;i<size;i++) {
  22. temp_p = PyTuple_GetItem(args,i);
  23. if(temp_p == NULL) {return NULL;}
  24.  
  25. /* Check if temp_p is numeric */
  26. if (PyNumber_Check(temp_p) != 1) {
  27. PyErr_SetString(PyExc_TypeError,"Non-numeric argument.");
  28. return NULL;
  29. }
  30.  
  31. /* Convert number to python long and than C unsigned long */
  32. temp_p2 = PyNumber_Long(temp_p);
  33. arr[i] = PyLong_AsUnsignedLong(temp_p2);
  34. Py_DECREF(temp_p2);
  35. if (arr[i] == 0) {
  36. PyErr_SetString(PyExc_ValueError,"Zero doesn't allowed as argument.");
  37. return NULL;
  38. }
  39. if (PyErr_Occurred()) {return NULL; }
  40. }
  41.  
  42. return 1;
  43. }
  44.  
  45. static PyObject *
  46. function_name_was_here(PyObject *self, PyObject *args)
  47. {
  48. Py_ssize_t TupleSize = PyTuple_Size(args);
  49. Py_ssize_t i;
  50. struct bigcouples *temp = malloc(sizeof(struct bigcouples));
  51. unsigned long current;
  52.  
  53. if(!TupleSize) {
  54. if(!PyErr_Occurred())
  55. PyErr_SetString(PyExc_TypeError,"You must supply at least one argument.");
  56. free(temp);
  57. return NULL;
  58. }
  59.  
  60. unsigned long *nums = malloc(TupleSize * sizeof(unsigned long));
  61.  
  62. if(!ParseArguments(nums, TupleSize, args)){
  63. /* Make a cleanup and than return null*/
  64. return null;
  65. }
Add Comment
Please, Sign In to add comment