|
| 1 | +# 11주차 |
| 2 | +## str type 분석 |
| 3 | +```c |
| 4 | +Objects/object.c |
| 5 | + |
| 6 | +INIT_TYPE(&PyUnicode_Type, "str"); |
| 7 | +``` |
| 8 | +- python의 str은 PyUnicode_Type 을 서용함 |
| 9 | +
|
| 10 | +```c |
| 11 | +Objects\unicodeobject.c |
| 12 | +
|
| 13 | +PyTypeObject PyUnicode_Type = { |
| 14 | + PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 15 | + "str", /* tp_name */ |
| 16 | + sizeof(PyUnicodeObject), /* tp_basicsize */ |
| 17 | + 0, /* tp_itemsize */ |
| 18 | + /* Slots */ |
| 19 | + (destructor)unicode_dealloc, /* tp_dealloc */ |
| 20 | + 0, /* tp_vectorcall_offset */ |
| 21 | + 0, /* tp_getattr */ |
| 22 | + 0, /* tp_setattr */ |
| 23 | + 0, /* tp_as_async */ |
| 24 | + unicode_repr, /* tp_repr */ |
| 25 | + &unicode_as_number, /* tp_as_number */ |
| 26 | + &unicode_as_sequence, /* tp_as_sequence */ |
| 27 | + &unicode_as_mapping, /* tp_as_mapping */ |
| 28 | + (hashfunc) unicode_hash, /* tp_hash*/ |
| 29 | + 0, /* tp_call*/ |
| 30 | + (reprfunc) unicode_str, /* tp_str */ |
| 31 | + PyObject_GenericGetAttr, /* tp_getattro */ |
| 32 | + 0, /* tp_setattro */ |
| 33 | + 0, /* tp_as_buffer */ |
| 34 | + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 35 | + Py_TPFLAGS_UNICODE_SUBCLASS, /* tp_flags */ |
| 36 | + unicode_doc, /* tp_doc */ |
| 37 | + 0, /* tp_traverse */ |
| 38 | + 0, /* tp_clear */ |
| 39 | + PyUnicode_RichCompare, /* tp_richcompare */ |
| 40 | + 0, /* tp_weaklistoffset */ |
| 41 | + unicode_iter, /* tp_iter */ |
| 42 | + 0, /* tp_iternext */ |
| 43 | + unicode_methods, /* tp_methods */ |
| 44 | + 0, /* tp_members */ |
| 45 | + 0, /* tp_getset */ |
| 46 | + &PyBaseObject_Type, /* tp_base */ |
| 47 | + 0, /* tp_dict */ |
| 48 | + 0, /* tp_descr_get */ |
| 49 | + 0, /* tp_descr_set */ |
| 50 | + 0, /* tp_dictoffset */ |
| 51 | + 0, /* tp_init */ |
| 52 | + 0, /* tp_alloc */ |
| 53 | + unicode_new, /* tp_new */ |
| 54 | + PyObject_Del, /* tp_free */ |
| 55 | +}; |
| 56 | +``` |
| 57 | +- type 정의 |
| 58 | + |
| 59 | +```c |
| 60 | +/* Strings allocated through PyUnicode_FromUnicode(NULL, len) use the |
| 61 | + PyUnicodeObject structure. The actual string data is initially in the wstr |
| 62 | + block, and copied into the data block using _PyUnicode_Ready. */ |
| 63 | +typedef struct { |
| 64 | + PyCompactUnicodeObject _base; |
| 65 | + union { |
| 66 | + void *any; |
| 67 | + Py_UCS1 *latin1; |
| 68 | + Py_UCS2 *ucs2; |
| 69 | + Py_UCS4 *ucs4; |
| 70 | + } data; /* Canonical, smallest-form Unicode buffer */ |
| 71 | +} PyUnicodeObject; |
| 72 | + |
| 73 | +``` |
| 74 | +- 구조체 |
0 commit comments