Skip to content
Open
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
33 changes: 13 additions & 20 deletions sphinxcontrib/jupyter/writers/translate_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,6 @@ def depart_document(self, node):
self.output.metadata.celltoolbar = "Slideshow"


# Update metadata
if self.jupyter_kernels is not None:
try:
self.output.metadata.kernelspec = \
self.jupyter_kernels[self.lang]["kernelspec"]
self.output.metadata["filename"] = self.source_file_name.split("/")[-1]
self.output.metadata["title"] = self.title
except:
self.warn(
"Invalid jupyter kernels. "
"jupyter_kernels: {}, lang: {}"
.format(self.jupyter_kernels, self.lang))

def visit_highlightlang(self, node):
lang = node.attributes["lang"].strip()
if lang in self.jupyter_kernels:
Expand Down Expand Up @@ -205,14 +192,20 @@ def visit_literal_block(self, node):
self.in_code_block = True
self.code_lines = []

# If the cell being processed contains code written in a language other than the one that
# was specified as the default language, do not create a code block for it - turn it into
# markup instead.
if self.nodelang != self.langTranslator.translate(self.lang):
# Update metadata
if self.jupyter_kernels is not None and not self.output.metadata:
Copy link

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition not self.output.metadata will be false if metadata exists but is empty (e.g., an empty dict). This could prevent kernelspec updates for subsequent code cells. Consider using a more specific check like tracking whether kernelspec has been set.

Copilot uses AI. Check for mistakes.

if self.nodelang in self.jupyter_lang_synonyms:
pass
else:
self.output_cell_type = JupyterOutputCellGenerators.MARKDOWN
self.nodelang = self.default_lang
try:
self.output.metadata.kernelspec = \
self.jupyter_kernels[self.nodelang]["kernelspec"]
self.output.metadata["filename"] = self.source_file_name.split("/")[-1]
self.output.metadata["title"] = self.title
except:
Copy link

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a bare except: clause catches all exceptions including system exits and keyboard interrupts. Consider catching specific exceptions like KeyError or AttributeError instead.

Suggested change
except:
except (KeyError, AttributeError, TypeError):

Copilot uses AI. Check for mistakes.

self.warn(
"Invalid jupyter kernels. "
"jupyter_kernels: {}, lang: {}"
.format(self.jupyter_kernels, self.nodelang))
Copy link

Copilot AI Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modifying self.nodelang here could have unintended side effects since this variable appears to represent the language of the current node. Consider using a separate variable for the kernelspec language determination.

Copilot uses AI. Check for mistakes.


def depart_literal_block(self, node):
if self.solution and self.jupyter_drop_solutions:
Expand Down