Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,150 @@ def func():

self.assert_events(self.events, events)

class AsyncTracingEventsUnitTest(TracingEventsUnitTest):
def async_trace(self, frame, event, arg):
code = frame.f_code
name = code.co_name
if name in self.names:
self.events.append((frame.f_lineno - self.first_line, name, event))
return self.async_trace

async def async_wrapper(self, afunc, names):
self.names = names
sys.settrace(self.async_trace)
try:
await afunc()
finally:
sys.settrace(None)
self.names = None

def trace_async_function(self, afunc, names):
self.first_line = afunc.__code__.co_firstlineno
self.events = []
asyncio.run(self.async_wrapper(afunc, names))

@unittest.skipIf(
sys.implementation.name == "graalpy",
"GR-65570: GraalPy does not report await exception trace events yet.",
)
def test_01_await(self):
async def helper(): # line -3
return 42

async def afunc():
value = await helper()
return value

self.trace_async_function(afunc, {"afunc", "helper"})

events = [
(0, 'afunc', 'call'),
(1, 'afunc', 'line'),
(-3, 'helper', 'call'),
(-2, 'helper', 'line'),
(-2, 'helper', 'return'),
(1, 'afunc', 'exception'),
(2, 'afunc', 'line'),
(2, 'afunc', 'return'),
]

self.assert_events(self.events, events)

@unittest.skipIf(
sys.implementation.name == "graalpy",
"GR-65570: GraalPy does not report async with exception trace events yet.",
)
def test_02_async_with(self):
class A:
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc, tb):
pass

async def afunc():
async with A():
pass

self.trace_async_function(afunc, {"afunc", "__aenter__", "__aexit__"})

events = [
(0, 'afunc', 'call'),
(1, 'afunc', 'line'),
(-5, '__aenter__', 'call'),
(-4, '__aenter__', 'line'),
(-4, '__aenter__', 'return'),
(1, 'afunc', 'exception'),
(2, 'afunc', 'line'),
(1, 'afunc', 'line'),
(-3, '__aexit__', 'call'),
(-2, '__aexit__', 'line'),
(-2, '__aexit__', 'return'),
(1, 'afunc', 'exception'),
(1, 'afunc', 'return'),
]

self.assert_events(self.events, events)

@unittest.skipIf(
sys.implementation.name == "graalpy",
"GR-65570: GraalPy does not match CPython async for trace events yet.",
)
def test_03_async_for(self):
class AsyncIterator:
def __init__(self, items):
self.items = iter(items)
def __aiter__(self):
return self
async def __anext__(self):
try:
return next(self.items)
except StopIteration:
raise StopAsyncIteration

async def afunc():
total = 0
async for item in AsyncIterator([1, 2]):
total += item
return total

self.trace_async_function(afunc, {"afunc", "__aiter__", "__anext__"})

events = [
(0, 'afunc', 'call'),
(1, 'afunc', 'line'),
(2, 'afunc', 'line'),
(-8, '__aiter__', 'call'),
(-7, '__aiter__', 'line'),
(-7, '__aiter__', 'return'),
(-6, '__anext__', 'call'),
(-5, '__anext__', 'line'),
(-4, '__anext__', 'line'),
(-4, '__anext__', 'return'),
(2, 'afunc', 'exception'),
(3, 'afunc', 'line'),
(2, 'afunc', 'line'),
(-6, '__anext__', 'call'),
(-5, '__anext__', 'line'),
(-4, '__anext__', 'line'),
(-4, '__anext__', 'return'),
(2, 'afunc', 'exception'),
(3, 'afunc', 'line'),
(2, 'afunc', 'line'),
(-6, '__anext__', 'call'),
(-5, '__anext__', 'line'),
(-4, '__anext__', 'line'),
(-4, '__anext__', 'exception'),
(-3, '__anext__', 'line'),
(-2, '__anext__', 'line'),
(-2, '__anext__', 'exception'),
(-2, '__anext__', 'return'),
(2, 'afunc', 'exception'),
(4, 'afunc', 'line'),
(4, 'afunc', 'return'),
]

self.assert_events(self.events, events)

class MultilineCallsTraceTest(TracingEventsUnitTest):
class A:
def m_basic(self, a1, a2, a3):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ test.test_cprofile.CProfileTest.test_output_file_when_changing_directory @ darwi
test.test_cprofile.CProfileTest.test_run_profile_as_module @ darwin-arm64,linux-aarch64,linux-aarch64-github,linux-x86_64,linux-x86_64-github,win32-AMD64,win32-AMD64-github
test.test_cprofile.CProfileTest.test_throw @ darwin-arm64,linux-aarch64,linux-aarch64-github,linux-x86_64,linux-x86_64-github,win32-AMD64,win32-AMD64-github
test.test_cprofile.TestCommandLine.test_sort @ darwin-arm64,linux-aarch64,linux-aarch64-github,linux-x86_64,linux-x86_64-github,win32-AMD64,win32-AMD64-github
# GR-71916
!test.test_profile.ProfileTest.test_calling_conventions @ darwin-arm64,linux-aarch64,linux-x86_64,win32-AMD64
!test.test_profile.ProfileTest.test_cprofile @ darwin-arm64,linux-aarch64,linux-x86_64,win32-AMD64
test.test_profile.ProfileTest.test_calling_conventions @ darwin-arm64,linux-aarch64,linux-x86_64,win32-AMD64
test.test_profile.ProfileTest.test_cprofile @ darwin-arm64,linux-aarch64,linux-x86_64,win32-AMD64
test.test_profile.ProfileTest.test_output_file_when_changing_directory @ darwin-arm64,linux-aarch64,linux-aarch64-github,linux-x86_64,linux-x86_64-github,win32-AMD64,win32-AMD64-github
test.test_profile.ProfileTest.test_run @ darwin-arm64,linux-aarch64,linux-aarch64-github,linux-x86_64,linux-x86_64-github,win32-AMD64,win32-AMD64-github
# This test times out in the gate even though it succeeds locally and in the retagger. Race condition?
!test.test_profile.ProfileTest.test_run_profile_as_module
test.test_profile.ProfileTest.test_runctx @ darwin-arm64,linux-aarch64,linux-aarch64-github,linux-x86_64,linux-x86_64-github,win32-AMD64,win32-AMD64-github
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# GR-71916
!test.test_profile.ProfileTest.test_calling_conventions @ darwin-arm64,linux-aarch64,linux-x86_64,win32-AMD64,win32-AMD64-github
!test.test_profile.ProfileTest.test_cprofile @ darwin-arm64,linux-aarch64,linux-x86_64,win32-AMD64,win32-AMD64-github
test.test_profile.ProfileTest.test_calling_conventions @ darwin-arm64,linux-aarch64,linux-x86_64,win32-AMD64,win32-AMD64-github
test.test_profile.ProfileTest.test_cprofile @ darwin-arm64,linux-aarch64,linux-x86_64,win32-AMD64,win32-AMD64-github
test.test_profile.ProfileTest.test_output_file_when_changing_directory @ darwin-arm64,linux-aarch64,linux-aarch64-github,linux-x86_64,linux-x86_64-github,win32-AMD64,win32-AMD64-github
test.test_profile.ProfileTest.test_run @ darwin-arm64,linux-aarch64,linux-aarch64-github,linux-x86_64,linux-x86_64-github,win32-AMD64,win32-AMD64-github
test.test_profile.ProfileTest.test_run_profile_as_module @ linux-aarch64-github,linux-x86_64-github
!test.test_profile.ProfileTest.test_run_profile_as_module @ darwin-arm64,linux-aarch64,linux-x86_64,win32-AMD64,win32-AMD64-github
test.test_profile.ProfileTest.test_runctx @ darwin-arm64,linux-aarch64,linux-aarch64-github,linux-x86_64,linux-x86_64-github,win32-AMD64,win32-AMD64-github
Original file line number Diff line number Diff line change
Expand Up @@ -1449,10 +1449,11 @@ private BytecodeDSLCodeUnit readBytecodeDSLCodeUnit() {
int selfIndex = readInt();
int yieldFromGeneratorIndex = readInt();
int instrumentationDataIndex = readInt();
int maxProfileCEventStackSize = readInt();

BytecodeSupplier provider = new BytecodeSupplier(serialized, bytecodeFile, bytecodeOffset, bytecodeSize, cacheKey);
return new BytecodeDSLCodeUnit(name, qualname, argCount, kwOnlyArgCount, positionalOnlyArgCount, flags, names, varnames, cellvars, freevars, cell2arg, constants,
startLine, startColumn, endLine, endColumn, classcellIndex, selfIndex, yieldFromGeneratorIndex, instrumentationDataIndex, provider);
startLine, startColumn, endLine, endColumn, classcellIndex, selfIndex, yieldFromGeneratorIndex, instrumentationDataIndex, maxProfileCEventStackSize, provider);
}

private void writeCodeUnit(CodeUnit code) throws IOException {
Expand Down Expand Up @@ -1533,6 +1534,7 @@ private void writeBytecodeDSLCodeUnit(BytecodeDSLCodeUnit code) throws IOExcepti
writeInt(code.selfIndex);
writeInt(code.yieldFromGeneratorIndex);
writeInt(code.instrumentationDataIndex);
writeInt(code.maxProfileCEventStackSize);
}

private PCode readCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,27 +448,26 @@ private static List<PTuple> convertTripleBcisToInstructionIndices(BytecodeNode b

int startInstructionIndex = 0;
int instructionIndex = 0;
boolean wasLastInstructionInstrumentation = false;
boolean rangeHasInstruction = false;
int lastTripleLine = -1;
for (Instruction instruction : bytecodeNode.getInstructions()) {
if (instruction.getBytecodeIndex() == triple[1] /* end bci */) {
if (lastTripleLine != triple[2]) {
if (!wasLastInstructionInstrumentation) {
if (rangeHasInstruction) {
result.add(PFactory.createTuple(language, new int[]{startInstructionIndex, instructionIndex, triple[2]}));
lastTripleLine = triple[2];
}
startInstructionIndex = instructionIndex;
}
triple = triples.get(++tripleIndex);
assert triple[0] == instruction.getBytecodeIndex() : "bytecode ranges should be consecutive";
rangeHasInstruction = false;
}

if (!instruction.isInstrumentation()) {
// Emulate CPython's fixed 2-word instructions.
instructionIndex += 2;
wasLastInstructionInstrumentation = false;
} else {
wasLastInstructionInstrumentation = true;
rangeHasInstruction = true;
}
}

Expand Down
Loading
Loading