Skip to content

Commit

Permalink
Merge pull request #1134 from pelson/fix/startup-args-handling
Browse files Browse the repository at this point in the history
Handle None being passed explicitly to startJVM
  • Loading branch information
marscher authored May 19, 2023
2 parents fffec42 + 507ede7 commit 8ed57b6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 33 deletions.
6 changes: 3 additions & 3 deletions jpype/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def interactive():
def startJVM(
*jvmargs: str,
jvmpath: typing.Optional[_PathOrStr] = None,
classpath: typing.Optional[_PathOrStr] = None,
classpath: typing.Optional[typing.Sequence[_PathOrStr], _PathOrStr] = None,
ignoreUnrecognized: bool = False,
convertStrings: bool = False,
interrupt: bool = not interactive(),
Expand Down Expand Up @@ -211,7 +211,7 @@ def startJVM(
# JVM path
if jvmargs:
# jvm is the first argument the first argument is a path or None
if jvmargs[0] is not None and isinstance(jvmargs[0], str) and not jvmargs[0].startswith('-'):
if jvmargs[0] is None or (isinstance(jvmargs[0], str) and not jvmargs[0].startswith('-')):
if jvmpath:
raise TypeError('jvmpath specified twice')
jvmpath = jvmargs[0]
Expand All @@ -223,7 +223,7 @@ def startJVM(
# Allow the path to be a PathLike.
jvmpath = os.fspath(jvmpath)

extra_jvm_args = tuple()
extra_jvm_args: typing.Tuple[str, ...] = tuple()

# Classpath handling
if _hasClassPath(jvmargs):
Expand Down
2 changes: 0 additions & 2 deletions jpype/_core.pyi

This file was deleted.

77 changes: 49 additions & 28 deletions test/jpypetest/test_startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,12 @@
#
# *****************************************************************************
import jpype
import common
import subrun
import functools
import os
from pathlib import Path
import sys
import unittest


@functools.wraps(jpype.startJVM)
def runStartJVMTest(*args, **kwargs):
jpype.startJVM(*args, **kwargs)
try:
assert jpype.JClass('jpype.array.TestArray') is not None
except Exception as err:
raise RuntimeError("Test class not found") from err


root = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
cp = os.path.join(root, 'classes').replace('\\', '/')

Expand Down Expand Up @@ -64,64 +52,97 @@ def testInvalidArgsFalse(self):
def testInvalidArgsTrue(self):
jpype.startJVM(
"-for_sure_InVaLiD",
ignoreUnrecognized=True, convertStrings=False,
ignoreUnrecognized=True,
convertStrings=False,
)

def testClasspathArgKeyword(self):
runStartJVMTest(classpath=cp, convertStrings=False)
jpype.startJVM(classpath=cp, convertStrings=False)
assert jpype.JClass('jpype.array.TestArray') is not None

def testClasspathArgList(self):
runStartJVMTest(classpath=[cp], convertStrings=False)
jpype.startJVM(
classpath=[cp],
convertStrings=False,
)
assert jpype.JClass('jpype.array.TestArray') is not None

def testClasspathArgListEmpty(self):
runStartJVMTest(classpath=[cp, ''], convertStrings=False)
jpype.startJVM(
classpath=[cp, ''],
convertStrings=False,
)
assert jpype.JClass('jpype.array.TestArray') is not None

def testClasspathArgDef(self):
runStartJVMTest('-Djava.class.path=%s' % cp, convertStrings=False)
jpype.startJVM('-Djava.class.path=%s' % cp, convertStrings=False)
assert jpype.JClass('jpype.array.TestArray') is not None

def testClasspathArgPath(self):
runStartJVMTest(classpath=Path(cp), convertStrings=False)
jpype.startJVM(classpath=Path(cp), convertStrings=False)
assert jpype.JClass('jpype.array.TestArray') is not None

def testClasspathArgPathList(self):
runStartJVMTest(classpath=[Path(cp)], convertStrings=False)
jpype.startJVM(classpath=[Path(cp)], convertStrings=False)
assert jpype.JClass('jpype.array.TestArray') is not None

def testClasspathArgGlob(self):
jpype.startJVM(classpath=os.path.join(cp, '..', 'jar', 'mrjar*'))
assert jpype.JClass('org.jpype.mrjar.A') is not None

def testClasspathTwice(self):
with self.assertRaises(TypeError):
runStartJVMTest('-Djava.class.path=%s' %
jpype.startJVM('-Djava.class.path=%s' %
cp, classpath=cp, convertStrings=False)

def testClasspathBadType(self):
with self.assertRaises(TypeError):
runStartJVMTest(classpath=1, convertStrings=False)
jpype.startJVM(classpath=1, convertStrings=False)

def testJVMPathArg_Str(self):
runStartJVMTest(self.jvmpath, classpath=cp, convertStrings=False)
jpype.startJVM(self.jvmpath, classpath=cp, convertStrings=False)
assert jpype.JClass('jpype.array.TestArray') is not None

def testJVMPathArg_None(self):
# It is allowed to pass None as a JVM path
jpype.startJVM(
None, # type: ignore
classpath=cp,
)
assert jpype.JClass('jpype.array.TestArray') is not None

def testJVMPathArg_NoArgs(self):
jpype.startJVM(
classpath=cp,
)
assert jpype.JClass('jpype.array.TestArray') is not None

def testJVMPathArg_Path(self):
with self.assertRaises(TypeError):
runStartJVMTest([
jpype.startJVM(
# Pass a path as the first argument. This isn't supported (this is
# reflected in the type definition), but the fact that it "works"
# gives rise to this test.
Path(self.jvmpath), cp], # type: ignore
Path(self.jvmpath), # type: ignore
convertStrings=False,
)

def testJVMPathKeyword_str(self):
runStartJVMTest(classpath=cp, jvmpath=self.jvmpath,
convertStrings=False)
jpype.startJVM(
classpath=cp,
jvmpath=self.jvmpath,
convertStrings=False,
)
assert jpype.JClass('jpype.array.TestArray') is not None

def testJVMPathKeyword_Path(self):
runStartJVMTest(jvmpath=Path(self.jvmpath), classpath=cp, convertStrings=False)
jpype.startJVM(jvmpath=Path(self.jvmpath), classpath=cp, convertStrings=False)
assert jpype.JClass('jpype.array.TestArray') is not None

def testPathTwice(self):
with self.assertRaises(TypeError):
jpype.startJVM(self.jvmpath, jvmpath=self.jvmpath)

def testBadKeyword(self):
with self.assertRaises(TypeError):
jpype.startJVM(invalid=True)
jpype.startJVM(invalid=True) # type: ignore

0 comments on commit 8ed57b6

Please sign in to comment.