Skip to content

Fix crash when isinstance() is called with JException before JVM starts#1436

Open
Thrameos wants to merge 3 commits into
jpype-project:masterfrom
Thrameos:fix-1329-isinstance-jexception-crash
Open

Fix crash when isinstance() is called with JException before JVM starts#1436
Thrameos wants to merge 3 commits into
jpype-project:masterfrom
Thrameos:fix-1329-isinstance-jexception-crash

Conversation

@Thrameos

@Thrameos Thrameos commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

Fixes issue #1329 where calling isinstance(obj, JException) would crash with an unhandled C++ exception when the JVM had not been started or had been shut down.

The problem occurred in PyJPClass_instancecheck() which created a JPJavaFrame without first checking if the JVM was running. This caused an unhandled JPypeException to be thrown from C++ code when users tried to wrap exceptions from failed startJVM() calls.

The fix adds a JVM running check before creating the frame, following the same pattern already used in PyJPClass_subclasscheck(). When the JVM is not running, the function now falls back to Python-only type checking.

Added comprehensive test coverage for:

  • isinstance() before JVM starts
  • isinstance() with non-Java objects after JVM starts
  • isinstance() with actual Java exceptions

Fixes #1329

Fixes issue jpype-project#1329 where calling isinstance(obj, JException) would crash
with an unhandled C++ exception when the JVM had not been started or had
been shut down.

The problem occurred in PyJPClass_instancecheck() which created a
JPJavaFrame without first checking if the JVM was running. This caused
an unhandled JPypeException to be thrown from C++ code when users tried
to wrap exceptions from failed startJVM() calls.

The fix adds a JVM running check before creating the frame, following
the same pattern already used in PyJPClass_subclasscheck(). When the
JVM is not running, the function now falls back to Python-only type
checking.

Added comprehensive test coverage for:
- isinstance() before JVM starts
- isinstance() with non-Java objects after JVM starts
- isinstance() with actual Java exceptions

Co-Authored-By: Claude Sonnet 4 <noreply@anthropic.com>
@codecov

codecov Bot commented Jul 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.22%. Comparing base (3e408b0) to head (66c00c2).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1436      +/-   ##
==========================================
- Coverage   86.24%   86.22%   -0.02%     
==========================================
  Files         112      112              
  Lines       10289    10289              
  Branches     4056     4056              
==========================================
- Hits         8874     8872       -2     
- Misses        809      811       +2     
  Partials      606      606              
Files with missing lines Coverage Δ
native/python/pyjp_class.cpp 85.82% <ø> (ø)

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Thrameos Thrameos requested a review from marscher July 11, 2026 14:15
@Thrameos

Copy link
Copy Markdown
Contributor Author

@marscher simple change to prevent a raise condition that happened during exception handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unhandled exception on isinstance(..., JException)

2 participants