older Ipython, failing test

This commit is contained in:
Aaron Munger 2023-10-16 15:57:14 -07:00 committed by Aaron Munger
parent fe8ac3f89e
commit 4540b9ba1f
2 changed files with 62 additions and 28 deletions

View file

@ -22,21 +22,27 @@ export function formatStackTrace(stack: string) {
return `${prefix}${num}${suffix}\n`;
});
if (isIpythonStackTrace(stack)) {
return linkifyStack(stack);
if (isIpythonStackTrace(cleaned)) {
return linkifyStack(cleaned);
}
return cleaned;
}
const fileRegex = /^File\s+(.+):\d+/;
const lineNumberRegex = /([ ->]*?)(\d+)(.*)/;
const cellRegex = /^(Cell\s+In\[(\d+)\])(,\s+line \d+)$/;
const formatSequence = /\u001b\[.+?m/g;
const fileRegex = /File\s+(?:\u001b\[.+?m)?(.+):(\d+)/;
const lineNumberRegex = /((?:\u001b\[.+?m)?[ ->]*?)(\d+)(.*)/;
const cellRegex = /(Cell\s+(?:\u001b\[.+?m)?In\s*\[(\d+)\])(,\s*line \d+)/;
// older versions of IPython ~8.3.0
const inputRegex = /(Input\s+?(?:\u001b\[.+?m)In\s*\[(\d+)\])(.*?)/;
function isIpythonStackTrace(stack: string) {
// at least one group will point to the Cell within the notebook
const cellIdentifier = /^Cell In\[\d+\], line \d+$/gm;
return cellIdentifier.test(stack);
return cellRegex.test(stack);
}
function stripFormatting(text: string) {
return text.replace(formatSequence, '');
}
function linkifyStack(stack: string) {
@ -50,22 +56,34 @@ function linkifyStack(stack: string) {
console.log(`linkify ${original}`); // REMOVE
if (fileRegex.test(original)) {
const fileMatch = lines[i].match(fileRegex);
fileOrCell = fileMatch![1];
fileOrCell = stripFormatting(fileMatch![1]);
console.log(`matched file ${fileOrCell}`); // REMOVE
continue;
} else if (cellRegex.test(original)) {
lines[i] = original.replace(cellRegex, (_s, cellLabel, executionCount, suffix) => {
fileOrCell = `vscode-notebook-cell:?execution=${executionCount}`;
return `<a href='${fileOrCell}'>${cellLabel}</a>${suffix}`;
fileOrCell = `vscode-notebook-cell:?execution=${stripFormatting(executionCount)}`;
return `<a href='${fileOrCell}'>${stripFormatting(cellLabel)}</a>${suffix}`;
});
console.log(`matched cell ${fileOrCell}`); // REMOVE
continue;
} else if (inputRegex.test(original)) {
lines[i] = original.replace(inputRegex, (_s, cellLabel, executionCount, suffix) => {
fileOrCell = `vscode-notebook-cell:?execution=${stripFormatting(executionCount)}`;
return `<a href='${fileOrCell}'>${stripFormatting(cellLabel)}</a>${suffix}`;
});
console.log(`matched cell ${fileOrCell}`); // REMOVE
continue;
} else if (!fileOrCell || original.trim() === '') {
// we don't have a location, so don't linkify anything
fileOrCell = undefined;
continue;
} else if (lineNumberRegex.test(original)) {
console.log(`linkify line ${original}`); // REMOVE
lines[i] = original.replace(lineNumberRegex, (_s, prefix, num, suffix) => {
return `${prefix}<a href='${fileOrCell}:${num}'>${num}</a>${suffix}`;
});
console.log(`matched line ${lines[i]}`); // REMOVE
continue;
}
}

View file

@ -20,15 +20,16 @@ suite('StackTraceHelper', () => {
test('IPython stack line numbers are linkified', () => {
const stack =
'---------------------------------------------------------------------------\n' +
'Exception Traceback(most recent call last)\n' +
'Cell In[3], line 2\n' +
' 1 import myLib\n' +
'----> 2 myLib.throwEx()\n' +
'\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n' +
'\u001b[1;31mException\u001b[0m Traceback (most recent call last)\n' +
'Cell \u001b[1;32mIn[3], line 2\u001b[0m\n' +
'\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mmyLib\u001b[39;00m\n' +
'\u001b[1;32m----> 2\u001b[0m \u001b[43mmyLib\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mthrowEx\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n' +
'\n' +
'File C:\\venvs\\myLib.py:2, in throwEx()\n' +
' 1 def throwEx():\n' +
'----> 2 raise Exception\n';
'File \u001b[1;32mC:\\venvs\\myLib.py:2\u001b[0m, in \u001b[0;36mthrowEx\u001b[1;34m()\u001b[0m\n' +
'\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mthrowEx\u001b[39m():\n' +
'\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m\n\n' +
'\u001b[1;31mException\u001b[0m\n:';
const formatted = formatStackTrace(stack);
assert.ok(formatted.indexOf('<a href=\'vscode-notebook-cell:?execution=3\'>Cell In[3]</a>') > 0, formatted);
@ -36,18 +37,33 @@ suite('StackTraceHelper', () => {
assert.ok(formatted.indexOf('<a href=\'C:\\venvs\\myLib.py:2\'>2</a>') > 0, formatted);
});
test('IPython stack trace lines without associated location are not linkified', () => {
test('IPython stack line numbers are linkified for IPython 8.3', () => {
const stack =
'---------------------------------------------------------------------------\n' +
'Exception Traceback(most recent call last)\n' +
'File C:\\venvs\\myLib.py:2, in throwEx()\n' +
'\n' +
'unkown reference' +
' 1 import myLib\n' + // trace lines without an associated file
'----> 2 myLib.throwEx()\n';
'\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n' +
'\u001b[1;31mException\u001b[0m Traceback (most recent call last)\n' +
'Input \u001b[1;32mIn [2]\u001b[0m, in \u001b[0;36m<cell line: 5>\u001b[1;34m()\u001b[0m\n' +
'\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mmyLib\u001b[39;00m\n' +
'\u001b[1;32m----> 2\u001b[0m \u001b[43mmyLib\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mthrowEx\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n';
const formatted = formatStackTrace(stack);
assert.ok(formatted.indexOf('<a') === -1, formatted);
assert.ok(formatted.indexOf('<a href=\'vscode-notebook-cell:?execution=3\'>Input [2]</a>') > 0, formatted);
assert.ok(formatted.indexOf('<a href=\'vscode-notebook-cell:?execution=3:2\'>2</a>') > 0, formatted);
assert.ok(formatted.indexOf('<a href=\'C:\\venvs\\myLib.py:2\'>2</a>') > 0, formatted);
});
test('IPython stack trace lines without associated location are not linkified', () => {
const stack =
'\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n' +
'\u001b[1;31mException\u001b[0m Traceback (most recent call last)\n' +
'Cell \u001b[1;32mIn[3], line 2\u001b[0m\n' +
'\n' +
'unknown source\n' +
'\u001b[0;32m 1\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mthrowEx\u001b[39m():\n' +
'\u001b[1;32m----> 2\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m\n\n' +
'\u001b[1;31mException\u001b[0m\n:';
const formatted = formatStackTrace(stack);
assert.ok(!/<a href=.*>\d<\/a>/.test(formatted), formatted);
});
});