Repository navigation
Expand file tree
/
Copy pathCallbackAccessTests.swift
More file actions
576 lines (512 loc) · 19.2 KB
/
Copy pathCallbackAccessTests.swift
File metadata and controls
576 lines (512 loc) · 19.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
//
// Created by Anton Spivak
//
import _NIOFileSystem
private import NIOCore
private import NIOPosix
import Python
import Synchronization
import SystemPackage
import Testing
// MARK: - CallbackAccess
@PythonExportedModule("callback_access")
private enum CallbackAccess {
@PythonFunction
static func sum(_ first: Int, _ second: Int) -> Int {
first + second
}
@PythonFunction
static func members(_ date: Python::DateTime, counter: AccessCounter) async throws -> [Int] {
let year = try date.year
await Task.yield()
try counter.$value { $0 + 1 }
let parsed = try Python::Date.fromISOFormat("2024-02-29")
return try [year, date.month, counter.value, parsed.day]
}
@PythonFunction
static func inspect(
_ python: Python::Context,
_ date: Python::DateTime,
counter: AccessCounter,
) async throws -> [Int] {
let year = try date.year
try counter.$value { $0 + 1 }
await Task.yield()
let month = try date.month
try await python.set(year, forKey: "callback_year")
let saved = try await python.get("callback_year", as: Int.self)
try await python.remove("callback_year")
let created = try Python::DateTime(year: year, month: month, day: 1, in: python)
let path = try Python::Path("reports/today.txt", in: python)
guard try path.name == "today.txt", try created.day == 1, saved == year else {
throw Python::Failure(.valueError, message: "Unexpected native value.")
}
return try [year, month, counter.value]
}
@PythonFunction("read_text")
static func readText(_ path: AccessPath) async throws -> String {
await Task.yield()
return try await path.readText()
}
@PythonFunction("read_text_sync")
static func readTextSynchronously(_ path: AccessPath) async throws -> String {
await Task.yield()
return try path.readTextSynchronously()
}
@PythonFunction
static func spin(_ value: AccessRunner) async throws {
await Task.yield()
try value.spin()
}
@PythonFunction("spin_swift")
static func spinSwift(_ python: Python::Context) throws {
try started(python)
while true {
try Task.checkCancellation()
}
}
@PythonFunction
static func started(_ python: Python::Context) throws {
let signal: AsyncStream<Void>.Continuation = try requireScope(
python,
message: "Missing test signal.",
)
signal.yield()
}
@PythonFunction("saved_year")
static func savedYear(_ python: Python::Context) async throws -> Int {
let date: Python::DateTime = try requireScope(python, message: "Missing date.")
await Task.yield()
return try date.year
}
@PythonFunction
static func hold(_ python: Python::Context, _ descriptor: Int) async throws {
let capture: RetainedCallback = try requireScope(python)
await Task.yield()
capture.state.withLock { $0 = (python, descriptor) }
}
@PythonFunction
static func escape(_ python: Python::Context, _ date: Python::DateTime) async throws {
let capture: EscapedCallback = try requireScope(python)
capture.start { try date.year }
}
@PythonFunction
static func expired(_ python: Python::Context) async throws -> Bool {
let capture: EscapedCallback = try requireScope(python, message: "Missing escaped task.")
guard let task = capture.task.withLock({ $0 }) else {
throw Python::Failure(.valueError, message: "Missing escaped task.")
}
capture.gate.continuation.finish()
do {
_ = try await task.value
return false
} catch let error as Python::Failure where error.kind == .closed {
return true
}
}
@PythonFunction("escape_sync")
static func escapeSynchronously(_ python: Python::Context) throws {
let capture: EscapedCallback = try requireScope(python)
capture.start { try Python::Date.today().year }
}
@PythonFunction
static func nested(_ python: Python::Context) async throws -> Bool {
let capture: EscapedCallback = try requireScope(python, message: "Missing nested session.")
guard let other = capture.session else {
throw Python::Failure(.valueError, message: "Missing nested session.")
}
_ = try await other.evaluate(
"from callback_access import escape_sync; escape_sync()",
scope: capture,
in: python,
)
return try await expired(python)
}
private static func requireScope<Value>(
_ context: Python::Context,
message: String = "Missing test capture.",
) throws -> Value {
guard let value = context.scope as? Value else {
throw Python::Failure(.valueError, message: message)
}
return value
}
}
// MARK: - AccessCounter
@PythonExportedClass("Counter", module: CallbackAccess.self)
private final class AccessCounter: Sendable {
@PythonProperty
var value: Int
@PythonInit
init(_ value: Int) throws {
try $value(value)
}
}
// MARK: - AccessPath
@PythonImportedClass(module: "pathlib", name: "Path")
private struct AccessPath {
@PythonFunction("read_text")
func readText() async throws -> String
@PythonFunction("read_text")
func readTextSynchronously() throws -> String
}
// MARK: - AccessRunner
@PythonImportedClass(module: "builtins", name: "object")
private struct AccessRunner {
@PythonFunction
func spin() throws
}
// MARK: - RetainedCallback
private final class RetainedCallback: Sendable {
let state: Mutex<(context: Python::Context, descriptor: Int)?> = .init(nil)
}
// MARK: - EscapedCallback
private final class EscapedCallback: Sendable {
let session: Python::Session?
let gate: (
stream: AsyncStream<Void>,
continuation: AsyncStream<Void>.Continuation,
) = AsyncStream<Void>.makeStream()
let task: Mutex<Task<Int, any Swift::Error>?> = .init(nil)
init(session: Python::Session? = nil) {
self.session = session
}
func start(_ operation: @escaping @Sendable () throws -> Int) {
let task = Task {
for await _ in self.gate.stream {}
return try operation()
}
self.task.withLock { $0 = task }
}
}
@Test
func nativeMembersWorkAcrossSwiftCallbackSuspension() async throws {
let runtime = try await Python::Runtime()
let session = try await runtime.session(
modules: [CallbackAccess.self],
)
defer { await session.close() }
let result = try await session.evaluate("""
from callback_access import Counter, inspect, members, sum
from datetime import datetime, UTC
counter = Counter(7)
date = datetime(2026, 11, 11, tzinfo=UTC)
assert await inspect(date, counter) == [2026, 11, 8]
assert counter.value == 8
assert sum(20, 22) == 42
assert await members(date, counter) == [2026, 11, 9, 29]
""")
#expect(result.end == .finished)
}
@Test(.timeLimit(.minutes(1)))
func inheritedCallbackAccessExpiresWhenItsExportReturns() async throws {
let capture = EscapedCallback()
let runtime = try await Python::Runtime()
let session = try await runtime.session(modules: [CallbackAccess.self])
defer { await session.close() }
let result = try await session.evaluate(
"""
from callback_access import escape, expired
from datetime import datetime
await escape(datetime(2026, 11, 11))
assert await expired()
""",
scope: capture,
)
#expect(result.end == .finished)
}
@Test(.timeLimit(.minutes(1)))
func synchronousCallbacksCannotInheritAnotherSessionsExport() async throws {
let runtime = try await Python::Runtime()
let first = try await runtime.session(modules: [CallbackAccess.self])
let second = try await runtime.session(modules: [CallbackAccess.self])
defer {
await first.close()
await second.close()
}
let result = try await first.evaluate(
"from callback_access import nested; assert await nested()",
scope: EscapedCallback(session: second),
)
#expect(result.end == .finished)
}
@Test
func nativeCallbackMutationsRemainAfterFailure() async throws {
let runtime = try await Python::Runtime()
let session = try await runtime.session(modules: [CallbackAccess.self])
defer { await session.close() }
_ = try await session.evaluate("""
from callback_access import Counter, inspect
from datetime import datetime, UTC
counter = Counter(7)
date = datetime(2026, 11, 11, tzinfo=UTC)
""")
await #expect(throws: Python::Failure.self) {
try await session.evaluate("""
assert await inspect(date, counter) == [2026, 11, 8]
raise ValueError("state remains")
""")
}
let result = try await session.evaluate("""
assert counter.value == 8
assert await inspect(date, counter) == [2026, 11, 9]
""")
#expect(result.end == .finished)
}
// MARK: - CallbackAccessDelegate
private actor CallbackAccessDelegate: Python::Session.Delegate {
private let allowed: Bool
private(set) var requests: [Python::Session.FileAccessRequest] = []
init(allowed: Bool) {
self.allowed = allowed
}
func shouldAllowFileAccess(_ request: Python::Session.FileAccessRequest) async throws -> Bool {
await Task.yield()
requests.append(request)
return allowed
}
}
@Test(.timeLimit(.minutes(1)), arguments: [true, false])
func nativeCallbackAccessUsesTheFileDelegate(allowed: Bool) async throws {
try await withTemporaryDirectory { directory in
let file = directory.appending("callback.txt")
try await "callback contents".write(toFileAt: file)
let delegate = CallbackAccessDelegate(allowed: allowed)
let runtime = try await Python::Runtime()
let session = try await runtime.session(modules: [CallbackAccess.self], delegate: delegate)
defer { await session.close() }
try await session.set(file.string, forKey: "filename")
try await session.set(allowed ? "callback contents" : "denied", forKey: "expected")
let result = try await session.evaluate(
"""
import asyncio
from callback_access import read_text
from pathlib import Path
loop = asyncio.get_running_loop()
class CallbackPath(Path):
def read_text(self):
assert asyncio.get_running_loop() is loop
return super().read_text()
limit = asyncio.Semaphore(3)
async def read_one():
async with limit:
try:
return await read_text(CallbackPath(filename))
except PermissionError:
return "denied"
values = await asyncio.gather(*(read_one() for _ in range(128)))
assert values == [expected] * 128
""",
scope: "foreign callback",
)
#expect(result.end == .finished)
let reads = await delegate.requests.filter { $0.path == file }
#expect(reads.contains { $0.scope as? String == "foreign callback" })
#expect(reads.count == 128)
}
}
// MARK: - SerialCallerExecutor
private final class SerialCallerExecutor: TaskExecutor {
private let loop: any EventLoop
init(_ loop: any EventLoop) {
self.loop = loop
}
func enqueue(_ job: consuming ExecutorJob) {
let job = UnownedJob(job)
let executor = unsafe asUnownedTaskExecutor()
loop.execute {
unsafe job.runSynchronously(on: executor)
}
}
}
private func evaluateWithSerialCaller(
_ operation: @escaping @Sendable () async throws -> Python::Execution,
) async throws -> Python::Execution {
try await MultiThreadedEventLoopGroup.withEventLoopGroup(numberOfThreads: 2) { group in
let caller = SerialCallerExecutor(group.next())
let pending = Task(executorPreference: caller, operation: operation)
// The second loop can signal interruption even when the caller's thread is blocked.
let watchdog = group.next().scheduleTask(in: .seconds(10)) { pending.cancel() }
defer { watchdog.cancel() }
return try await withTaskCancellationHandler(
operation: {
try await pending.value
},
onCancel: {
pending.cancel()
},
)
}
}
@Test(.timeLimit(.minutes(1)), arguments: [true, false])
func synchronousImportedFileAccessFinishesWithASerialCaller(allowed: Bool) async throws {
try await withTemporaryDirectory { directory in
let file = directory.appending("serial.txt")
try await "contents".write(toFileAt: file)
let delegate = CallbackAccessDelegate(allowed: allowed)
let session = try await Python::Runtime().session(
modules: [CallbackAccess.self],
delegate: delegate,
)
defer { await session.close() }
try await session.set(file.string, forKey: "filename")
try await session.set(allowed, forKey: "allowed")
let result = try await evaluateWithSerialCaller {
try await session.evaluate(
"""
from callback_access import read_text_sync
from pathlib import Path
try:
contents = await read_text_sync(Path(filename))
except PermissionError:
assert not allowed
else:
assert allowed and contents == 'contents'
""",
scope: "serial caller",
)
}
#expect(result.end == .finished)
let requests = await delegate.requests.filter { $0.path == file }
#expect(requests.count == 1)
#expect(requests.first?.scope as? String == "serial caller")
#expect(try await session.evaluate("6 * 7").output.stdout == "42\n")
}
}
@Test(.timeLimit(.minutes(1)))
func synchronousImportedMethodsCanReenterAsyncSwiftExports() async throws {
let session = try await Python::Runtime().session(modules: [CallbackAccess.self])
defer { await session.close() }
try await session.initialize()
let result = try await evaluateWithSerialCaller {
try await session.evaluate("""
import asyncio
from callback_access import spin
returned = []
class Runner:
def __init__(self, depth):
self.depth = depth
def spin(self):
if self.depth:
asyncio.run(spin(Runner(self.depth - 1)))
returned.append(self.depth)
await spin(Runner(3))
assert returned == [0, 1, 2, 3]
""")
}
#expect(result.end == .finished)
}
@Test(.timeLimit(.minutes(1)))
func interruptionReachesEveryAttachedThread() async throws {
let signal = AsyncStream<Void>.makeStream(bufferingPolicy: .bufferingNewest(2))
let runtime = try await Python::Runtime()
let session = try await runtime.session(modules: [CallbackAccess.self])
defer { await session.close() }
try await session.initialize()
let interrupted = try await runUntilInterrupted(
whenReady: signal,
signals: 2,
) {
try await session.evaluate(
"""
import asyncio
from callback_access import spin, started
class Runner:
def spin(self):
started()
while True:
pass
await asyncio.gather(spin(Runner()), spin(Runner()))
""",
scope: signal.continuation,
)
}
#expect(interrupted.end == .stopped)
let next = try await session.evaluate("assert 'Runner' in globals()")
#expect(next.end == .finished)
}
@Test(.timeLimit(.minutes(1)))
func synchronousSwiftExportsUseTaskCancellation() async throws {
let signal = AsyncStream<Void>.makeStream(bufferingPolicy: .bufferingNewest(1))
let runtime = try await Python::Runtime()
let session = try await runtime.session(modules: [CallbackAccess.self])
defer { await session.close() }
try await session.initialize()
let result = try await runUntilInterrupted(whenReady: signal) {
try await session.evaluate(
"from callback_access import spin_swift; spin_swift()",
scope: signal.continuation,
)
}
#expect(result.end == .stopped)
#expect(try await session.evaluate("6 * 7").output.stdout == "42\n")
}
@Test
func nativeCallbackRejectsReferencesFromAnExpiredGeneration() async throws {
let runtime = try await Python::Runtime()
let session = try await runtime.session(modules: [CallbackAccess.self])
defer { await session.close() }
_ = try await session.evaluate("from datetime import datetime; date = datetime(2026, 11, 11)")
let date = try await session.get("date", as: Python::DateTime.self)
_ = try await session.evaluate(
"from callback_access import saved_year; assert await saved_year() == 2026",
scope: date,
)
try await session.reset()
let result = try await session.evaluate(
"""
from callback_access import saved_year
try:
await saved_year()
except ReferenceError:
pass
else:
raise AssertionError("An expired reference was accepted")
""",
scope: date,
)
#expect(result.end == .finished)
await session.close()
#expect(throws: Python::Failure.self) { try date.year }
}
@Test
func nativeCallbackStreamsCloseEvenWhenItsContextIsRetained() async throws {
try await withTemporaryDirectory { directory in
let output = directory.appending("stdout.txt")
let capture = RetainedCallback()
let runtime = try await Python::Runtime()
let session = try await runtime.session(modules: [CallbackAccess.self])
defer { await session.close() }
_ = try await session.evaluate(
"""
import sys
from callback_access import hold, spin
class Writer:
def spin(self):
print("native output")
await hold(sys.stdout.fileno())
await spin(Writer())
""",
scope: capture,
redirecting: [.stdout: output],
)
let retained = try #require(capture.state.withLock { $0 })
#expect(retained.context.scope == nil)
let descriptor = FileDescriptor(rawValue: Int32(retained.descriptor))
try expectDescriptorReleased(descriptor, for: output)
let bytes = try await FileSystem.shared.withFileHandle(forReadingAt: output) { handle in
try await handle.readChunk(fromAbsoluteOffset: 0, length: .bytes(1024))
}
#expect(Array(bytes.readableBytesView) == Array("native output\n".utf8))
}
}
private func expectDescriptorReleased(_ descriptor: FileDescriptor, for path: FilePath) throws {
let outputInfo = try path.stat()
do {
// Parallel tests can reuse a closed descriptor number for a different file.
let current = try descriptor.stat()
#expect(current.deviceID != outputInfo.deviceID || current.inode != outputInfo.inode)
} catch {
#expect(error == .badFileDescriptor)
}
}