Skip to content

Commit 5a94b1f

Browse files
committed
新增 整数对象的一些说明
1 parent a846191 commit 5a94b1f

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

objects/long-object.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,18 @@ typedef struct _longobject PyLongObject; /* Revealed in longintrepr.h */
3737
In a normalized number, ob_digit[abs(ob_size)-1] (the most significant
3838
digit) is never zero. Also, in all cases, for all valid i,
3939
40+
在一个规范的数字ob_digit[abs(ob_size)-1]()永不为0。而且,所有有效的 i 都满足以下要求
4041
0 <= ob_digit[i] <= MASK.
4142
4243
The allocation function takes care of allocating extra memory
4344
so that ob_digit[0] ... ob_digit[abs(ob_size)-1] are actually available.
4445
46+
内存分配函数要小心额外的内存,
47+
4548
CAUTION: Generic code manipulating subtypes of PyVarObject has to
46-
aware that ints abuse ob_size's sign bit.
49+
aware that ints abuse ob_size's sign bit.
50+
51+
警告: 通用代码操作 PyVarObject 的子类型必须注意 ob_size的符号滥用问题。
4752
*/
4853

4954
struct _longobject {
@@ -154,8 +159,29 @@ get_small_int(sdigit ival)
154159
} while(0)
155160
```
156161
157-
宏**CHECK_SMALL_INT**会检查传入的数是否在小整数范围内,如果是直接返回
162+
宏 **CHECK_SMALL_INT** 会检查传入的数是否在小整数范围内,如果是直接返回。
163+
可以在创建或复制整数对象等函数中找到 **CHECK_SMALL_INT** 的身影,以下只列出了
164+
**PyLong_FromLong**,就不一一列举了
165+
166+
`源文件:`[Objects/longobject.c](https://github.com/python/cpython/blob/v3.7.0/Objects/longobject.c#L239)
167+
168+
```c
169+
// Object/longobject.c
170+
171+
PyObject *
172+
PyLong_FromLong(long ival)
173+
{
174+
PyLongObject *v;
175+
unsigned long abs_ival;
176+
unsigned long t; /* unsigned so >> doesn't propagate sign bit */
177+
int ndigits = 0;
178+
int sign;
179+
180+
CHECK_SMALL_INT(ival);
158181
182+
...
183+
}
184+
```
159185

160186
### 小整数初始化
161187

@@ -266,4 +292,4 @@ print(num)
266292

267293
![longobject storage](longobject_storage.png)
268294

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

0 commit comments

Comments
 (0)