Skip to content

Commit

Permalink
use named capture groups
Browse files Browse the repository at this point in the history
  • Loading branch information
amunger committed Oct 19, 2023
1 parent de13a70 commit 2b721ec
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
30 changes: 12 additions & 18 deletions extensions/notebook-renderers/src/stackTraceHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export function formatStackTrace(stack: string) {
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+)/;
const cellRegex = /(?<prefix>Cell\s+(?:\u001b\[.+?m)?In\s*\[(?<executionCount>\d+)\],\s*)(?<lineLabel>line (?<lineNumber>\d+)).*/;
// older versions of IPython ~8.3.0
const inputRegex = /(Input\s+?(?:\u001b\[.+?m)In\s*\[(\d+)\])(.*)/;
const inputRegex = /(?<prefix>Input\s+?(?:\u001b\[.+?m)(?<cellLabel>In\s*\[(?<executionCount>\d+)\]))(?<postfix>.*)/;

function isIpythonStackTrace(stack: string) {
return cellRegex.test(stack) || inputRegex.test(stack) || fileRegex.test(stack);
Expand Down Expand Up @@ -63,25 +63,19 @@ function linkifyStack(stack: string) {

continue;
} else if (cellRegex.test(original)) {
lines[i] = original.replace(cellRegex, (_s, cellLabel, executionCount, suffix) => {
fileOrCell = { kind: 'cell', path: `vscode-notebook-cell:?execution_count=${stripFormatting(executionCount)}` };
const lineNumberMatch = /line (\d+)/i.exec(suffix);
if (lineNumberMatch) {
suffix = `, <a href='${fileOrCell.path}&line=${lineNumberMatch[1]}'>line ${lineNumberMatch[1]}</a>`;
}
return `<a href='${fileOrCell.path}'>${stripFormatting(cellLabel)}</a>${suffix}`;
});
fileOrCell = {
kind: 'cell',
path: stripFormatting(original.replace(cellRegex, 'vscode-notebook-cell:?execution_count=$<executionCount>'))
};
lines[i] = original.replace(cellRegex, `$<prefix><a href=\'${fileOrCell.path}&line=$<lineNumber>\'>line $<lineNumber></a>`);

continue;
} else if (inputRegex.test(original)) {
lines[i] = original.replace(inputRegex, (_s, cellLabel, executionCount, suffix) => {
fileOrCell = { kind: 'cell', path: `vscode-notebook-cell:?execution_count=${stripFormatting(executionCount)}` };
const lineNumberMatch = /<cell line: (\d+)>/i.exec(suffix);
if (lineNumberMatch) {
suffix = `, <a href='${fileOrCell.path}&line=${lineNumberMatch[1]}'>line ${lineNumberMatch[1]}</a>`;
}
return `<a href='${fileOrCell.path}'>${stripFormatting(cellLabel)}</a>${suffix}`;
});
fileOrCell = {
kind: 'cell',
path: stripFormatting(original.replace(inputRegex, 'vscode-notebook-cell:?execution_count=$<executionCount>'))
};
lines[i] = original.replace(inputRegex, `Input <a href=\'${fileOrCell.path}>\'>$<cellLabel></a>$<postfix>`);

continue;
} else if (!fileOrCell || original.trim() === '') {
Expand Down
43 changes: 32 additions & 11 deletions extensions/notebook-renderers/src/test/stackTraceHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { formatStackTrace } from '../stackTraceHelper';
import * as assert from 'assert';

// The stack frames for these tests can be retreived by using the raw json for a notebook with an error
suite('StackTraceHelper', () => {

test('Non Ipython stack trace is left alone', () => {
Expand All @@ -18,6 +19,11 @@ suite('StackTraceHelper', () => {
assert.equal(formatStackTrace(stack), stack);
});

const formatSequence = /\u001b\[.+?m/g;
function stripAsciiFormatting(text: string) {
return text.replace(formatSequence, '');
}

test('IPython stack line numbers are linkified', () => {
const stack =
'\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n' +
Expand All @@ -31,25 +37,40 @@ suite('StackTraceHelper', () => {
'\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_count=3\'>Cell In[3]</a>') > 0, formatted);
assert.ok(formatted.indexOf('<a href=\'vscode-notebook-cell:?execution_count=3&line=2\'>line 2</a>') > 0, formatted);
assert.ok(formatted.indexOf('<a href=\'vscode-notebook-cell:?execution_count=3&line=2\'>2</a>') > 0, formatted);
assert.ok(formatted.indexOf('<a href=\'C:\\venvs\\myLib.py:2\'>2</a>') > 0, formatted);
const formatted = stripAsciiFormatting(formatStackTrace(stack));
assert.ok(formatted.indexOf('Cell In[3], <a href=\'vscode-notebook-cell:?execution_count=3&line=2\'>line 2</a>') > 0, 'Missing line link in ' + formatted);
assert.ok(formatted.indexOf('<a href=\'vscode-notebook-cell:?execution_count=3&line=2\'>2</a>') > 0, 'Missing frame link in ' + formatted);
assert.ok(formatted.indexOf('<a href=\'C:\\venvs\\myLib.py:2\'>2</a>') > 0, 'Missing frame link in ' + formatted);
});



test('IPython stack line numbers are linkified for IPython 8.3', () => {
// stack frames within functions do not list the line number, i.e.
// 'Input In [1], in myfunc()' vs
// 'Input In [2], in <cell line: 5>()'
const stack =
'\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 4\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mmyLib\u001b[39;00m\n' +
'\u001b[1;32m----> 5\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';
'\u001b[0;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\'\u001b[39m\u001b[38;5;124mipykernel\u001b[39m\u001b[38;5;124m\'\u001b[39m, ipykernel\u001b[38;5;241m.\u001b[39m__version__)\n' +
'\u001b[0;32m 4\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\'\u001b[39m\u001b[38;5;124mipython\u001b[39m\u001b[38;5;124m\'\u001b[39m, IPython\u001b[38;5;241m.\u001b[39m__version__)\n' +
'\u001b[1;32m----> 5\u001b[0m \u001b[43mmyfunc\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n' +
'\n\n' +
'Input \u001b[1;32mIn [1]\u001b[0m, in \u001b[0;36mmyfunc\u001b[1;34m()\u001b[0m\n' +
'\u001b[0;32m 3\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mmyfunc\u001b[39m():\n' +
'\u001b[1;32m----> 4\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\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_count=2\'>Input In [2]</a>') > 0, formatted);
assert.ok(formatted.indexOf('<a href=\'vscode-notebook-cell:?execution_count=2&line=5\'>line 5</a>') > 0, formatted);
assert.ok(formatted.indexOf('<a href=\'vscode-notebook-cell:?execution_count=2&line=5\'>5</a>') > 0, formatted);
const formatted = stripAsciiFormatting(formatStackTrace(stack));
assert.ok(formatted.indexOf('Input <a href=\'vscode-notebook-cell:?execution_count=2>\'>In [2]</a>, in <cell line: 5>') > 0, 'Missing cell link in ' + formatted);
assert.ok(formatted.indexOf('Input <a href=\'vscode-notebook-cell:?execution_count=1>\'>In [1]</a>, in myfunc()') > 0, 'Missing cell link in ' + formatted);
assert.ok(formatted.indexOf('<a href=\'vscode-notebook-cell:?execution_count=2&line=5\'>5</a>') > 0, 'Missing frame link in ' + formatted);
});

test('IPython stack trace lines without associated location are not linkified', () => {
Expand Down

0 comments on commit 2b721ec

Please sign in to comment.