-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.html
More file actions
1012 lines (871 loc) · 36.2 KB
/
Copy pathindex.html
File metadata and controls
1012 lines (871 loc) · 36.2 KB
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 lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Understanding and expanding Python: hands-on experience with the Python internals</title>
<link rel="stylesheet" href="reveal.js/dist/reset.css">
<link rel="stylesheet" href="reveal.js/dist/reveal.css">
<link rel="stylesheet" href="css/personal-theme.css">
<link rel="stylesheet" href="css/qt.css">
<link rel="stylesheet" href="css/extras.css">
<!-- Theme used for syntax highlighted code -->
<link rel="stylesheet" href="reveal.js/plugin/highlight/monokai.css">
<!-- Printing and PDF exports -->
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'reveal.js/css/print/pdf.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script>
</head>
<!-- CONTENT -->
<body>
<div class="reveal">
<div class="slides">
<section data-transition="fade-in fade-out" data-background-image="img/background_cover.svg">
<div id="left70">
<p><i class="qt-neon">Tutorial</i></p>
<h3 style="font-size: 90%;">Understanding and expanding Python: hands-on experience with the Python internals</h3>
<hr class="yellow-line"/>
<p class="x80">
<i class="python-yellow" style="font-size: 3rem;">Cristián</i><br>
maureira.dev<br/><br/>
@cmaureir<br/>
<img class="clean-img" src="img/bluesky.png">
<img class="clean-img" src="img/mastodon.png">
<img class="clean-img" src="img/github_white.png">
<img class="clean-img" src="img/linkedin.png">
</p>
</div>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h2 class="r-fit-text">The <i class="qt-lemon">Structure</i></h2>
<h2 class="r-fit-text">of this tutorial</h2>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<ul>
<li>🧑🏫 The tutorial has <i class="qt-lemon">two parts</i></li>
<li class="fragment">🖵 Each part will have a <i class="qt-lemon">short presentation</i></li>
<li class="fragment">🏃➡️ A <i class="qt-lemon">set of exercises</i> follows each presentation</li>
<li class="fragment">⭐ Some exercises have <i class="qt-neon">optional</i> follow-ups</li>
<li class="fragment">⭐ If you finish quickly, continue with the next one</li>
<li class="fragment">⭐ If you do everything even faster, help others!</li>
</ul>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h3><i class="qt-lemon">First Part Presentation</i></h3>
<h1 class="r-fit-text">Navigating cpython</h1>
</section>
<section data-transition="fade-in fade-out">
<img style="width: 90%;" src="img/interpreters1.png" />
</section>
<section data-transition="fade-in fade-out">
<img style="width: 90%;" src="img/interpreters2.png" />
</section>
<section data-auto-animate data-background-image="img/background_stars.svg"
data-auto-animate="cubic-bezier(0.77, 0, 0.175, 1.0)">
<img data-id="logo1" data-auto-animate-delay="0.0" style="margin: 20px; width: 130px;" src="img/micropython.png" />
<img data-id="logo7" data-auto-animate-delay="0.3" style="margin: 20px; width: 200px;" src="img/logo_graalpy.svg" />
<img data-id="logo3" data-auto-animate-delay="0.6" style="margin: 20px; width: 200px;" src="img/jython.png" />
<img data-id="logo4" data-auto-animate-delay="0.9" style="margin: 20px; width: 200px; background: white; padding: 10px;" src="img/pypy.svg" />
<img data-id="logo5" data-auto-animate-delay="1.2" style="margin: 20px; width: 180px;" src="img/pyston.jpg" />
<img data-id="logo6" data-auto-animate-delay="1.5" style="margin: 20px; width: 200px;" src="img/cinder.svg" />
<img data-id="logo2" data-auto-animate-delay="1.8" style="margin: 20px; width: 100px;" src="img/rustpython.svg" />
<img data-id="logo8" data-auto-animate-delay="2.1" style="margin: 20px; width: 180px;" src="img/logo_mojo.png" />
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h2>CPython is the <i class="qt-lemon">standard</i> implementation</h2>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h3>To get around it</h3>
<h2>We need to understand C</h2>
<h3><i class="fragment qt-lemon r-fit-text">a little bit</i></h2>
</section>
<section data-transition="slide-in fade-out">
<h3>Python and C (1/3)</h3>
<div id="left">
<pre class="fade-in fragment" data-fragment-index="1" style="font-size: 40%;width: 90%;"><code class="python" data-trim data-noescape>
<span class="fragment" data-fragment-index="1"># A comment</span>
<span class="fragment" data-fragment-index="2">import my_module</span>
<span class="fragment" data-fragment-index="3">def add(a: int, b: float) -> float:</span>
<span class="fragment" data-fragment-index="3"> return a + b</span>
<span class="fragment" data-fragment-index="4">def main():</span>
<span class="fragment" data-fragment-index="5"> msg: str = "Hello World"</span>
<span class="fragment" data-fragment-index="5"> x: int = 3</span>
<span class="fragment" data-fragment-index="5"> y: float = 0.14</span>
<span class="fragment" data-fragment-index="6"> z: float = add(x, y)</span>
<span class="fragment" data-fragment-index="6"> print("%f" % z)</span>
<span class="fragment" data-fragment-index="4">if __name__ == "__main__":</span>
<span class="fragment" data-fragment-index="4"> main()</span>
</code></pre>
</div>
<div id="right">
<pre class="fade-in fragment" data-fragment-index="2" style="font-size: 40%;width: 90%;"><code class="c" data-trim data-noescape>
<span class="fragment" data-fragment-index="1">// A comment</span>
<span class="fragment" data-fragment-index="2">#include <my_module.h></span>
<span class="fragment" data-fragment-index="3">float add(int a, float b) {</span>
<span class="fragment" data-fragment-index="3"> return a + b;</span>
<span class="fragment" data-fragment-index="3">}</span>
<span class="fragment" data-fragment-index="4">int main(){</span>
<span class="fragment" data-fragment-index="5"> char msg[] = "Hello World";</span>
<span class="fragment" data-fragment-index="5"> int x = 3;</span>
<span class="fragment" data-fragment-index="5"> float y = 0.14;</span>
<span class="fragment" data-fragment-index="6"> float z = add(x, y);</span>
<span class="fragment" data-fragment-index="6"> printf("%f", z);</span>
<span class="fragment" data-fragment-index="4"> return 0;</span>
<span class="fragment" data-fragment-index="4">}</span>
</code></pre>
</div>
</section>
<section data-transition="fade-in fade-out">
<h3>Python and C (2/3)</h3>
<div id="left" class="fragment">
<pre style="font-size: 50%;width: 95%;"><code class="python" data-trim data-noescape>
l = [1, 2, 3, 4, 5]
l2 = []
l2.append(42)
l2.append(17)
l2.append("Hallo")
</code></pre>
</div>
<div id="right" class="fragment">
<pre style="font-size: 50%;width: 95%;"><code class="c" data-trim data-noescape>
int a[] = {1, 2, 3, 4, 5};
int *a2 = malloc(3 * sizeof(int));
a2[0] = 42;
a2[1] = 17;
a2[2] = "Hallo"; // BOOM! 🔥
</code></pre>
<pre style="font-size: 50%;width: 95%;" class="fragment"><code class="c" data-trim data-noescape>
// Don't forget to free
// the memory you allocated
free(a2);
</code></pre>
</div>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_solarsystem.png">
<h2>Python and C (3/3)</h2>
<div id="left" class="fragment">
<pre style="font-size: 30%;width: 98%;"><code class="c" data-trim data-noescape>
typedef struct {
size_t size;
int *data;
} int_vector;
int *create_vector(size_t n) {
return malloc(n * sizeof(int));
}
void resize_vector(int_vector *v, size_t n) {
v->size = n + 1;
v->data = realloc(v->data, v->size * sizeof(int));
}
void set_vector(int_vector *v, size_t n, int x) {
if(v) {
if(n >= v->size) {
resize_vector(v, n);
}
v->data[n] = x;
}
}
</code></pre>
</div>
<div id="right" class="fragment">
<pre style="font-size: 50%;width: 95%;"><code class="c" data-trim data-noescape>
// Yay, we got "kind of" a vector
int_vector *v = create_vector(10);
set_vector(v, 0, 123);
</code></pre>
<ul>
<li><i class="qt-lemon">We might need to reinvent the wheel</i></li>
</ul>
<p><a href="https://stackoverflow.com/questions/7826163/whats-the-best-c-implementation-of-the-c-vector">source</a></p>
</div>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h1 style="font-size: 500%;">*</h1>
<h3 class="fragment"><code>int *a;</code></h3>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h3>C pointers (1/2)</h3>
<img style="width: 90%;" src="img/pointer.png" />
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h3>C pointers (1/2)</h3>
<img style="width: 90%;" src="img/pointer2.png" />
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<img style="width: 60%;" src="img/pointers_meme.jpg" />
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<img style="width: 60%;" src="img/pointers_meme2.jpg" />
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h2>The <i class="qt-lemon"><code>cpython</code></i> repo structure</h2>
<p><a href="https://github.com/python/cpython">github.com/python/cpython</a></p>
<ul>
<li class="fragment fade-in" data-fragment-index="2"><i class="qt-violet">Doc</i> - Official documentation</li>
<li class="fragment fade-in" data-fragment-index="3"><i class="qt-violet">Include</i> - Interpreter header files</li>
<li class="fragment fade-in" data-fragment-index="4"><i class="qt-violet">Lib</i> - stdlib in pure Python</li>
<li class="fragment fade-in" data-fragment-index="5"><i class="qt-violet">Modules</i> - stdlib in C</li>
<li class="fragment fade-in" data-fragment-index="6"><i class="qt-violet">Objects</i> - built-in types</li>
<li class="fragment fade-in" data-fragment-index="7"><i class="qt-violet">Python</i> - CPython runtime</li>
</ul>
</section>
<section data-transition="fade-in fade-out">
<h3>PyObject (1/2)</h3>
<pre style="font-size: 50%;width: 80%;"><code class="c" data-trim data-noescape>
typedef struct _object {
_PyObject_HEAD_EXTRA
Py_ssize_t ob_refcnt;
struct _typeobject *ob_type;
} PyObject;
</code></pre>
</section>
<section data-transition="fade-in fade-out">
<h3>PyObject (2/2)</h3>
<div id="left">
Using the GIL
<pre style="font-size: 25%;width: 95%;"><code class="c" data-trim data-noescape>
struct _object {
_Py_ANONYMOUS union {
#if SIZEOF_VOID_P > 4
int64_t ob_refcnt_full; /* This field is needed
for efficient initialization
with Clang on ARM */
struct {
# if PY_BIG_ENDIAN
uint16_t ob_flags;
uint16_t ob_overflow;
uint32_t ob_refcnt;
# else
uint32_t ob_refcnt;
uint16_t ob_overflow;
uint16_t ob_flags;
# endif
};
#else
Py_ssize_t ob_refcnt; // part of stable ABI; do not change
#endif
_Py_ALIGNED_DEF(_PyObject_MIN_ALIGNMENT, char) _aligner;
};
PyTypeObject *ob_type; // part of stable ABI; do not change
};
</code></pre>
</div>
<div id="right">
Not using the GIL
<pre style="font-size: 25%;width: 95%;"><code class="c" data-trim data-noescape>
struct _object {
// ob_tid stores the thread id (or zero). It is also used
// by the GC and the trashcan mechanism as a linked list
// pointer and by the GC to store the computed "gc_refs"
// refcount.
_Py_ALIGNED_DEF(_PyObject_MIN_ALIGNMENT, uintptr_t) ob_tid;
uint16_t ob_flags;
PyMutex ob_mutex; // per-object lock
uint8_t ob_gc_bits; // gc-related state
uint32_t ob_ref_local; // local reference count
Py_ssize_t ob_ref_shared; // shared (atomic) reference count
PyTypeObject *ob_type;
};
</code></pre>
</div>
</section>
<section data-transition="fade-in fade-out">
<div id="left">
<pre style="font-size: 20%;width: 90%;"><code class="c" data-trim data-noescape>
typedef struct _typeobject {
PyObject_VAR_HEAD
const char *tp_name; /* For printing, in format "<module>.<name>" */
Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
/* Methods to implement standard operations */
destructor tp_dealloc;
Py_ssize_t tp_vectorcall_offset;
getattrfunc tp_getattr;
setattrfunc tp_setattr;
PyAsyncMethods *tp_as_async; /* formerly known as tp_compare (Python 2)
or tp_reserved (Python 3) */
reprfunc tp_repr;
/* Method suites for standard classes */
PyNumberMethods *tp_as_number;
PySequenceMethods *tp_as_sequence;
PyMappingMethods *tp_as_mapping;
/* More standard operations (here for binary compatibility) */
hashfunc tp_hash;
ternaryfunc tp_call;
reprfunc tp_str;
getattrofunc tp_getattro;
setattrofunc tp_setattro;
/* Functions to access object as input/output buffer */
PyBufferProcs *tp_as_buffer;
/* Flags to define presence of optional/expanded features */
unsigned long tp_flags;
const char *tp_doc; /* Documentation string */
/* Assigned meaning in release 2.0 */
/* call function for all accessible objects */
traverseproc tp_traverse;
/* delete references to contained objects */
inquiry tp_clear;
/* Assigned meaning in release 2.1 */
/* rich comparisons */
richcmpfunc tp_richcompare;
/* weak reference enabler */
Py_ssize_t tp_weaklistoffset;
/* Iterators */
getiterfunc tp_iter;
iternextfunc tp_iternext;
</code></pre>
</div>
<div id="right">
<pre style="font-size: 20%;width: 90%;"><code class="c" data-trim data-noescape>
/* Attribute descriptor and subclassing stuff */
struct PyMethodDef *tp_methods;
struct PyMemberDef *tp_members;
struct PyGetSetDef *tp_getset;
struct _typeobject *tp_base;
PyObject *tp_dict;
descrgetfunc tp_descr_get;
descrsetfunc tp_descr_set;
Py_ssize_t tp_dictoffset;
initproc tp_init;
allocfunc tp_alloc;
newfunc tp_new;
freefunc tp_free; /* Low-level free-memory routine */
inquiry tp_is_gc; /* For PyObject_IS_GC */
PyObject *tp_bases;
PyObject *tp_mro; /* method resolution order */
PyObject *tp_cache;
PyObject *tp_subclasses;
PyObject *tp_weaklist;
destructor tp_del;
/* Type attribute cache version tag. Added in version 2.6 */
unsigned int tp_version_tag;
destructor tp_finalize;
vectorcallfunc tp_vectorcall;
/* bpo-37250: kept for backwards compatibility in CPython 3.8 only */
Py_DEPRECATED(3.8) int (*tp_print)(PyObject *, FILE *, int);
#ifdef COUNT_ALLOCS
/* these must be last and never explicitly initialized */
Py_ssize_t tp_allocs;
Py_ssize_t tp_frees;
Py_ssize_t tp_maxalloc;
struct _typeobject *tp_prev;
struct _typeobject *tp_next;
#endif
} PyTypeObject;
/* The *real* layout of a type object when allocated on the heap */
typedef struct _heaptypeobject {
/* Note: there's a dependency on the order of these members
in slotptr() in typeobject.c . */
PyTypeObject ht_type;
PyAsyncMethods as_async;
PyNumberMethods as_number;
PyMappingMethods as_mapping;
PySequenceMethods as_sequence; /* as_sequence comes after as_mapping,
so that the mapping wins when both
the mapping and the sequence define
a given operator (e.g. __getitem__).
see add_operators() in typeobject.c . */
PyBufferProcs as_buffer;
PyObject *ht_name, *ht_slots, *ht_qualname;
struct _dictkeysobject *ht_cached_keys;
/* here are optional user slots, followed by the members. */
} PyHeapTypeObject;
</code></pre>
</div>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h2 class="r-fit-text">Python C-API</h2>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h3>Strings</h3>
<p>
<a href="https://docs.python.org/3/c-api/unicode.html">docs.python.org/3/c-api/unicode.html</a>
</p>
<pre style="font-size: 70%;width: 80%;"><code class="c" data-trim data-noescape>
char msg[] = "Hello World!";
PyObject *s = PyUnicode_FromString(msg);
...
Py_ssize_t len = PyUnicode_GetLength(s);
</code></pre>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h3>Lists</h3>
<p>
<a href="https://docs.python.org/3/c-api/list.html">docs.python.org/3/c-api/list.html</a>
</p>
<pre style="font-size: 70%;width: 80%;"><code class="c" data-trim data-noescape>
char msg[] = "Hello World!";
PyObject *s = PyUnicode_FromString(msg);
PyObject *l = PyList_New(0);
PyList_Append(l, s);
...
</code></pre>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h3>Check what's the PyObject storing</h3>
<pre style="font-size: 80%;width: 80%;"><code class="c" data-trim data-noescape>
int PyUnicode_Check(PyObject *o);
int PyList_Check(PyObject *o);
int PyDict_Check(PyObject *o);
int PyFloat_Check(PyObject *o);
int PyTuple_Check(PyObject *o);
</code></pre>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h3>Parse arguments</h3>
<pre style="font-size: 50%;width: 100%;"><code class="c" data-trim data-noescape>
static PyObject* glob(PyObject* self, PyObject* args,
PyObject* kwds){
const char *kwlist[] = {"directory", "recursive", 0};
const char *directory = nullptr;
int recursive = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds,
"z|p",
const_cast<char **>(kwlist),
&directory,
&recursive)) {
return nullptr;
}
...
</code></pre>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h3 class="r-fit-text">How does <i class="qt-lemon">C</i><i class="qt-violet">Python</i> works?</h3>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<img style="width: 90%;" src="img/python_diagram.png" />
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<img style="width: 90%;" src="img/python_diagram_2.png" />
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h2>From Python to Machine Code</h2>
<div class="r-hstack justify-center" style="font-size: 70%;">
<div>Bytecode</div>
<div class="fragment" data-fragment-index="2" style="margin-left: 15px;">➡️</div>
<div class="fragment" data-fragment-index="2" style="margin: 40px;">Specialized<br>Bytecode</div>
<div class="fragment" data-fragment-index="3">➡️</div>
<div class="fragment" data-fragment-index="3" style="margin: 40px;">Micro-Op<br>traces</div>
<div class="fragment" data-fragment-index="4">➡️</div>
<div class="fragment" data-fragment-index="4" style="margin: 40px;">Optimized<br>Micro-Op<br>traces</div>
</div>
<p style="font-size: 50%;">
Brandt Bucher: <a href="https://www.youtube.com/watch?v=kMO3Ju0QCDo">Building a JIT compiler for CPython</a> (PyCon US 2024)
</p>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h3><i class="qt-lemon">End of First Part Presentation</i></h3>
<h1 class="r-fit-text">Navigating cpython</h1>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<p class="r-fit-text"><code>git clone https://github.com/cmaureir/cpython_tutorial</code></p>
</section>
<!-- End of presentation -->
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h1 class="r-fit-text">Compiler Setup</h1>
<ul>
<li>Linux: <code>gcc</code></li>
<li>macOS: <code>clang</code> (via XCode)</li>
<li>Windows: <code>cl</code> (via MSVC2022)</li>
</ul>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h1 class="r-fit-text">Environment Setup</h1>
<ul>
<li>Terminal (command-line)</li>
<li>Preferable Linux or macOS</li>
<li>Optional: configure your favorite IDE</li>
</ul>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h1 class="r-fit-text">Exercise 00</h1>
<h3><i class="qt-lemon">Compile a <code><i class="qt-neon">"Hello, World!"</i></code> example in C</i></h3>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h1 class="r-fit-text">Exercise 01</h1>
<h3><i class="qt-lemon">Build <code><i class="qt-neon">cpython</i></code></i></h3>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h3><i class="qt-lemon">Exercise 01</i> - Build cpython</h3>
<p style="font-size: 60%;">
More info: <a href="https://devguide.python.org/getting-started/setup-building/">
devguide.python.org/getting-started/setup-building/</a>
</p>
<div id="left">
<h3>For Linux/macOS</h3>
<pre style="font-size: 60%;width: 90%;"><code class="bash" data-trim data-noescape>
cd cpython
./configure --with-pydebug
make -j16 # or more
./python
</code></pre>
</div>
<div id="right">
<h3>For Windows</h3>
<pre style="font-size: 60%;width: 90%;"><code class="bash" data-trim data-noescape>
cd cpython
PCBuild\build.bat
PCbuild\amd64\python_d.exe
</code></pre>
</div>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h1 class="r-fit-text">Exercise 02</h1>
<h3><i class="qt-lemon">Add a Python function to <code><i class="qt-neon">cpython</i></code></i></h3>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h3 style="font-size: 90%;"><i class="qt-lemon">Exercise 02</i> - New Python function in cpython</h3>
<ul style="font-size: 80%;">
<li>Pick your favorite Python module (Lib/)</li>
<li>Add a new function (maybe based on an existing one)</li>
<ul>
<li>Change parameters defaults</li>
<li>Remove parameters</li>
<li>Add new functionality</li>
</ul>
<li>Recompile (?) cpython and try it out!</li>
</ul>
</section>
<!--
// Replace argument
if (PyUnicode_Compare(args[i], PyUnicode_FromString("Rust")) == 0) {
err = PyFile_WriteObject(PyUnicode_FromString("Python"), file, Py_PRINT_RAW);
} else {
err = PyFile_WriteObject(args[i], file, Py_PRINT_RAW);
}
-->
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h1 class="r-fit-text">Exercise 03</h1>
<h3><i class="qt-lemon">Add a new built-in function to <code><i class="qt-neon">cpython</i></code></i></h3>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h3 class="r-fit-text"><i class="qt-lemon">Exercise 03</i> - New C function in cpython</h3>
<ul>
<li>Consider functionality you know, and change it</li>
<li>For example:
<ul>
<li><i class="qt-neon">pdir()</i> like <i class="qt-neon">dir()</i> but for non_dunder attributes.</li>
</ul>
</li>
</ul>
</section>
<!--
static PyObject *
builtin_pdir(PyObject *self, PyObject *args)
{
PyObject *arg = NULL;
if (!PyArg_UnpackTuple(args, "dir", 0, 1, &arg))
return NULL;
PyObject *pdir_list = PyList_New(0);
PyObject *dir_list = PyObject_Dir(arg);
for (Py_ssize_t i = 0 ; i < PyList_Size(dir_list); i++) {
PyObject *item = PyList_GetItem(dir_list, i);
const char *item_chars = PyUnicode_AsUTF8(item);
if (item_chars[0] == '_' && item_chars[1] == '_')
continue;
if (PyList_Append(pdir_list, item) != 0)
return NULL;
Py_DECREF(item);
}
Py_DECREF(dir_list);
return pdir_list;
}
-->
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h1 class="r-fit-text">Exercise 04</h1>
<h3><i class="qt-lemon">Add a new method to a Python container</i></h3>
</section>
<!--
/*[clinic input]
list.appendappend
object: object
/
Append twice object to the end of the list.
[clinic start generated code]*/
static PyObject *
list_appendappend(PyListObject *self, PyObject *object)
/*[clinic end generated code: output=7c096003a29c0eae input=43a3fe48a7066e91]*/
{
app1(self, object);
app1(self, object);
Py_RETURN_NONE;
}
/*[clinic input]
list.dneppa
index: Py_ssize_t
object: object
/
Insert object before index.
[clinic start generated code]*/
static PyObject *
list_dneppa(PyListObject *self, PyObject *object)
{
if (ins1(self, 0, object) == 0)
Py_RETURN_NONE;
return NULL;
}
-->
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h1 class="r-fit-text">Exercise 05</h1>
<h3><i class="qt-lemon">Add an alias to a Python keyword</i></h3>
</section>
<!--
Grammar/python.gram
-->
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h3><i class="qt-lemon">Second Part Presentation</i></h3>
<h1 class="r-fit-text">Extending cpython</h1>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h2>Creating a Python C-extension</h2>
<p><a href="https://docs.python.org/3/extending/extending.html">docs.python.org/3/extending/extending.html</a></p>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h2>Parts of a C extension (1/4)</h2>
<pre style="font-size: 60%;width: 90%;"><code class="c" data-trim data-noescape>
static PyObject* hello(PyObject* self, PyObject* args) {
char *msg = "Hey there!";
return Py_BuildValue("s", msg);
}
static PyMethodDef the_methods[] = {
{"hello", (PyCFunction)hello, METH_NOARGS, NULL},
{NULL, NULL, 0, NULL}
};
</code></pre>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h2>Parts of a C extension (2/4)</h2>
<pre style="font-size: 70%;width: 80%;"><code class="c" data-trim data-noescape>
static struct PyModuleDef module = {
.m_base = PyModuleDef_HEAD_INIT,
.m_name = "mymodule",
.m_size = 0,
.m_methods = the_methods,
};
</code></pre>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h2>Parts of a C extension (3/4)</h2>
<pre style="font-size: 70%;width: 80%;"><code class="c" data-trim data-noescape>
PyMODINIT_FUNC PyInit_mymodule(void){
return PyModuleDef_Init(&module);
}
</code></pre>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h2>Parts of a C extension (4/4)</h2>
<pre style="font-size: 70%;width: 80%;"><code class="python" data-trim data-noescape>
>>> from mymodule import hello
>>> hello()
'Hey there!'
</code></pre>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h2>Binding generators</h2>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h2>Python: Cython</h2>
<pre style="font-size: 65%; width: 90%;"><code class="python" data-trim data-noescape>
# hello.pyx
def add(int i, int j):
return i + j
</code></pre>
<pre style="font-size: 50%; width: 90%;"><code class="python" data-trim data-noescape>
# setup.py
from setuptools import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize("hello.pyx"))
</code></pre>
<pre style="font-size: 50%; width: 90%;"><code class="bash" data-trim data-noescape>
$ python setup.py build_ext --inplace
</code></pre>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h2>C++: pybind11</h2>
<pre style="font-size: 65%; width: 90%;"><code class="cpp" data-trim data-noescape>
#include <pybind11/pybind11.h>
int add(int i, int j) {
return i + j;
}
PYBIND11_MODULE(example, m) {
// optional module docstring
m.doc() = "pybind11 example plugin";
m.def("add", &add,
"A function that adds two numbers");
}
</code></pre>
<pre style="font-size: 50%; width: 90%;"><code class="bash" data-trim data-noescape>
$ c++ -O3 -Wall -shared -std=c++11 \
-fPIC $(python3 -m pybind11 --includes) example.cpp \
-o example$(python3-config --extension-suffix)
</code></pre>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h2>C++: shiboken</h2>
<div id="left">
<pre style="font-size: 65%; width: 90%;"><code class="cpp" data-trim data-noescape>
// test.cpp
#include <test.hpp>
int add(int i, int j) {
return i + j;
}
</code></pre>
<pre style="font-size: 65%; width: 90%;"><code class="cpp" data-trim data-noescape>
// test.hpp
int add(int i, int j);
</code></pre>
</div>
<div id="right">
<pre style="font-size: 40%; width: 90%;"><code class="xml" data-trim data-noescape>
<!-- bindings.xml -->
<?xml version="1.0"?>
<typesystem package="simple">
<function signature="add(int, int)"/>
</typesystem>
</code></pre>
<pre style="font-size: 50%; width: 90%;"><code class="bash" data-trim data-noescape>
$ cmake -S . -B build
$ cmake --build build
$ cmake --install build
</code></pre>
</div>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h2>Zig: Ziggy Pydust</h2>
<pre style="font-size: 50%; width: 90%;"><code class="zig" data-trim data-noescape>
const py = @import("pydust");
pub fn add(args: struct { a: i32, b: i32 }) i32 {
return args.a + args.b;
}
comptime {
py.rootmodule(@This());
}
</code></pre>
<pre style="font-size: 50%; width: 90%;"><code class="bash" data-trim data-noescape>
$ poetry install
$ poetry run python -c "from hello_zig import hello; print(hello())"
</code></pre>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h2>Rust: PyO3</h2>
<pre style="font-size: 50%;width: 80%;"><code class="rust" data-trim data-noescape>
use pyo3::prelude::*;
/// Formats the sum of two numbers as string.
#[pyfunction]
fn hello(s: &str) -> PyResult<String> {
let msg = format!("Hey there {s}!");
Ok(msg)
}
/// A Python module implemented in Rust.
#[pymodule]
fn mymodule(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(hello, m)?)?;
Ok(())
}
</code></pre>
<pre style="font-size: 50%; width: 80%;"><code class="bash" data-trim data-noescape>
$ maturin develop
</code></pre>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h3><i class="qt-lemon">End of Second Part Presentation</i></h3>
<h1 class="r-fit-text">Extending cpython</h1>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h1 class="r-fit-text">Exercise 06</h1>
<h3><i class="qt-lemon">Create a minimal <code><i class="qt-neon">"Hello, World!"</i></code> extension in C</i></h3>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h1 class="r-fit-text">Exercise 07</h1>
<h3><i class="qt-lemon">Create a minimal <code><i class="qt-neon">"Hello, World!"</i></code> extension in Zig</i></h3>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h1 class="r-fit-text">Exercise 08</h1>
<h3><i class="qt-lemon">Create a minimal <code><i class="qt-neon">"Hello, World!"</i></code> extension in Rust</i></h3>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h1 class="r-fit-text">Exercise 09</h1>
<h3><i class="qt-lemon">Create a new module with one or more functions</i></h3>
</section>
<!--
Offer to pick any language and have a solution for a them
-->
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h1 class="r-fit-text">Exercise 10</h1>
<h3><i class="qt-lemon">Re-write a Python functionality in Rust</i></h3>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_stars.svg">
<h3 class="r-fit-text"><i class="qt-lemon">Exercise 10</i> - Rewrite a Python functionality in Rust</h3>
<ul>
<li>Pick any functionality you like!</li>
<li>Try to keep it minimal and simple first</li>
<li>For example: <i class="qt-neon">glob.glob</i></li>
</ul>
</section>
<section data-transition="fade-in fade-out" data-background-image="img/background_backcover.svg">
<div id="left">
<p><i class="qt-neon">Tutorial</i></p>
<h3 style="font-size: 60%;">Understanding and expanding Python: hands-on experience with the Python internals</h3>
<p class="x80">
Dr. <i class="qt-lemon">Cristián</i> Maureira-Fredes<br>
<hr class="yellow-line"/>
<span>@cmaureir</span><br/>
<img class="clean-img" src="img/bluesky.png">
<img class="clean-img" src="img/mastodon.png">
<img class="clean-img" src="img/github_white.png">
<img class="clean-img" src="img/linkedin.png">
</p>
<img style="width: 30%;" src="img/feedback_qr.png" />
</div>
</section>
<!-- END TUTORIAL -->
</div>
</div>
<script src="reveal.js/dist/reveal.js"></script>
<script src="reveal.js/plugin/notes/notes.js"></script>
<script src="reveal.js/plugin/markdown/markdown.js"></script>
<script src="reveal.js/plugin/highlight/highlight.js"></script>
<script>
// More info about initialization & config:
// - https://revealjs.com/initialization/
// - https://revealjs.com/config/
Reveal.initialize({
hash: true,
// Learn about plugins: https://revealjs.com/plugins/
plugins: [ RevealMarkdown, RevealHighlight, RevealNotes ]
});
</script>
<!-- Hack to have a footer-->
<!-- 1. Style header/footer <div> so they are positioned as desired: in css/qt.css -->
<!-- 2. Create hidden header/footer <div> -->
<div id="hidden" style="display:none;">
<div id="header">
<div id="header-left" style="margin-top: 20px; margin-left: 20px; font-size: 12pt;">
</div>
<div id="header-right"></div>
<div id="footer-left">
<p style="font-size: 16pt;margin-left: 20px;">
<!--<i class="python-yellow">PyCon Portugal 2025 | </i>-->
<i class="python-yellow">EuroPython 2026 | </i>
<b>@cmaureir</b>
</div>
</div>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script type="text/javascript">
// 3. On Reveal.js ready event, copy header/footer <div> into each `.slide-background` <div>