Skip to content

Commit ca453ab

Browse files
committed
contextvars
1 parent 52aad1e commit ca453ab

5 files changed

Lines changed: 455 additions & 108 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_context.py

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from test.support import threading_helper
1111

1212
try:
13-
from _testcapi import hamt
13+
from _testinternalcapi import hamt
1414
except ImportError:
1515
hamt = None
1616

@@ -42,8 +42,6 @@ def test_context_var_new_1(self):
4242

4343
self.assertNotEqual(hash(c), hash('aaa'))
4444

45-
# TODO: RUSTPYTHON
46-
@unittest.expectedFailure
4745
@isolated_context
4846
def test_context_var_repr_1(self):
4947
c = contextvars.ContextVar('a')
@@ -78,8 +76,6 @@ class MyContext(contextvars.Context):
7876
class MyToken(contextvars.Token):
7977
pass
8078

81-
# TODO: RUSTPYTHON
82-
@unittest.expectedFailure
8379
def test_context_new_1(self):
8480
with self.assertRaisesRegex(TypeError, 'any arguments'):
8581
contextvars.Context(1)
@@ -101,22 +97,16 @@ def test_context_typerrors_1(self):
10197
with self.assertRaisesRegex(TypeError, 'ContextVar key was expected'):
10298
ctx.get(1)
10399

104-
# TODO: RUSTPYTHON
105-
@unittest.expectedFailure
106100
def test_context_get_context_1(self):
107101
ctx = contextvars.copy_context()
108102
self.assertIsInstance(ctx, contextvars.Context)
109103

110-
# TODO: RUSTPYTHON
111-
@unittest.expectedFailure
112104
def test_context_run_1(self):
113105
ctx = contextvars.Context()
114106

115107
with self.assertRaisesRegex(TypeError, 'missing 1 required'):
116108
ctx.run()
117109

118-
# TODO: RUSTPYTHON
119-
@unittest.expectedFailure
120110
def test_context_run_2(self):
121111
ctx = contextvars.Context()
122112

@@ -145,8 +135,6 @@ def func(*args, **kwargs):
145135
((11, 'bar'), {'spam': 'foo'}))
146136
self.assertEqual(a, {})
147137

148-
# TODO: RUSTPYTHON
149-
@unittest.expectedFailure
150138
def test_context_run_3(self):
151139
ctx = contextvars.Context()
152140

@@ -160,8 +148,6 @@ def func(*args, **kwargs):
160148
with self.assertRaises(ZeroDivisionError):
161149
ctx.run(func, 1, 2, a=123)
162150

163-
# TODO: RUSTPYTHON
164-
@unittest.expectedFailure
165151
@isolated_context
166152
def test_context_run_4(self):
167153
ctx1 = contextvars.Context()
@@ -187,8 +173,6 @@ def func1():
187173
self.assertEqual(returned_ctx[var], 'spam')
188174
self.assertIn(var, returned_ctx)
189175

190-
# TODO: RUSTPYTHON
191-
@unittest.expectedFailure
192176
def test_context_run_5(self):
193177
ctx = contextvars.Context()
194178
var = contextvars.ContextVar('var')
@@ -203,8 +187,6 @@ def func():
203187

204188
self.assertIsNone(var.get(None))
205189

206-
# TODO: RUSTPYTHON
207-
@unittest.expectedFailure
208190
def test_context_run_6(self):
209191
ctx = contextvars.Context()
210192
c = contextvars.ContextVar('a', default=0)
@@ -219,8 +201,6 @@ def fun():
219201

220202
ctx.run(fun)
221203

222-
# TODO: RUSTPYTHON
223-
@unittest.expectedFailure
224204
def test_context_run_7(self):
225205
ctx = contextvars.Context()
226206

@@ -230,8 +210,6 @@ def fun():
230210

231211
ctx.run(fun)
232212

233-
# TODO: RUSTPYTHON
234-
@unittest.expectedFailure
235213
@isolated_context
236214
def test_context_getset_1(self):
237215
c = contextvars.ContextVar('c')
@@ -286,8 +264,6 @@ def test_context_getset_1(self):
286264
self.assertEqual(len(ctx2), 0)
287265
self.assertEqual(list(ctx2), [])
288266

289-
# TODO: RUSTPYTHON
290-
@unittest.expectedFailure
291267
@isolated_context
292268
def test_context_getset_2(self):
293269
v1 = contextvars.ContextVar('v1')
@@ -297,8 +273,6 @@ def test_context_getset_2(self):
297273
with self.assertRaisesRegex(ValueError, 'by a different'):
298274
v2.reset(t1)
299275

300-
# TODO: RUSTPYTHON
301-
@unittest.expectedFailure
302276
@isolated_context
303277
def test_context_getset_3(self):
304278
c = contextvars.ContextVar('c', default=42)
@@ -324,8 +298,6 @@ def fun():
324298

325299
ctx.run(fun)
326300

327-
# TODO: RUSTPYTHON
328-
@unittest.expectedFailure
329301
@isolated_context
330302
def test_context_getset_4(self):
331303
c = contextvars.ContextVar('c', default=42)
@@ -336,8 +308,6 @@ def test_context_getset_4(self):
336308
with self.assertRaisesRegex(ValueError, 'different Context'):
337309
c.reset(tok)
338310

339-
# TODO: RUSTPYTHON
340-
@unittest.expectedFailure
341311
@isolated_context
342312
def test_context_getset_5(self):
343313
c = contextvars.ContextVar('c', default=42)
@@ -351,8 +321,6 @@ def fun():
351321
contextvars.copy_context().run(fun)
352322
self.assertEqual(c.get(), [])
353323

354-
# TODO: RUSTPYTHON
355-
@unittest.expectedFailure
356324
def test_context_copy_1(self):
357325
ctx1 = contextvars.Context()
358326
c = contextvars.ContextVar('c', default=42)
@@ -378,8 +346,6 @@ def ctx2_fun():
378346

379347
ctx1.run(ctx1_fun)
380348

381-
# TODO: RUSTPYTHON
382-
@unittest.expectedFailure
383349
@isolated_context
384350
@threading_helper.requires_working_threading()
385351
def test_context_threads_1(self):
@@ -470,7 +436,7 @@ class EqError(Exception):
470436
pass
471437

472438

473-
@unittest.skipIf(hamt is None, '_testcapi lacks "hamt()" function')
439+
@unittest.skipIf(hamt is None, '_testinternalcapi.hamt() not available')
474440
class HamtTest(unittest.TestCase):
475441

476442
def test_hashkey_helper_1(self):

common/src/hash.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ impl HashSecret {
8888
}
8989
}
9090

91+
#[inline]
92+
pub fn hash_pointer(value: usize) -> PyHash {
93+
// TODO: 32bit?
94+
let hash = (value >> 4) | value;
95+
hash as _
96+
}
97+
9198
#[inline]
9299
pub fn hash_float(value: f64) -> Option<PyHash> {
93100
// cpython _Py_HashDouble

stdlib/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ cfg-if = { workspace = true }
2828
crossbeam-utils = { workspace = true }
2929
hex = { workspace = true }
3030
itertools = { workspace = true }
31+
indexmap = { workspace = true }
3132
libc = { workspace = true }
3233
nix = { workspace = true }
3334
num-complex = { workspace = true }
@@ -37,6 +38,7 @@ num-traits = { workspace = true }
3738
num_enum = { workspace = true }
3839
once_cell = { workspace = true }
3940
parking_lot = { workspace = true }
41+
thread_local = { workspace = true }
4042

4143
memchr = { workspace = true }
4244
base64 = "0.13.0"

0 commit comments

Comments
 (0)