bytesobject.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* A copy of pypy2's PyStringObject */
  2. #ifndef Py_BYTESOBJECT_H
  3. #define Py_BYTESOBJECT_H
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #include <stdarg.h>
  8. #define PyBytes_GET_SIZE(op) PyBytes_Size(op)
  9. /*
  10. Type PyStringObject represents a character string. An extra zero byte is
  11. reserved at the end to ensure it is zero-terminated, but a size is
  12. present so strings with null bytes in them can be represented. This
  13. is an immutable object type.
  14. There are functions to create new string objects, to test
  15. an object for string-ness, and to get the
  16. string value. The latter function returns a null pointer
  17. if the object is not of the proper type.
  18. There is a variant that takes an explicit size as well as a
  19. variant that assumes a zero-terminated string. Note that none of the
  20. functions should be applied to nil objects.
  21. */
  22. /* Caching the hash (ob_shash) saves recalculation of a string's hash value.
  23. Interning strings (ob_sstate) tries to ensure that only one string
  24. object with a given value exists, so equality tests can be one pointer
  25. comparison. This is generally restricted to strings that "look like"
  26. Python identifiers, although the intern() builtin can be used to force
  27. interning of any string.
  28. Together, these sped cpython up by up to 20%, and since they are part of the
  29. "public" interface PyPy must reimpliment them. */
  30. typedef struct {
  31. PyObject_VAR_HEAD
  32. long ob_shash;
  33. int ob_sstate;
  34. char ob_sval[1];
  35. /* Invariants
  36. * ob_sval contains space for 'ob_size+1' elements.
  37. * ob_sval[ob_size] == 0.
  38. * ob_shash is the hash of the string or -1 if not computed yet.
  39. * ob_sstate != 0 iff the string object is in stringobject.c's
  40. * 'interned' dictionary; in this case the two references
  41. * from 'interned' to this object are *not counted* in ob_refcnt.
  42. */
  43. } PyBytesObject;
  44. #define SSTATE_NOT_INTERNED 0
  45. #define SSTATE_INTERNED_MORTAL 1
  46. #define SSTATE_INTERNED_IMMORTAL 2
  47. #define PyString_CHECK_INTERNED(op) (((PyStringObject *)(op))->ob_sstate)
  48. #define PyBytes_Check(op) \
  49. PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_BYTES_SUBCLASS)
  50. #define PyBytes_CheckExact(op) (Py_TYPE(op) == &PyBytes_Type)
  51. PyAPI_FUNC(PyObject *) PyBytes_FromFormatV(const char*, va_list);
  52. PyAPI_FUNC(PyObject *) PyBytes_FromFormat(const char*, ...);
  53. #ifdef __cplusplus
  54. }
  55. #endif
  56. #endif