Skip to content

Commit 6e40413

Browse files
authored
Merge pull request #411 from bozhodimitrov/ep584-585
Examples for episode 584 & 585
2 parents 84950df + de30220 commit 6e40413

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-0
lines changed

sample_code/ep584/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# [debugging flaky cascading failure after upgrading pytest!](https://youtu.be/zyZXdvJgGPM)
2+
3+
another deep dive debugging session! I utilize a dirty bisect to figure out what introduced the behaviour. from there test pollution helped me find a reproduction and then I isolated what was interfering and utilized a new technique to find _why_!
4+
5+
## Setup commands
6+
7+
```bash
8+
git clone git@github.com:pytest-dev/pytest
9+
git clone git@github.com:getsentry/sentry
10+
```
11+
12+
## Interactive examples
13+
14+
### Python
15+
16+
```python
17+
import sys
18+
import pytest
19+
20+
21+
@pytest.fixture(autouse=True, scope='session')
22+
def settrace():
23+
with open(f'trace.{os.getpid()}', 'a+') as f:
24+
def tracefunc(frame, event, arg):
25+
if (
26+
event == 'call' and
27+
'/new_migrations/monkey/' not in frame.f_code.co_filename and
28+
any(
29+
s in frame.f_code.co_filename
30+
for s in (
31+
'/src/sentry/testutils/', '/tests/sentry/',
32+
'/django/test/', '/pytest_django/',
33+
)
34+
):
35+
f.write(f'{frame.f_code.co_filename}:{frame.f_code.co_name}\n')
36+
37+
orig = sys.settrace(tracefunc)
38+
yield
39+
sys.settrace(orig)
40+
```
41+
42+
### Bash
43+
44+
Session 1:
45+
46+
```bash
47+
cd pytest/
48+
git log --oneline --first-parent 8.1.0..8.2.0
49+
git log --oneline --first-parent 8.1.0..8.2.0 -- src
50+
```
51+
52+
Session 2:
53+
54+
```bash
55+
cd sentry/
56+
pytest --setup-plan tests/sentry/utils/test_math.py | less
57+
58+
diff -u planbefore planafter | less
59+
jq . last-failed-list
60+
diff -u trace.old trace.new | less
61+
62+
virtualenv venv
63+
./venv/bin/pip install pytest pytest-rerunfailures
64+
./venv/bin/pytest --reruns=2 -s t.py
65+
```

sample_code/ep584/rev01/t.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from unittest import TestCase
2+
3+
g = True
4+
5+
6+
class TestCaseTest(TestCase):
7+
@classmethod
8+
def tearDownClass(cls):
9+
print('class teardown!')
10+
11+
def test(self):
12+
global g
13+
print('test!')
14+
if g:
15+
print('flaky fail!')
16+
g = False
17+
raise AssertionError('#')

sample_code/ep585/README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# [how does python's module `__getattr__` actually work?](https://youtu.be/K1-wYUSQoF8)
2+
3+
today I walk you through a bit of a deep dive into how modules work, what we used to do before this feature, and where the magic lives in cpython!
4+
5+
## Setup commands
6+
7+
```bash
8+
git@github.com:python/cpython
9+
```
10+
11+
## Interactive examples
12+
13+
### Python
14+
15+
```python
16+
import t
17+
t.foo
18+
19+
import sys
20+
sys.modules
21+
sorted(sys.modules)
22+
23+
class C:
24+
def __getattr__(self, x):
25+
print(f'looking up {x}')
26+
return x * 2
27+
28+
sys.modules['c'] = C()
29+
import c
30+
c
31+
c.foo
32+
33+
src = open('/usr/lib/python3.13/typing.py').read()
34+
globs = {'__name__': 'typing'}
35+
exec(src, globs)
36+
37+
typing = type('typing', (), globs)
38+
typing
39+
40+
typing.OrderedDict
41+
typing.Match
42+
43+
typing.__getattr__
44+
typing().Match
45+
46+
import sys
47+
import os
48+
os
49+
import typing
50+
typing
51+
type(typing)
52+
53+
import types
54+
types.ModuleType
55+
56+
type(os)
57+
import sys
58+
type(sys)
59+
60+
import _decimal
61+
type(_decimal)
62+
```
63+
64+
### Bash
65+
66+
```bash
67+
python3
68+
69+
git clone git@github.com:asottile/flake8-typing-imports
70+
cd flake8-typing-imports/
71+
72+
nano flake8_typing_imports.py
73+
nano bin/build-generated
74+
75+
git log -- bin/build-generated
76+
git checkout <commit_hash>
77+
nano bin/build-generated
78+
79+
python 3.13 -m pydoc typing
80+
nano /usr/lib/python3.13/typing.py
81+
82+
python3.13
83+
84+
cd ../cpython/
85+
ls Python/
86+
ls Objects/
87+
ls Modules/
88+
89+
nano Objects/moduleobject.c
90+
91+
cd ../flake8-typing-imports/
92+
git checkout main
93+
nano bin/build-generated
94+
```

sample_code/ep585/rev01/t.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def __getattr__(x: str) -> object:
2+
print(f'looking for {x}')
3+
return x * 2

0 commit comments

Comments
 (0)