/Doc/c-api/function.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 83 lines · 42 code · 41 blank · 0 comment · 0 complexity · fcd8b5a2e3bd5309644fb2f6fb8dc930 MD5 · raw file

  1. .. highlightlang:: c
  2. .. _function-objects:
  3. Function Objects
  4. ----------------
  5. .. index:: object: function
  6. There are a few functions specific to Python functions.
  7. .. ctype:: PyFunctionObject
  8. The C structure used for functions.
  9. .. cvar:: PyTypeObject PyFunction_Type
  10. .. index:: single: MethodType (in module types)
  11. This is an instance of :ctype:`PyTypeObject` and represents the Python function
  12. type. It is exposed to Python programmers as ``types.FunctionType``.
  13. .. cfunction:: int PyFunction_Check(PyObject *o)
  14. Return true if *o* is a function object (has type :cdata:`PyFunction_Type`).
  15. The parameter must not be *NULL*.
  16. .. cfunction:: PyObject* PyFunction_New(PyObject *code, PyObject *globals)
  17. Return a new function object associated with the code object *code*. *globals*
  18. must be a dictionary with the global variables accessible to the function.
  19. The function's docstring, name and *__module__* are retrieved from the code
  20. object, the argument defaults and closure are set to *NULL*.
  21. .. cfunction:: PyObject* PyFunction_GetCode(PyObject *op)
  22. Return the code object associated with the function object *op*.
  23. .. cfunction:: PyObject* PyFunction_GetGlobals(PyObject *op)
  24. Return the globals dictionary associated with the function object *op*.
  25. .. cfunction:: PyObject* PyFunction_GetModule(PyObject *op)
  26. Return the *__module__* attribute of the function object *op*. This is normally
  27. a string containing the module name, but can be set to any other object by
  28. Python code.
  29. .. cfunction:: PyObject* PyFunction_GetDefaults(PyObject *op)
  30. Return the argument default values of the function object *op*. This can be a
  31. tuple of arguments or *NULL*.
  32. .. cfunction:: int PyFunction_SetDefaults(PyObject *op, PyObject *defaults)
  33. Set the argument default values for the function object *op*. *defaults* must be
  34. *Py_None* or a tuple.
  35. Raises :exc:`SystemError` and returns ``-1`` on failure.
  36. .. cfunction:: PyObject* PyFunction_GetClosure(PyObject *op)
  37. Return the closure associated with the function object *op*. This can be *NULL*
  38. or a tuple of cell objects.
  39. .. cfunction:: int PyFunction_SetClosure(PyObject *op, PyObject *closure)
  40. Set the closure associated with the function object *op*. *closure* must be
  41. *Py_None* or a tuple of cell objects.
  42. Raises :exc:`SystemError` and returns ``-1`` on failure.