Skip to content

Commit

Permalink
option to disable stack trace formatting (#14525)
Browse files Browse the repository at this point in the history
* option to disable stack trace formatting

* fix unit tests
  • Loading branch information
amunger authored Oct 19, 2023
1 parent bd7730f commit 5e6985b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/notebooks/outputs/tracebackFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { injectable } from 'inversify';
import { inject, injectable } from 'inversify';
import { NotebookCell } from 'vscode';
import { ITracebackFormatter } from '../../kernels/types';
import { JupyterNotebookView } from '../../platform/common/constants';
import { getFilePath } from '../../platform/common/platform/fs-paths';
import { DataScience } from '../../platform/common/utils/localize';
import { traceInfoIfCI } from '../../platform/logging';
import { IConfigurationService } from '../../platform/common/types';
const LineNumberMatchRegex = /(;32m[ ->]*?)(\d+)(.*)/g;

/**
* Used to format the traceback of an error in a notebook
*/
@injectable()
export class NotebookTracebackFormatter implements ITracebackFormatter {
constructor(@inject(IConfigurationService) private configurationService: IConfigurationService) {}

public format(cell: NotebookCell, traceback: string[]): string[] {
if (cell.notebook.notebookType !== JupyterNotebookView) {
return traceback;
Expand All @@ -23,7 +26,10 @@ export class NotebookTracebackFormatter implements ITracebackFormatter {
return traceback.map((traceFrame) => this.modifyTracebackFrameIPython(cell, traceFrame));
}
private modifyTracebackFrameIPython(cell: NotebookCell, traceFrame: string): string {
if (/^[Cell|Input|File].*?\n.*/.test(traceFrame)) {
const settings = this.configurationService.getSettings(cell.document.uri);
const formatStackTraces = settings?.formatStackTraces ?? true;

if (formatStackTraces && /^[Cell|Input|File].*?\n.*/.test(traceFrame)) {
return this.modifyTracebackFrameIPython8(cell, traceFrame);
} else {
return traceFrame;
Expand Down
7 changes: 5 additions & 2 deletions src/notebooks/outputs/tracebackFormatter.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import { instance, mock, when } from 'ts-mockito';
import { NotebookCell, NotebookDocument, TextDocument, Uri } from 'vscode';
import { JupyterNotebookView } from '../../platform/common/constants';
import { NotebookTracebackFormatter } from './tracebackFormatter';
import { IConfigurationService } from '../../platform/common/types';

suite(`Notebook trace formatter`, function () {
let notebook: NotebookDocument;
let cell: NotebookCell;
let document: TextDocument;
let config: IConfigurationService;

setup(() => {
config = mock<IConfigurationService>();
notebook = mock<NotebookDocument>();
when(notebook.notebookType).thenReturn(JupyterNotebookView);
cell = mock<NotebookCell>();
Expand All @@ -25,7 +28,7 @@ suite(`Notebook trace formatter`, function () {
});

test('ipython: 8.3.0, ipykernel: 6.13.0', function () {
const formatter = new NotebookTracebackFormatter();
const formatter = new NotebookTracebackFormatter(config);
/**
* To generate the traceback, install a specific version of ipykernel and ipython.
* Then run following code in a cell:
Expand Down Expand Up @@ -54,7 +57,7 @@ suite(`Notebook trace formatter`, function () {
});

test('ipython 8.5.0, ipykernel 6.16.0', function () {
const formatter = new NotebookTracebackFormatter();
const formatter = new NotebookTracebackFormatter(config);
const traceback = [
'---------------------------------------------------------------------------',
'NameError Traceback (most recent call last)',
Expand Down
1 change: 1 addition & 0 deletions src/platform/common/configSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export class JupyterSettings implements IWatchableJupyterSettings {
public excludeUserSitePackages: boolean = false;
public enableExtendedKernelCompletions: boolean = false;
public useOldKernelResolve: boolean = false;
public formatStackTraces: boolean = false;

public variableTooltipFields: IVariableTooltipFields = {
python: {
Expand Down
1 change: 1 addition & 0 deletions src/platform/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export interface IJupyterSettings {
* Added as a fallback in case the new approach of resolving Python env variables for Kernels fails or does not work as expected.
*/
readonly useOldKernelResolve: boolean;
readonly formatStackTraces: boolean;
}

export interface IVariableTooltipFields {
Expand Down

0 comments on commit 5e6985b

Please sign in to comment.