0%

cpython PyObject 继承机制的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
(lldb) call PyUnicode_FromWideChar(L"算法符合", -1)
(_object *) $16 = 0x000001fd3a9e0a50
(lldb) p *$16
(_object) $17 = {
ob_refcnt = 1
ob_type = 0x00007fff35396140
}
(lldb) p *(PyASCIIObject *)$16
(PyASCIIObject) $18 = {
ob_base = {
ob_refcnt = 1
ob_type = 0x00007fff35396140
}
length = 4
hash = -1
state = (interned = 0, kind = 2, compact = 1, ascii = 0, ready = 1)
wstr = 0x000001fd3a9e0a98 L"算法符合"
}
(lldb) p *(PyCompactUnicodeObject *)$16
(PyCompactUnicodeObject) $20 = {
_base = {
ob_base = {
ob_refcnt = 1
ob_type = 0x00007fff35396140
}
length = 4
hash = -1
state = (interned = 0, kind = 2, compact = 1, ascii = 0, ready = 1)
wstr = 0x000001fd3a9e0a98 L"算法符合"
}
utf8_length = 0
utf8 = 0x0000000000000000
wstr_length = 4
}
(lldb) p *(PyUnicodeObject *)$16
(PyUnicodeObject) $19 = {
_base = {
_base = {
ob_base = {
ob_refcnt = 1
ob_type = 0x00007fff35396140
}
length = 4
hash = -1
state = (interned = 0, kind = 2, compact = 1, ascii = 0, ready = 1)
wstr = 0x000001fd3a9e0a98 L"算法符合"
}
utf8_length = 0
utf8 = 0x0000000000000000
wstr_length = 4
}
data = (interned = 3, kind = 5, compact = 0, ascii = 0, ready = 1)
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
PyObject * PyUnicode_FromWideChar(const wchar_t *u, Py_ssize_t size)
PyObject * PyUnicode_New(Py_ssize_t size, Py_UCS4 maxchar)
_PyUnicode_CONVERT_BYTES

struct_size = sizeof(PyCompactUnicodeObject);
if (maxchar < 128) {
kind = PyUnicode_1BYTE_KIND;
char_size = 1;
is_ascii = 1;
struct_size = sizeof(PyASCIIObject);
}

obj = (PyObject *) PyObject_Malloc(struct_size + (size + 1) * char_size);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(lldb) p *(PyASCIIObject *)unicode
(PyASCIIObject) $18 = {
ob_base = {
ob_refcnt = 1
ob_type = 0x00007fff35396140
}
length = 67
hash = -1
state = (interned = 0, kind = 1, compact = 1, ascii = 1, ready = 1)
wstr = 0x0000000000000000
}

do {
unsigned char *_to = (unsigned char *)(((
Py_UCS1
*)((void)((!!(PyType_HasFeature((((PyObject *)(unicode))->ob_type),
(1UL << 28)))) ||
(_wassert(L"PyUnicode_Check(unicode)", L"_file_name_",
(unsigned)(2255)),
0)),
(((PyASCIIObject *)(unicode))->state.compact)
? (((void)((!!(PyType_HasFeature(
(((PyObject *)(unicode))->ob_type),
(1UL << 28)))) ||
(_wassert(L"PyUnicode_Check(unicode)",
L"_file_name_", (unsigned)(2255)),
0)),
(void)((!!((((PyASCIIObject *)unicode)->state.ready))) ||
(_wassert(L"PyUnicode_IS_READY(unicode)",
L"_file_name_", (unsigned)(2255)),
0)),
((PyASCIIObject *)unicode)->state.ascii)
? ((void *)((PyASCIIObject *)(unicode) + 1))
: ((void *)((PyCompactUnicodeObject *)(unicode) + 1)))
: ((void)((!!(((PyUnicodeObject *)(unicode))->data.any)) ||
(_wassert(L"((PyUnicodeObject*)(unicode))->data.any",
L"_file_name_", (unsigned)(2255)),
0)),
((((PyUnicodeObject *)(unicode))->data.any))))));
const Py_UNICODE *_iter = (const Py_UNICODE *)(u);
const Py_UNICODE *_end = (const Py_UNICODE *)(u + size);
Py_ssize_t n = (_end) - (_iter);
const Py_UNICODE *_unrolled_end = _iter + ((size_t)(n) & ~(size_t)((4) - 1));
while (_iter < (_unrolled_end)) {
_to[0] = (unsigned char)_iter[0];
_to[1] = (unsigned char)_iter[1];
_to[2] = (unsigned char)_iter[2];
_to[3] = (unsigned char)_iter[3];
_iter += 4;
_to += 4;
}
while (_iter < (_end))
*_to++ = (unsigned char)*_iter++;
} while (0)


(lldb) p ((void *)((PyASCIIObject *)(unicode) + 1))
(void *) $19 = 0x0000021aa88448e0
(lldb) p ((const char *)((PyASCIIObject *)(unicode) + 1))
(const char *) $20 = 0x0000021aa88448e0 "G:\projects\open_source_projects\cpython\PCbuild\amd64\python_d.exe"