This repository has been archived by the owner on Nov 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1416 lines (660 loc) · 48 KB
/
index.html
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html class="theme-next pisces use-motion">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="/vendors/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css"/>
<link href="//fonts.googleapis.com/css?family=Lato:300,400,700,400italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
<link href="/vendors/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />
<link href="/css/main.css?v=5.0.0" rel="stylesheet" type="text/css" />
<meta name="keywords" content="Hexo, NexT" />
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.0" />
<meta name="description" content="Programer | Pythoner | Life fucker">
<meta property="og:type" content="website">
<meta property="og:title" content="remones note">
<meta property="og:url" content="http://remones.me/index.html">
<meta property="og:site_name" content="remones note">
<meta property="og:description" content="Programer | Pythoner | Life fucker">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="remones note">
<meta name="twitter:description" content="Programer | Pythoner | Life fucker">
<script type="text/javascript" id="hexo.configuration">
var NexT = window.NexT || {};
var CONFIG = {
scheme: 'Pisces',
sidebar: '[object Object]',
fancybox: true,
motion: true,
duoshuo: {
userId: 0,
author: '博主'
}
};
</script>
<title> remones note </title>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left
page-home
">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">remones note</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle">to be, or not to be</p>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
<nav class="site-nav">
<ul id="menu" class="menu ">
<li class="menu-item menu-item-home">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-home fa-fw"></i> <br />
首页
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives" rel="section">
<i class="menu-item-icon fa fa-archive fa-fw"></i> <br />
归档
</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags" rel="section">
<i class="menu-item-icon fa fa-tags fa-fw"></i> <br />
标签
</a>
</li>
</ul>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/03/31/AMQP笔记/" itemprop="url">
AMQP笔记
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time itemprop="dateCreated" datetime="2016-03-31T09:53:20+08:00" content="2016-03-31">
2016-03-31
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="AMQP-交换机类型"><a href="#AMQP-交换机类型" class="headerlink" title="AMQP 交换机类型"></a>AMQP 交换机类型</h3><h4 id="direct"><a href="#direct" class="headerlink" title="direct"></a>direct</h4><p>直接匹配消息中的 <code>routing key</code> ,当有一个队列绑定到该交换机时这个路由标准也会被使用。</p>
<h4 id="topic"><a href="#topic" class="headerlink" title="topic"></a>topic</h4><p>模糊匹配 <code>exchange/queue</code> 绑定过程中指定的 <code>routing key</code> 和 <code>routing pattern</code>,<code>routing key</code> 应该是由 <code>.</code> 符号分隔的0或多个词汇,并且还支持使用特殊通配符 <code>*</code> 配匹单个词汇,使用 <code>#</code> 可以匹配0或多个词汇。</p>
<div class="post-more-link text-center">
<a class="btn" href="/2016/03/31/AMQP笔记/#more" rel="contents">
阅读全文 »
</a>
</div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/03/30/Redis笔记/" itemprop="url">
Redis笔记
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time itemprop="dateCreated" datetime="2016-03-30T01:02:39+08:00" content="2016-03-30">
2016-03-30
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>文档 <a href="http://redisdoc.com/">Redis 命令参考</a> 非常详细,学习的内容主要源自于文档。</p>
<h1 id="基础总结篇"><a href="#基础总结篇" class="headerlink" title="基础总结篇"></a>基础总结篇</h1><h2 id="数据结构的操作和特点"><a href="#数据结构的操作和特点" class="headerlink" title="数据结构的操作和特点"></a>数据结构的操作和特点</h2><h3 id="Key"><a href="#Key" class="headerlink" title="Key"></a>Key</h3><p>Redis 里所有单个命令都属于原子操作。</p>
<ul>
<li>查询:<ul>
<li><code>KEYS partten</code>, 可使用通配符<code>?, *, []</code>,时间复杂度 O(n)。 在 key 很多的情况下,每次查询都会很慢,踩过这个坑,慎用。 </li>
</ul>
</li>
<li>序列化和反序列化<ul>
<li><code>DUMP key</code>,得到序列化的值,和RDB格式一致</li>
<li><code>RESTORE key ttl dumped-value [replace]</code>,反序列化。<code>dumped-value</code> 是序列化的值,<code>ttl</code> 设置成 0 表示不超时,<code>replace</code> 在 3.0 之后的版本出现,表示替换已存在的 key</li>
</ul>
</li>
<li>迁移<ul>
<li><code>MIGRATE host port key destination-db timeout [COPY] [REPLACE]</code>,跨实例的迁移数据,主要用于主从备份</li>
<li><code>MOVE key db</code>,在当前实例迁移数据
<div class="post-more-link text-center">
<a class="btn" href="/2016/03/30/Redis笔记/#more" rel="contents">
阅读全文 »
</a>
</div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/03/22/《Python源码剖析》读书笔记-基础类型/" itemprop="url">
《Python源码剖析》读书笔记-基础类型
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time itemprop="dateCreated" datetime="2016-03-22T10:32:49+08:00" content="2016-03-22">
2016-03-22
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="Int"><a href="#Int" class="headerlink" title="Int"></a>Int</h2><h3 id="小整数池"><a href="#小整数池" class="headerlink" title="小整数池"></a>小整数池</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="preprocessor">#<span class="keyword">ifndef</span> NSMALLPOSINTS</span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">define</span> NSMALLPOSINTS <span class="number">257</span> </span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"><span class="preprocessor">#<span class="keyword">ifndef</span> NSMALLNEGINTS</span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">define</span> NSMALLNEGINTS <span class="number">5</span></span></span><br><span class="line"><span class="comment">// 小整数池 [-5, 257) </span></span><br><span class="line"><span class="preprocessor">#<span class="keyword">endif</span></span></span><br><span class="line"></span><br><span class="line"><span class="preprocessor">#<span class="keyword">if</span> NSMALLNEGINTS + NSMALLPOSINTS > <span class="number">0</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">static</span> PyIntObject *small_ints[NSMALLNEGINTS + NSMALLPOSINTS];</span><br><span class="line"><span class="preprocessor">#<span class="keyword">endif</span></span></span><br></pre></td></tr></table></figure>
<p>在 Python解释器运行之初就创建了小整数池,范围在[-5, 257)。<br>
<div class="post-more-link text-center">
<a class="btn" href="/2016/03/22/《Python源码剖析》读书笔记-基础类型/#more" rel="contents">
阅读全文 »
</a>
</div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/03/04/Python 的一些总结/" itemprop="url">
Python的一些个人总结
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time itemprop="dateCreated" datetime="2016-03-04T18:57:53+08:00" content="2016-03-04">
2016-03-04
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>无规律的个人总结,想到什么就写什么。官方FAQ: <a href="https://docs.python.org/2/faq/programming.html">Python Programming FAQ</a></p>
<h2 id="基础类型"><a href="#基础类型" class="headerlink" title="基础类型"></a>基础类型</h2><h3 id="int"><a href="#int" class="headerlink" title="int"></a>int</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">1</span>]: a = <span class="number">10</span></span><br><span class="line">In [<span class="number">2</span>]: a.</span><br><span class="line">int.bit_length int.denominator int.imag int.real</span><br><span class="line">int.conjugate int.from_bytes int.numerator int.to_bytes</span><br></pre></td></tr></table></figure>
<p>部分方法属性:</p>
<table>
<thead>
<tr>
<th style="text-align:left">属性</th>
<th>值</th>
<th>解释</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">bit_length</td>
<td>4</td>
<td>二进制位长</td>
</tr>
<tr>
<td style="text-align:left">denominator</td>
<td>1</td>
<td>分母</td>
</tr>
<tr>
<td style="text-align:left">imag</td>
<td>0</td>
<td>虚数</td>
</tr>
<tr>
<td style="text-align:left">real</td>
<td>10</td>
<td>实数</td>
</tr>
<tr>
<td style="text-align:left">conjugate</td>
<td>10</td>
<td>共轭复数</td>
</tr>
<tr>
<td style="text-align:left">numerator</td>
<td>10</td>
<td>分子</td>
</tr>
</tbody>
</table>
<p>整数池缓存:[-5, 256]对应的整数对象被缓存在一个对象中,避免频繁申请创建对象带来的开销。</p>
<div class="post-more-link text-center">
<a class="btn" href="/2016/03/04/Python 的一些总结/#more" rel="contents">
阅读全文 »
</a>
</div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/03/03/《Python源码剖析》读书笔记-Python虚拟机中的类机制/" itemprop="url">
《Python源码剖析》读书笔记: Python虚拟机中的类机制
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time itemprop="dateCreated" datetime="2016-03-03T18:57:53+08:00" content="2016-03-03">
2016-03-03
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="对象模型概述"><a href="#对象模型概述" class="headerlink" title="对象模型概述"></a>对象模型概述</h3><p>Python 中的对象(new class 诞生以后):</p>
<ul>
<li><code>class</code>对象: Python 中自定义类型</li>
<li><code>instance</code>对象: 由<code>class</code>对象创建的实例</li>
</ul>
<p>Python 中内置类型对象<code>int</code>, <code>str</code>本质都是<code>class</code>,在 Python2.2 之前它们是单独一套<code>type</code>类型,2.2之后类型机制重构后全部变成了<code>class</code>对象。</p>
<p>对象之间对应着两种关系:</p>
<ul>
<li>is-kind-of 关系:基类与子类的关系,子类<code>is kind of</code>基类</li>
<li>is-instance-of 关系:类与实例的关系,实例<code>is instance of</code>类</li>
</ul>
<div class="post-more-link text-center">
<a class="btn" href="/2016/03/03/《Python源码剖析》读书笔记-Python虚拟机中的类机制/#more" rel="contents">
阅读全文 »
</a>
</div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/03/02/Python源码剖析》读书笔记-编译与运行/" itemprop="url">
《Python源码剖析》读书笔记:编译与运行
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time itemprop="dateCreated" datetime="2016-03-02T01:51:27+08:00" content="2016-03-02">
2016-03-02
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="编译期"><a href="#编译期" class="headerlink" title="编译期"></a>编译期</h2><p>Python 解释器在工作时,首先会将代码编译成字节码、字符串和常量组成的静态信息,静态信息在运行时被存储在 PyCodeObject 对象中,运行结束后会将这个对象写入到 pyc 文件中。模块被 import 说明模块需要被复用,这是触发 pyc 文件生成的条件。</p>
<h3 id="PyCodeObject"><a href="#PyCodeObject" class="headerlink" title="PyCodeObject"></a>PyCodeObject</h3><p>一个 Code Block 对应一个 PyCodeObject 对象,模块、类和函数都能产生 Code Block。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">[code.h]</span><br><span class="line"></span><br><span class="line"><span class="keyword">typedef</span> <span class="keyword">struct</span> {</span><br><span class="line"> PyObject_HEAD</span><br><span class="line"> <span class="keyword">int</span> co_argcount; <span class="comment">/* #参数个数,除了 *arg 和 **kwarg */</span></span><br><span class="line"> <span class="keyword">int</span> co_kwonlyargcount; <span class="comment">/* keyword only arguments */</span></span><br><span class="line"> <span class="keyword">int</span> co_nlocals; <span class="comment">/* 局部变量 */</span></span><br><span class="line"> <span class="keyword">int</span> co_stacksize; <span class="comment">/* 用来计算栈空间的*/</span></span><br><span class="line"> <span class="keyword">int</span> co_flags; <span class="comment">/* CO_...开头的常量flag */</span></span><br><span class="line"> PyObject *co_code; <span class="comment">/* 指令码 */</span></span><br><span class="line"> PyObject *co_consts; <span class="comment">/* 用于存储常量 */</span></span><br><span class="line"> PyObject *co_names; <span class="comment">/* 使用了的变量名列表,赋值(变量绑定)不算使用 */</span></span><br><span class="line"> PyObject *co_varnames; <span class="comment">/* 存放 局部变量符号的 tuple */</span></span><br><span class="line"> PyObject *co_freevars; <span class="comment">/* 存放自由变量名的tuple */</span></span><br><span class="line"> PyObject *co_cellvars; <span class="comment">/* 存放cell变量名的tuple */</span></span><br><span class="line"> PyObject *co_filename; <span class="comment">/* 文件地址 */</span></span><br><span class="line"> PyObject *co_name; <span class="comment">/* Code Block name, 函数就是函数名,模块就是模块名*/</span></span><br><span class="line"> <span class="keyword">int</span> co_firstlineno; <span class="comment">/* 代码初始行 */</span></span><br><span class="line"> PyObject *co_lnotab; <span class="comment">/* 指令对应的行号,存储的是与上一行的增量 */</span></span><br><span class="line"> <span class="keyword">void</span> *co_zombieframe; <span class="comment">/* 帧 */</span></span><br><span class="line"> PyObject *co_weakreflist; <span class="comment">/* 引用计数列表 */</span> </span><br><span class="line">} PyCodeObject;</span><br></pre></td></tr></table></figure>
<p><code>PyMarshal_WriteObjectToFile</code>完成写入 PyCodeObject 到 pyc 文件的动作,函数内部实际调用<code>w_object</code>将 Python 代码中出现的对象和对应的<code>TYPE_*</code>标识一一存入对象,比如<code>int</code>对象对应标识<code>TYPE_INT</code>。</p>
<p>作用域及相关信息(free variables、cell variables)也是在编译时期就确定好了,并存于 PyCodeObject 当中。</p>
<p>PyCodeObject 实际上是对应了 Python 中的对象 <code><type 'code'></code></p>
<figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">1</span>]:def <span class="function"><span class="title">foo</span><span class="params">(x, y)</span></span>:</span><br><span class="line"> ...: print x, y</span><br><span class="line"> ...: def <span class="function"><span class="title">bar</span><span class="params">(i, j)</span></span>:</span><br><span class="line"> ...: x, y = <span class="number">1</span>, <span class="number">2</span></span><br><span class="line"> ...: return foo</span><br><span class="line"></span><br><span class="line">In [<span class="number">2</span>]: foo<span class="class">.func_code</span>.</span><br><span class="line">foo<span class="class">.func_code</span><span class="class">.co_argcount</span> foo<span class="class">.func_code</span><span class="class">.co_firstlineno</span> foo<span class="class">.func_code</span><span class="class">.co_names</span></span><br><span class="line">foo<span class="class">.func_code</span><span class="class">.co_cellvars</span> foo<span class="class">.func_code</span><span class="class">.co_flags</span> foo<span class="class">.func_code</span><span class="class">.co_nlocals</span></span><br><span class="line">foo<span class="class">.func_code</span><span class="class">.co_code</span> foo<span class="class">.func_code</span><span class="class">.co_freevars</span> foo<span class="class">.func_code</span><span class="class">.co_stacksize</span></span><br><span class="line">foo<span class="class">.func_code</span><span class="class">.co_consts</span> foo<span class="class">.func_code</span><span class="class">.co_lnotab</span> foo<span class="class">.func_code</span><span class="class">.co_varnames</span></span><br><span class="line">foo<span class="class">.func_code</span><span class="class">.co_filename</span> foo<span class="class">.func_code</span><span class="class">.co_name</span></span><br><span class="line"></span><br><span class="line">In [<span class="number">15</span>]: print <span class="function"><span class="title">type</span><span class="params">(foo.func_code)</span></span></span><br><span class="line"><type <span class="string">'code'</span>></span><br></pre></td></tr></table></figure>
<h2 id="运行期"><a href="#运行期" class="headerlink" title="运行期"></a>运行期</h2><h3 id="执行环境"><a href="#执行环境" class="headerlink" title="执行环境"></a>执行环境</h3><p>Python在运行时,它的虚拟机实际上面对的并不是一个PyCodeObject对象,而是与之对应的帧栈对象 PyFrameObject。</p>
<figure class="highlight gherkin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">[frameobject.h]</span><br><span class="line"></span><br><span class="line">typedef struct _frame {</span><br><span class="line"> PyObject_VAR_HEAD</span><br><span class="line"> struct _frame <span class="keyword">*</span>f_back; /<span class="keyword">*</span> previous frame, or NULL <span class="keyword">*</span>/</span><br><span class="line"> PyCodeObject <span class="keyword">*</span>f_code; /<span class="keyword">*</span> code segment <span class="keyword">*</span>/</span><br><span class="line"> PyObject <span class="keyword">*</span>f_builtins; /<span class="keyword">*</span> builtin symbol table (PyDictObject) <span class="keyword">*</span>/</span><br><span class="line"> PyObject <span class="keyword">*</span>f_globals; /<span class="keyword">*</span> global symbol table (PyDictObject) <span class="keyword">*</span>/</span><br><span class="line"> PyObject <span class="keyword">*</span>f_locals; /<span class="keyword">*</span> local symbol table (any mapping) <span class="keyword">*</span>/</span><br><span class="line"> PyObject <span class="keyword">*</span><span class="keyword">*</span>f_valuestack; /<span class="keyword">*</span> points after the last local <span class="keyword">*</span>/</span><br><span class="line"> PyObject <span class="keyword">*</span><span class="keyword">*</span>f_stacktop;</span><br><span class="line"> ...</span><br><span class="line">} PyFrameObject;</span><br></pre></td></tr></table></figure>
<h3 id="PyFrameObject"><a href="#PyFrameObject" class="headerlink" title="PyFrameObject"></a>PyFrameObject</h3><figure class="highlight gherkin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">[frameobject.h]</span><br><span class="line"></span><br><span class="line">typedef struct _frame {</span><br><span class="line"> PyObject_VAR_HEAD</span><br><span class="line"> struct _frame <span class="keyword">*</span>f_back; /<span class="keyword">*</span>执行环境链上的前一个frame, or NULL <span class="keyword">*</span>/</span><br><span class="line"> PyCodeObject <span class="keyword">*</span>f_code; /<span class="keyword">*</span> PyCodeObject对象 <span class="keyword">*</span>/</span><br><span class="line"> PyObject <span class="keyword">*</span>f_builtins; /<span class="keyword">*</span> 内置符号表(PyDictObject) <span class="keyword">*</span>/</span><br><span class="line"> PyObject <span class="keyword">*</span>f_globals; /<span class="keyword">*</span> 全局符号表(PyDictObject) <span class="keyword">*</span>/</span><br><span class="line"> PyObject <span class="keyword">*</span>f_locals; /<span class="keyword">*</span> 局部符号表 (any mapping) <span class="keyword">*</span>/</span><br><span class="line"> PyObject <span class="keyword">*</span><span class="keyword">*</span>f_valuestack; /<span class="keyword">*</span> 运行时栈的栈底位置 <span class="keyword">*</span>/</span><br><span class="line"> PyObject <span class="keyword">*</span><span class="keyword">*</span>f_stacktop; /<span class="keyword">*</span> 运行时栈的栈顶位置 <span class="keyword">*</span>/</span><br><span class="line"> ...</span><br><span class="line"> //动态内存,维护(局部变量+cell对象集合+free对象集合+运行时栈)所需要的空间</span><br><span class="line"> PyObject <span class="keyword">*</span>f_localsplus[1];</span><br><span class="line">} PyFrameObject;</span><br></pre></td></tr></table></figure>
<p>PyFrameObject 除了 PyCodeObject 信息外还包含执行环境信息。执行环境包含运行时栈(程序执行所需要的内存空间),以及名字空间(builtints locals, globals (k, v)对应变量名和变量值的PyDictObject对象指针)。</p>
<p>Python 会根据调用对象不同,产生很多帧栈对象,这些帧栈联系在一起形成调用栈(与X86的运行时栈对应)。</p>
<p>创建 PyFrameObject 过程</p>
<figure class="highlight armasm"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">[py.c]</span><br><span class="line"><span class="label">PyFrameObject</span> *</span><br><span class="line"><span class="label">PyFrame_New</span>(PyThreadState *<span class="keyword">tstate, </span>PyCodeObject *<span class="preprocessor">code</span>, PyObject *globals,</span><br><span class="line"> PyObject *locals) </span><br><span class="line">{</span><br><span class="line"><span class="label">...</span></span><br><span class="line"></span><br><span class="line"> <span class="preprocessor">else</span> {</span><br><span class="line"> Py_ssize_t extras, ncells, nfrees<span class="comment">;</span></span><br><span class="line"> ncells = PyTuple_GET_SIZE(<span class="preprocessor">code</span>->co_cellvars)<span class="comment">;</span></span><br><span class="line"> nfrees = PyTuple_GET_SIZE(<span class="preprocessor">code</span>->co_freevars)<span class="comment">;</span></span><br><span class="line"> extras = <span class="preprocessor">code</span>->co_stacksize + <span class="preprocessor">code</span>->co_nlocals + ncells +</span><br><span class="line"> nfrees<span class="comment">; // 动态内存区由 co_stacksize, co_nlocals, ncells, nfrees 构成</span></span><br><span class="line"> }</span><br></pre></td></tr></table></figure>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/03/01/Go语言学习笔记/" itemprop="url">
Go语言学习笔记
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time itemprop="dateCreated" datetime="2016-03-01T18:00:10+08:00" content="2016-03-01">
2016-03-01
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h2 id="1-类型"><a href="#1-类型" class="headerlink" title="1.类型"></a>1.类型</h2><h3 id="值类型"><a href="#值类型" class="headerlink" title="值类型"></a>值类型</h3><ul>
<li>基本值类型</li>
</ul>
<table>
<thead>
<tr>
<th>类型</th>
<th style="text-align:center">长度</th>
<th>默认值</th>
</tr>
</thead>
<tbody>
<tr>
<td>bool</td>
<td style="text-align:center">1</td>
<td>false</td>
</tr>
<tr>
<td>byte</td>
<td style="text-align:center">1</td>
<td>0</td>
</tr>
<tr>
<td>rune</td>
<td style="text-align:center">4</td>
<td>0</td>
</tr>
<tr>
<td>int, uint</td>
<td style="text-align:center">4 or 8</td>
<td>0</td>
</tr>
<tr>
<td>int8, uint8</td>
<td style="text-align:center">1</td>
<td>0 </td>
</tr>
<tr>
<td>int16,uint16</td>
<td style="text-align:center">2</td>
<td>0</td>
</tr>
<tr>
<td>int32,uint32</td>
<td style="text-align:center">4</td>
<td>0</td>
</tr>
<tr>
<td>int64,uint64</td>
<td style="text-align:center">8</td>
<td>0</td>
</tr>
<tr>
<td>float32</td>
<td style="text-align:center">4</td>
<td>0.0</td>
</tr>
<tr>
<td>float64</td>
<td style="text-align:center">8</td>
<td>0.0</td>
</tr>
<tr>
<td>complex64</td>
<td style="text-align:center">8</td>
<td>-</td>
</tr>
<tr>
<td>complex128</td>
<td style="text-align:center">16</td>
<td>-</td>
</tr>
<tr>
<td>uintptr</td>
<td style="text-align:center">4 or 8</td>
<td>-</td>
</tr>
<tr>
<td>string</td>
<td style="text-align:center">-</td>
<td>“”</td>
</tr>
</tbody>
</table>
<div class="post-more-link text-center">
<a class="btn" href="/2016/03/01/Go语言学习笔记/#more" rel="contents">
阅读全文 »
</a>
</div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</article>
<article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2016/02/28/Tornado源码分析-三-stack-context/" itemprop="url">
Tornado源码分析(三): stack_context
</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time itemprop="dateCreated" datetime="2016-02-28T23:32:21+08:00" content="2016-02-28">
2016-02-28
</time>
</span>