Skip to content

Commit f52b6cf

Browse files
committed
restructure
1 parent 833810d commit f52b6cf

31 files changed

Lines changed: 144 additions & 158 deletions

SUMMARY.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
## 第 1 部分:序章
44

55
- [前言](README.md)
6-
- [Python 源代码的组织](preface/code-organization.md)
7-
- [Windows 环境下编译 Python](preface/windows-build.md)
8-
- [UNIX/Linux 环境下编译 Python](preface/unix-linux-build.md)
9-
- [修改 Python 源码](preface/modify-code.md)
6+
- [Python 源代码的组织](preface/code-organization/index.md)
7+
- [Windows 环境下编译 Python](preface/windows-build/index.md)
8+
- [UNIX/Linux 环境下编译 Python](preface/unix-linux-build/index.md)
9+
- [修改 Python 源码](preface/modify-code/index.md)
1010

1111
## 第 2 部分:Python 内建对象
1212

13-
- [Python 对象初探](objects/object.md)
14-
- [Python 整数对象](objects/long-object.md)
15-
- [Python 字符串 对象](objects/string-object.md)
16-
- [Python List 对象](objects/list-object.md)
17-
- [Python Dict 对象](objects/dict-object.md)
18-
- [Python Set 对象](objects/set-object.md)
19-
- [实现简版 Python](objects/simple-implementation.md)
13+
- [Python 对象初探](objects/object/index.md)
14+
- [Python 整数对象](objects/long-object/index.md)
15+
- [Python 字符串 对象](objects/string-object/index.md)
16+
- [Python List 对象](objects/list-object/index.md)
17+
- [Python Dict 对象](objects/dict-object/index.md)
18+
- [Python Set 对象](objects/set-object/index.md)
19+
- [实现简版 Python](objects/simple-interpreter/index.md)
2020

2121
## 第 3 部分:Python 虚拟机

book.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,12 @@
5151
"all": ["weibo", "douban", "linkedin", "facebook", "google", "twitter"]
5252
},
5353
"tbfed-pagefooter": {
54-
"copyright": "Copyright © Prodesire 2018",
54+
"copyright": "Copyright © FlagGo 2019",
5555
"modify_label": "该文件修订时间:",
5656
"modify_format": "YYYY-MM-DD HH:mm:ss"
5757
},
5858
"edit-link": {
59-
"base":
60-
"https://github.com/flaggo/python3-source-code-analysis/edit/master",
59+
"base": "https://github.com/flaggo/python3-source-code-analysis/edit/master",
6160
"label": "编辑此页面"
6261
},
6362
"anchor-navigation-ex": {
@@ -71,9 +70,7 @@
7170
"console": "bash",
7271
"shell": "bash"
7372
},
74-
"css": [
75-
"prismjs/themes/prism-okaidia.css"
76-
]
73+
"css": ["prismjs/themes/prism-okaidia.css"]
7774
}
7875
}
7976
}
Lines changed: 53 additions & 55 deletions
Large diffs are not rendered by default.
File renamed without changes.
Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ CPython3 只保留了 `PyLongObject`
1818
typedef struct _longobject PyLongObject; /* Revealed in longintrepr.h */
1919
```
2020

21-
2221
`源文件:`[Include/longintrepr.h](https://github.com/python/cpython/blob/v3.7.0/Include/longintrepr.h#L85)
2322

2423
```c
@@ -57,7 +56,6 @@ struct _longobject {
5756

5857
从源码可以看出 PyLongObject 是变长对象
5958

60-
6159
## 类型对象 PyLong_Type
6260

6361
`源文件:`[Objects/longobject.c](https://github.com/python/cpython/blob/v3.7.0/Objects/longobject.c#L5379)
@@ -205,11 +203,11 @@ long_new_impl(PyTypeObject *type, PyObject *x, PyObject *obase)
205203

206204
从 long_new_impl 函数可以看出有如下几种情况
207205

208-
- x == NULL 且 obase != NULL 调用 PyLong_FromLong
209-
- obase 为NULL 调用 PyNumber_Long
206+
- x == NULL 且 obase != NULL 调用 PyLong_FromLong
207+
- obase 为 NULL 调用 PyNumber_Long
210208
- x 和 obase 都不为 NULL
211-
- PyUnicode 调用PyLong_FromUnicodeObject,最终调用PyLong_FromString
212-
- PyByteArray/PyBytes 调用_PyLong_FromBytes,最终调用PyLong_FromString
209+
- PyUnicode 调用 PyLong_FromUnicodeObject,最终调用 PyLong_FromString
210+
- PyByteArray/PyBytes 调用\_PyLong_FromBytes,最终调用 PyLong_FromString
213211

214212
## 小整数对象
215213

@@ -343,6 +341,7 @@ _PyLong_Init(void)
343341
`源文件:`[Objects/longobject.c](https://github.com/python/cpython/blob/v3.7.0/Objects/longobject.c#L1581)
344342
345343
在 **long_to_decimal_string_internal**中添加如下代码并重新编译安装
344+
346345
```c
347346
// Objects/longobject.c
348347
static int
@@ -372,7 +371,7 @@ long_to_decimal_string_internal(PyObject *aa,
372371
}
373372
```
374373

375-
编译安装后进入python解释器输入如下代码
374+
编译安装后进入 python 解释器输入如下代码
376375

377376
```python
378377
num = 9223372043297226753
@@ -388,9 +387,9 @@ print(num)
388387

389388
如下图所示
390389

391-
![longobject storage](longobject_storage.png)
390+
![longobject storage](longo-storage.png)
392391

393-
注:这里的 30 是由 **PyLong_SHIFT** 决定的,64位系统中**PyLong_SHIFT** 为30,否则 **PyLong_SHIFT** 为15
392+
注:这里的 30 是由 **PyLong_SHIFT** 决定的,64 位系统中**PyLong_SHIFT** 为 30,否则 **PyLong_SHIFT** 为 15
394393

395394
## 整数对象的数值操作
396395

@@ -481,7 +480,7 @@ long_add(PyLongObject *a, PyLongObject *b)
481480
}
482481
```
483482
484-
可以看到整数的加法运算函数long_add根据 a、b的ob_size 又细分为两个函数 (x_add 和 x_sub) 做处理
483+
可以看到整数的加法运算函数 long_add 根据 a、b 的 ob_size 又细分为两个函数 (x_add 和 x_sub) 做处理
485484
486485
`源文件:`[Objects/longobject.c](https://github.com/python/cpython/blob/v3.7.0/Objects/longobject.c#L2991)
487486
@@ -523,10 +522,9 @@ x_add(PyLongObject *a, PyLongObject *b)
523522
}
524523
```
525524

526-
加法运算函数 x_add 从 ob_digit 数组的低位开始依次按位相加,carry做进位处理,然后处理a对象的高位数字,最后使用 long_normalize 函数调整 ob_size,确保ob_digit[abs(ob_size)-1]不为零,这与普通四则运算的加法运算相同,只不过进位单元不同而已
527-
528-
![longobject x_add](longobject_x_add.png)
525+
加法运算函数 x_add 从 ob_digit 数组的低位开始依次按位相加,carry 做进位处理,然后处理 a 对象的高位数字,最后使用 long_normalize 函数调整 ob_size,确保 ob_digit[abs(ob_size)-1]不为零,这与普通四则运算的加法运算相同,只不过进位单元不同而已
529526

527+
![longobject x_add](long-x-add.png)
530528

531529
`源文件:`[Objects/longobject.c](https://github.com/python/cpython/blob/v3.7.0/Objects/longobject.c#L3025)
532530

@@ -593,9 +591,9 @@ x_sub(PyLongObject *a, PyLongObject *b)
593591
```
594592
595593
与普通四则运算减法相同,数不够大则向高一位借位,
596-
减法运算函数 x_sub 的示例图如下,注:PyLong_SHIFT为30
594+
减法运算函数 x_sub 的示例图如下,注:PyLong_SHIFT 为 30
597595
598-
![longobject x_sub](longobject_x_sub.png)
596+
![longobject x_sub](long-x-sub.png)
599597
600598
### 整数相乘
601599
@@ -627,11 +625,10 @@ long_mul(PyLongObject *a, PyLongObject *b)
627625
}
628626
```
629627

630-
k_mul函数是一种快速乘法 [源文件](
631-
https://github.com/python/cpython/blob/v3.7.0/Objects/longobject.c#L3268)
628+
k_mul 函数是一种快速乘法 [源文件](https://github.com/python/cpython/blob/v3.7.0/Objects/longobject.c#L3268)
632629

633-
> Karatsuba的算法主要是用于两个大数的乘法,极大提高了运算效率,相较于普通乘法降低了复杂度,并在其中运用了递归的思想。
634-
> 基本的原理和做法是将位数很多的两个大数x和y分成位数较少的数,每个数都是原来x和y位数的一半
630+
> Karatsuba 的算法主要是用于两个大数的乘法,极大提高了运算效率,相较于普通乘法降低了复杂度,并在其中运用了递归的思想。
631+
> 基本的原理和做法是将位数很多的两个大数 x 和 y 分成位数较少的数,每个数都是原来 x 和 y 位数的一半
635632
> 这样处理之后,简化为做三次乘法,并附带少量的加法操作和移位操作。
636633
637-
具体可以看wiki [Karatsuba算法](https://www.wikiwand.com/zh-hans/Karatsuba算法)的实现
634+
具体可以看 wiki [Karatsuba 算法](https://www.wikiwand.com/zh-hans/Karatsuba算法)的实现

0 commit comments

Comments
 (0)