├── .gitignore ├── CHANGES.md ├── LICENSE.txt ├── MANIFEST.in ├── README.md ├── appveyor.yml ├── ci ├── appveyor │ └── dump-dlls.py └── travis │ ├── before_script.sh │ ├── run.sh │ └── upload-to-ftp.sh ├── doc ├── .gitignore ├── Makefile ├── _static │ ├── figures │ │ ├── deployment.png │ │ └── deployment.puml │ └── java-apidocs │ │ ├── allclasses-frame.html │ │ ├── allclasses-noframe.html │ │ ├── constant-values.html │ │ ├── deprecated-list.html │ │ ├── help-doc.html │ │ ├── index-all.html │ │ ├── index.html │ │ ├── org │ │ └── jpy │ │ │ ├── DL.html │ │ │ ├── KeyError.html │ │ │ ├── PyDictWrapper.html │ │ │ ├── PyInputMode.html │ │ │ ├── PyLib.CallableKind.html │ │ │ ├── PyLib.Diag.html │ │ │ ├── PyLib.html │ │ │ ├── PyModule.html │ │ │ ├── PyObject.html │ │ │ ├── StopIteration.html │ │ │ ├── class-use │ │ │ ├── DL.html │ │ │ ├── KeyError.html │ │ │ ├── PyDictWrapper.html │ │ │ ├── PyInputMode.html │ │ │ ├── PyLib.CallableKind.html │ │ │ ├── PyLib.Diag.html │ │ │ ├── PyLib.html │ │ │ ├── PyModule.html │ │ │ ├── PyObject.html │ │ │ └── StopIteration.html │ │ │ ├── package-frame.html │ │ │ ├── package-summary.html │ │ │ ├── package-tree.html │ │ │ └── package-use.html │ │ ├── overview-tree.html │ │ ├── package-list │ │ ├── script.js │ │ ├── serialized-form.html │ │ └── stylesheet.css ├── conf.py ├── index.rst ├── install.rst ├── intro.rst ├── make.bat ├── modify.rst ├── reference.rst └── tutorial.rst ├── ez_setup.py ├── jpyutil.py ├── pom.xml ├── pysobug ├── .gitignore ├── README.txt ├── make.sh ├── mypy.c ├── mypydl.c ├── mypymod.c └── setup.py ├── setup.cfg ├── setup.py ├── src ├── main │ ├── c │ │ ├── jni │ │ │ ├── org_jpy_DL.c │ │ │ ├── org_jpy_DL.h │ │ │ ├── org_jpy_PyLib.c │ │ │ ├── org_jpy_PyLib.h │ │ │ ├── org_jpy_PyLib_CallableKind.h │ │ │ └── org_jpy_PyLib_Diag.h │ │ ├── jpy_compat.c │ │ ├── jpy_compat.h │ │ ├── jpy_conv.c │ │ ├── jpy_conv.h │ │ ├── jpy_diag.c │ │ ├── jpy_diag.h │ │ ├── jpy_jarray.c │ │ ├── jpy_jarray.h │ │ ├── jpy_jfield.c │ │ ├── jpy_jfield.h │ │ ├── jpy_jmethod.c │ │ ├── jpy_jmethod.h │ │ ├── jpy_jobj.c │ │ ├── jpy_jobj.h │ │ ├── jpy_jtype.c │ │ ├── jpy_jtype.h │ │ ├── jpy_module.c │ │ ├── jpy_module.h │ │ ├── jpy_verboseexcept.c │ │ └── jpy_verboseexcept.h │ └── java │ │ └── org │ │ └── jpy │ │ ├── DL.java │ │ ├── KeyError.java │ │ ├── PyDictWrapper.java │ │ ├── PyInputMode.java │ │ ├── PyLib.java │ │ ├── PyLibConfig.java │ │ ├── PyListWrapper.java │ │ ├── PyModule.java │ │ ├── PyObject.java │ │ ├── PyProxyHandler.java │ │ ├── StopIteration.java │ │ ├── annotations │ │ ├── Mutable.java │ │ ├── Output.java │ │ └── Return.java │ │ ├── jsr223 │ │ ├── ScriptEngineFactoryImpl.java │ │ └── ScriptEngineImpl.java │ │ └── package-info.java └── test │ ├── java │ └── org │ │ └── jpy │ │ ├── JavaReflectionTest.java │ │ ├── LifeCycleTest.java │ │ ├── PyLibTest.java │ │ ├── PyLibWithSysPathTest.java │ │ ├── PyModuleTest.java │ │ ├── PyObjectTest.java │ │ ├── UseCases.java │ │ ├── fixtures │ │ ├── ConstructorOverloadTestFixture.java │ │ ├── CovariantOverloadExtendTestFixture.java │ │ ├── CovariantOverloadTestFixture.java │ │ ├── DefaultInterfaceImplTestFixture.java │ │ ├── DefaultInterfaceTestFixture.java │ │ ├── ExceptionTestFixture.java │ │ ├── FieldTestFixture.java │ │ ├── MethodOverloadTestFixture.java │ │ ├── MethodReturnValueTestFixture.java │ │ ├── ModifyAndReturnParametersTestFixture.java │ │ ├── Processor.java │ │ ├── Thing.java │ │ ├── TypeConversionTestFixture.java │ │ ├── TypeResolutionTestFixture.java │ │ ├── TypeTranslationTestFixture.java │ │ └── VarArgsTestFixture.java │ │ └── jsr223 │ │ └── Jsr223Test.java │ ├── python │ ├── fixtures │ │ ├── hasheqstr.py │ │ ├── proc_class.py │ │ ├── proc_module.py │ │ └── raise_errors.py │ ├── imp │ │ ├── import_ex1.py │ │ ├── import_ex2.py │ │ ├── packages-jre6.txt │ │ └── packages-jre7.txt │ ├── jpy_array_test.py │ ├── jpy_diag_test.py │ ├── jpy_exception_test.py │ ├── jpy_field_test.py │ ├── jpy_gettype_test.py │ ├── jpy_modretparam_test.py │ ├── jpy_mt_test.py │ ├── jpy_overload_test.py │ ├── jpy_perf_test.py │ ├── jpy_retval_test.py │ ├── jpy_rt_test.py │ ├── jpy_translation_test.py │ ├── jpy_typeconv_test.py │ └── jpy_typeres_test.py │ └── resources │ ├── META-INF │ └── services │ │ └── javax.script.ScriptEngineFactory │ └── pymodules │ └── mod_1.py └── winbuild.cmd /.gitignore: -------------------------------------------------------------------------------- 1 | venv 2 | target 3 | build 4 | dist 5 | MANIFEST 6 | .idea 7 | *.iml 8 | *.jar 9 | *.war 10 | *.ear 11 | __pycache__/ 12 | hs_err_pid* 13 | winbuild.log 14 | local.dat 15 | setup.out 16 | *.pyc 17 | jpy.egg-info 18 | lib/ 19 | .vagrant 20 | Vagrantfile 21 | *.so 22 | *.dll 23 | 24 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include *.txt 2 | include *.xml 3 | include doc/make.bat 4 | include doc/Makefile 5 | include lib/*.jar 6 | recursive-include doc *.py 7 | recursive-include doc *.rst 8 | recursive-include src/main/c *.h 9 | recursive-include src/main/c *.c 10 | recursive-include src/main/java *.java 11 | recursive-include src/test/java *.java 12 | recursive-include src/test/python *.py 13 | 14 | -------------------------------------------------------------------------------- /ci/appveyor/dump-dlls.py: -------------------------------------------------------------------------------- 1 | import psutil, os 2 | 3 | p = psutil.Process(os.getpid()) 4 | for dll in p.memory_maps(): 5 | print(dll.path) 6 | 7 | -------------------------------------------------------------------------------- /ci/travis/before_script.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | if [[ $TRAVIS_OS_NAME == 'osx' ]]; then 4 | 5 | # Install some custom requirements on OS X 6 | # e.g. brew install pyenv-virtualenv 7 | # See https://gist.github.com/Bouke/11261620 8 | # and https://github.com/bincrafters/conan-bazel_installer 9 | 10 | brew update || brew update 11 | brew outdated pyenv || brew upgrade pyenv 12 | brew install pyenv-virtualenv 13 | eval "$(pyenv init -)" 14 | eval "$(pyenv virtualenv-init -)" 15 | 16 | pyenv install --list 17 | pyenv install --skip-existing $PYTHON_VERSION 18 | pyenv virtualenv $PYTHON_VERSION jpy-venv 19 | 20 | else 21 | # Install pyenv 22 | # See https://github.com/pyenv/pyenv 23 | git clone https://github.com/pyenv/pyenv.git $HOME/.pyenv 24 | export PYENV_ROOT="$HOME/.pyenv" 25 | export PATH="$PYENV_ROOT/bin:$PATH" 26 | eval "$(pyenv init -)" 27 | 28 | # Install pyenv virtualenv plugin 29 | # See https://github.com/pyenv/pyenv-virtualenv 30 | git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv 31 | eval "$(pyenv virtualenv-init -)" 32 | 33 | # Create virtualenv from current Python 34 | pyenv virtualenv jpy-venv 35 | fi 36 | 37 | pyenv rehash 38 | pyenv activate jpy-venv 39 | pip install wheel 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /ci/travis/run.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | 4 | if [[ $TRAVIS_OS_NAME != 'osx' ]]; then 5 | export PYENV_ROOT="$HOME/.pyenv" 6 | export PATH="$PYENV_ROOT/bin:$PATH" 7 | fi 8 | 9 | eval "$(pyenv init -)" 10 | eval "$(pyenv virtualenv-init -)" 11 | pyenv activate jpy-venv 12 | python --version 13 | 14 | # oracle-java8-set-default seems to modify PATH but not JAVA_HOME :( 15 | java -version 16 | if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then 17 | export JAVA_HOME=$(/usr/libexec/java_home -v 1.8); 18 | else 19 | export JAVA_HOME=/usr/lib/jvm/java-8-oracle 20 | fi 21 | echo $JAVA_HOME 22 | 23 | python setup.py --maven bdist_wheel 24 | -------------------------------------------------------------------------------- /ci/travis/upload-to-ftp.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | function upload_ftp { 4 | echo Uploading ${1}: ${2} 5 | curl --ftp-create-dirs -T $2 -u "$FTP_USER:$FTP_PASSWORD" "ftp://$FTP_HOST/software/$TRAVIS_OS_NAME/" 6 | } 7 | 8 | echo "success pushing artifacts to FTP..." 9 | upload_ftp "wheel" "dist/*.whl" 10 | -------------------------------------------------------------------------------- /doc/.gitignore: -------------------------------------------------------------------------------- 1 | _build 2 | _templates 3 | -------------------------------------------------------------------------------- /doc/_static/figures/deployment.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bcdev/jpy/76548cb6f3b9ba795ed6b3a42327607234eaba3c/doc/_static/figures/deployment.png -------------------------------------------------------------------------------- /doc/_static/figures/deployment.puml: -------------------------------------------------------------------------------- 1 | @startuml 2 | 3 | component JVM 4 | component Python 5 | 6 | artifact jdl.so 7 | artifact jpy.so 8 | artifact jpy.jar 9 | artifact jpyutil.py 10 | file jpyconfig.properties 11 | file jpyconfig.py 12 | 13 | jpy.jar ..> jpy.so 14 | jpy.jar ..> jdl.so 15 | jpy.jar ..> jpyconfig.properties 16 | 17 | jpyutil.py ..> jpyconfig.py 18 | 19 | Python ..> jpy.so 20 | Python ..> jpyutil.py 21 | JVM ..> jpy.jar 22 | 23 | @enduml -------------------------------------------------------------------------------- /doc/_static/java-apidocs/allclasses-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 | 7 |Method and Description | 88 |
---|
org.jpy.PyLib.execScript(String) | 92 |
Copyright © 2014–2020 Brockmann Consult GmbH. All rights reserved.
145 | 146 | 147 | -------------------------------------------------------------------------------- /doc/_static/java-apidocs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |Copyright © 2014–2020 Brockmann Consult GmbH. All rights reserved.
123 | 124 | 125 | -------------------------------------------------------------------------------- /doc/_static/java-apidocs/org/jpy/class-use/KeyError.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |Copyright © 2014–2020 Brockmann Consult GmbH. All rights reserved.
123 | 124 | 125 | -------------------------------------------------------------------------------- /doc/_static/java-apidocs/org/jpy/class-use/PyLib.Diag.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |Copyright © 2014–2020 Brockmann Consult GmbH. All rights reserved.
123 | 124 | 125 | -------------------------------------------------------------------------------- /doc/_static/java-apidocs/org/jpy/class-use/PyLib.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |Copyright © 2014–2020 Brockmann Consult GmbH. All rights reserved.
123 | 124 | 125 | -------------------------------------------------------------------------------- /doc/_static/java-apidocs/org/jpy/class-use/StopIteration.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |Copyright © 2014–2020 Brockmann Consult GmbH. All rights reserved.
123 | 124 | 125 | -------------------------------------------------------------------------------- /doc/_static/java-apidocs/org/jpy/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 |Copyright © 2014–2020 Brockmann Consult GmbH. All rights reserved.
141 | 142 | 143 | -------------------------------------------------------------------------------- /doc/index.rst: -------------------------------------------------------------------------------- 1 | .. jpy documentation master file, created by 2 | sphinx-quickstart on Mon Jan 20 21:26:19 2014. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | Welcome to jpy's documentation! 7 | =============================== 8 | 9 | jpy is a *bi-directional* Java-Python bridge allowing you to call Java from Python and Python from Java. 10 | 11 | Contents: 12 | 13 | .. toctree:: 14 | :maxdepth: 2 15 | 16 | intro 17 | install 18 | tutorial 19 | reference 20 | modify 21 | 22 | 23 | Indices and tables 24 | ================== 25 | 26 | * :ref:`genindex` 27 | * :ref:`modindex` 28 | * :ref:`search` 29 | 30 | -------------------------------------------------------------------------------- /doc/modify.rst: -------------------------------------------------------------------------------- 1 | ############# 2 | How to Modify 3 | ############# 4 | 5 | =============== 6 | Rebuild Process 7 | =============== 8 | 9 | jpy's source distribution directory layout uses the `Maven common directory structure22 | * Important note: This class is useful on POSIX (Unix/Linux) systems only. On Windows OSes, all methods 23 | * are no-ops. 24 | * 25 | * @author Norman Fomferra 26 | * @see dlopen(3) - Linux manual page 27 | * @since 0.7 28 | */ 29 | public class DL { 30 | /** 31 | * Resolve undefined symbols as code from the dynamic library is executed. 32 | */ 33 | public static final int RTLD_LAZY = 0x0001; 34 | /** 35 | * Resolve all undefined symbols before {@link #dlopen} returns and fail if this cannot be done. 36 | */ 37 | public static final int RTLD_NOW = 0x0002; 38 | /** 39 | * This is the converse of RTLD_GLOBAL, and the default if neither flag is specified. 40 | */ 41 | public static final int RTLD_LOCAL = 0x0004; 42 | /** 43 | * External symbols defined in the library will be made available to subsequently loaded libraries. 44 | */ 45 | public static final int RTLD_GLOBAL = 0x0008; 46 | 47 | /** 48 | * loads the dynamic library file named by the null-terminated string filename and returns 49 | * an opaque "handle" for the dynamic library. If filename is {@code null}, then the returned handle 50 | * is for the main program. If filename contains a slash ("/"), then it is interpreted as a 51 | * (relative or absolute) pathname. 52 | * 53 | * @param filename dynamic library filename or {@code null} 54 | * @param flag combination of {@link #RTLD_GLOBAL} or {@link #RTLD_LOCAL} with {@link #RTLD_LAZY}, 55 | * {@link #RTLD_NOW}. 56 | * @return opaque "handle" for the dynamic library. 57 | */ 58 | public static native long dlopen(String filename, int flag); 59 | 60 | public static native int dlclose(long handle); 61 | 62 | public static native String dlerror(); 63 | 64 | static { 65 | try { 66 | System.loadLibrary("jdl"); 67 | } catch (Throwable t) { 68 | String jdlLibPath = System.getProperty("jpy.jdlLib"); 69 | if (jdlLibPath != null) { 70 | System.load(jdlLibPath); 71 | } else { 72 | throw new RuntimeException("Failed to load 'jdl' shared library. You can use system property 'jpy.jdlLib' to specify it.", t); 73 | } 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/main/java/org/jpy/KeyError.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Brockmann Consult GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * This file was modified by Illumon. 17 | * 18 | */ 19 | package org.jpy; 20 | 21 | /** 22 | * Translation of Python KeyErrors so that they can be programmatically detected from Java. 23 | */ 24 | public class KeyError extends RuntimeException { 25 | KeyError(String message) { 26 | super(message); 27 | } 28 | } -------------------------------------------------------------------------------- /src/main/java/org/jpy/PyInputMode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2015 Brockmann Consult GmbH 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package org.jpy; 18 | 19 | import java.util.Map; 20 | 21 | /** 22 | * Source code input modes for compiling/executing Python source code. 23 | * 24 | * @author Norman Fomferra 25 | * @since 0.8 26 | * @see PyObject#executeCode(String, PyInputMode) 27 | * @see PyObject#executeCode(String, PyInputMode, Map, Map) 28 | */ 29 | public enum PyInputMode { 30 | /** 31 | * Compile/execute single statement code. 32 | *
33 | * The start symbol from the Python grammar for a 34 | * single statement ({@code Py_single_input} in Python PyRun API). 35 | * This is the symbol used for the interactive interpreter loop. 36 | */ 37 | STATEMENT(256), 38 | /** 39 | * Compile/execute multi-statement script code. 40 | *
41 | * The start symbol from the Python grammar for sequences of 42 | * statements as read from a file or other source ({@code Py_file_input} in Python PyRun API). 43 | * This is the symbol to use when compiling arbitrarily long Python source code. 44 | */ 45 | SCRIPT(257), 46 | /** 47 | * Compile/execute single expression. 48 | *
49 | * The start symbol from the Python grammar for sequences of
50 | * statements as read from a file or other source ({@code Py_eval_input} in Python PyRun API).
51 | */
52 | EXPRESSION(258);
53 |
54 | public int value() {
55 | return value;
56 | }
57 |
58 | private final int value;
59 |
60 | PyInputMode(int value) {
61 | this.value = value;
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/src/main/java/org/jpy/PyLibConfig.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jpy;
18 |
19 | import java.io.File;
20 | import java.io.FileReader;
21 | import java.io.IOException;
22 | import java.io.InputStream;
23 | import java.io.InputStreamReader;
24 | import java.io.Reader;
25 | import java.util.Properties;
26 | import java.util.Set;
27 |
28 | /**
29 | * Provides configuration for {@link org.jpy.PyLib}.
30 | *
31 | * @author Norman Fomferra
32 | * @since 0.7
33 | */
34 | class PyLibConfig {
35 |
36 | private static final boolean DEBUG = Boolean.getBoolean("jpy.debug");
37 | public static final String PYTHON_LIB_KEY = "jpy.pythonLib";
38 | public static final String JPY_LIB_KEY = "jpy.jpyLib";
39 | public static final String JPY_CONFIG_KEY = "jpy.config";
40 | public static final String JPY_CONFIG_RESOURCE = "jpyconfig.properties";
41 |
42 | public enum OS {
43 | WINDOWS,
44 | UNIX,
45 | MAC_OS,
46 | SUNOS,
47 | }
48 |
49 | private static final Properties properties = new Properties();
50 |
51 |
52 | static {
53 | if (DEBUG) System.out.println("org.jpy.PyLibConfig: entered static initializer");
54 | InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(JPY_CONFIG_RESOURCE);
55 | if (stream != null) {
56 | loadConfig(stream, JPY_CONFIG_RESOURCE);
57 | }
58 | String path = System.getProperty(JPY_CONFIG_KEY);
59 | if (path != null) {
60 | File file = new File(path);
61 | if (file.isFile()) {
62 | loadConfig(file);
63 | }
64 | }
65 | File file = new File(JPY_CONFIG_RESOURCE).getAbsoluteFile();
66 | if (file.isFile()) {
67 | loadConfig(file);
68 | }
69 | if (DEBUG) System.out.println("org.jpy.PyLibConfig: exited static initializer");
70 | }
71 |
72 | private static void loadConfig(InputStream stream, String name) {
73 | try {
74 | if (DEBUG)
75 | System.out.printf(String.format("org.jpy.PyLibConfig: loading configuration resource %s\n", name));
76 | try (Reader reader = new InputStreamReader(stream)) {
77 | loadConfig(reader);
78 | }
79 | } catch (IOException e) {
80 | if (DEBUG) e.printStackTrace(System.err);
81 | }
82 | }
83 |
84 | private static void loadConfig(File file) {
85 | try {
86 | if (DEBUG)
87 | System.out.printf(String.format("%s: loading configuration file %s\n", PyLibConfig.class.getName(), file));
88 | try (Reader reader = new FileReader(file)) {
89 | loadConfig(reader);
90 | }
91 | } catch (IOException e) {
92 | System.err.printf("org.jpy.PyLibConfig: %s: %s\n", file, e.getMessage());
93 | if (DEBUG) e.printStackTrace(System.err);
94 | }
95 | }
96 |
97 | private static void loadConfig(Reader reader) throws IOException {
98 | properties.load(reader);
99 | Set
27 | * Note: this class is not used yet.
28 | *
29 | * @author Norman Fomferra
30 | */
31 | @Target(value = ElementType.PARAMETER)
32 | @Retention(value = RetentionPolicy.RUNTIME)
33 | public @interface Mutable {
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/org/jpy/annotations/Output.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jpy.annotations;
18 |
19 | import java.lang.annotation.ElementType;
20 | import java.lang.annotation.Retention;
21 | import java.lang.annotation.RetentionPolicy;
22 | import java.lang.annotation.Target;
23 |
24 | /**
25 | * Used to mark method parameters as mere output, that is, an argument is expected to be written to by the method but not read from.
26 | *
27 | * Note: this class is not used yet.
28 | *
29 | * @author Norman Fomferra
30 | */
31 | @Target(value = ElementType.PARAMETER)
32 | @Retention(value = RetentionPolicy.RUNTIME)
33 | public @interface Output {
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/org/jpy/annotations/Return.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jpy.annotations;
18 |
19 | import java.lang.annotation.ElementType;
20 | import java.lang.annotation.Retention;
21 | import java.lang.annotation.RetentionPolicy;
22 | import java.lang.annotation.Target;
23 |
24 | /**
25 | * Used to mark method parameters as return values, that is, an argument may be returned as-is by the method.
26 | *
27 | * Note: this class is not used yet.
28 | *
29 | * @author Norman Fomferra
30 | */
31 | @Target(value = ElementType.PARAMETER)
32 | @Retention(value = RetentionPolicy.RUNTIME)
33 | public @interface Return {
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/org/jpy/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | /**
18 | * Provides the classes necessary to
19 | *
28 | * The entry point to the jpy Java API is the {@link org.jpy.PyLib} class which is used to either
29 | * detect an already running Python interpreter or to start a new one:
30 | *
31 | *
40 | * Once the Python interpreter in running clients can either execute Python code directly using the {@link org.jpy.PyLib#execScript(String)} method or
41 | * load a Python module using {@link org.jpy.PyModule#importModule(String)}. The returned {@link org.jpy.PyModule} object
42 | * then is the entry point to access Python variables and invoke functions.
43 | *
44 | * Some {@link org.jpy.PyModule} methods return {@link org.jpy.PyObject} instances. These can be used to
45 | * create instances of Python classes, and to access Python class members, attributes and to invoke Python object methods..
46 | *
47 | * @since 0.7
48 | */
49 | package org.jpy;
--------------------------------------------------------------------------------
/src/test/java/org/jpy/LifeCycleTest.java:
--------------------------------------------------------------------------------
1 | package org.jpy;
2 |
3 | import org.junit.Assert;
4 | import org.junit.Test;
5 |
6 | public class LifeCycleTest {
7 | private static final boolean ON_WINDOWS = System.getProperty("os.name").toLowerCase().contains("windows");
8 |
9 | @Test
10 | public void testCanStartAndStopWithoutException() {
11 | PyLib.startPython();
12 | Assert.assertTrue(PyLib.isPythonRunning());
13 | PyModule sys1 = PyModule.importModule("sys");
14 | Assert.assertNotNull(sys1);
15 | final long sys1Pointer = sys1.getPointer();
16 |
17 | PyLib.stopPython();
18 | if (!ON_WINDOWS) {
19 | Assert.assertFalse(PyLib.isPythonRunning());
20 | }
21 |
22 | PyLib.startPython();
23 | Assert.assertTrue(PyLib.isPythonRunning());
24 |
25 | PyModule sys2 = PyModule.importModule("sys");
26 | Assert.assertNotNull(sys2);
27 | final long sys2Pointer = sys2.getPointer();
28 |
29 | if (!ON_WINDOWS) {
30 | Assert.assertNotEquals(sys1Pointer, sys2Pointer);
31 | }
32 |
33 | PyLib.stopPython();
34 | if (!ON_WINDOWS) {
35 | Assert.assertFalse(PyLib.isPythonRunning());
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/test/java/org/jpy/PyLibWithSysPathTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jpy;
18 |
19 | import org.junit.*;
20 |
21 | import java.io.File;
22 | import java.net.URI;
23 | import java.security.CodeSource;
24 |
25 | import static org.junit.Assert.*;
26 |
27 |
28 | public class PyLibWithSysPathTest {
29 |
30 | @Before
31 | public void setUp() throws Exception {
32 |
33 | CodeSource codeSource = PyLibWithSysPathTest.class.getProtectionDomain().getCodeSource();
34 | if (codeSource == null) {
35 | System.out.println(PyLibWithSysPathTest.class + " not run: no code source found");
36 | return;
37 | }
38 | URI codeSourceLocation = codeSource.getLocation().toURI();
39 | System.out.println(PyLibWithSysPathTest.class + ": code source: " + codeSourceLocation);
40 | File codeSourceDir = new File(codeSourceLocation);
41 | if (!codeSourceDir.isDirectory()) {
42 | System.out.println(PyLibWithSysPathTest.class + " not run: code source is not a directory: " + codeSourceLocation);
43 | return;
44 | }
45 |
46 | File pymodulesDir = new File(codeSourceDir, "pymodules");
47 | //assertFalse(PyLib.isPythonRunning());
48 | System.out.println("PyLibWithSysPathTest: starting Python with 'sys.path' extension: " + pymodulesDir);
49 | PyLib.startPython(pymodulesDir.getPath());
50 | //PyLib.startPython("x");
51 | assertTrue(PyLib.isPythonRunning());
52 | }
53 |
54 | @After
55 | public void tearDown() throws Exception {
56 | PyLib.stopPython();
57 | }
58 |
59 | @Test
60 | public void testLoadModule() throws Exception {
61 | PyModule pyModule = PyModule.importModule("mod_1");
62 | assertNotNull(pyModule);
63 | PyObject pyAnswer = pyModule.getAttribute("answer");
64 | assertNotNull(pyAnswer);
65 | assertEquals(42, pyAnswer.getIntValue());
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/test/java/org/jpy/PyModuleTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jpy;
18 |
19 | import org.junit.*;
20 |
21 | import java.io.File;
22 |
23 | import static org.junit.Assert.assertEquals;
24 | import static org.junit.Assert.assertNotNull;
25 | import static org.junit.Assert.assertTrue;
26 |
27 | /**
28 | * @author Norman Fomferra
29 | */
30 | public class PyModuleTest {
31 |
32 | @Before
33 | public void setUp() throws Exception {
34 | //System.out.println("PyModuleTest: Current thread: " + Thread.currentThread());
35 |
36 | String importPath = new File("src/test/python/fixtures").getCanonicalPath();
37 | PyLib.startPython(importPath);
38 | assertEquals(true, PyLib.isPythonRunning());
39 |
40 | //PyLib.Diag.setFlags(PyLib.Diag.F_METH);
41 | }
42 |
43 | @After
44 | public void tearDown() throws Exception {
45 | PyLib.Diag.setFlags(PyLib.Diag.F_OFF);
46 | PyLib.stopPython();
47 | }
48 |
49 | @Test
50 | public void testCreateAndCallProxySingleThreaded() throws Exception {
51 | //PyObjectTest.addTestDirToPythonSysPath();
52 | PyModule procModule = PyModule.importModule("proc_module");
53 | PyObjectTest.testCallProxySingleThreaded(procModule);
54 | }
55 |
56 | // see https://github.com/bcdev/jpy/issues/26
57 | @Test
58 | public void testCreateAndCallProxyMultiThreaded() throws Exception {
59 | //PyObjectTest.addTestDirToPythonSysPath();
60 | PyModule procModule = PyModule.importModule("proc_module");
61 | PyObjectTest.testCallProxyMultiThreaded(procModule);
62 | }
63 |
64 | // see: https://github.com/bcdev/jpy/issues/39: Improve Java exception messages on Python errors #39
65 | @Test
66 | public void testPythonErrorMessages() throws Exception {
67 | //PyObjectTest.addTestDirToPythonSysPath();
68 | PyModule raiserModule = PyModule.importModule("raise_errors");
69 | for (int i=0;i < 10;i++) {
70 | try {
71 | raiserModule.call("raise_if_zero", 0);
72 | Assert.fail();
73 | } catch (RuntimeException e) {
74 | //e.printStackTrace();
75 | String message = e.getMessage();
76 | //System.out.println("message = " + message);
77 | assertNotNull(message);
78 | assertTrue(message.startsWith("Error in Python interpreter"));
79 | assertTrue(message.contains("Type: <"));
80 | assertTrue(message.contains("IndexError'>\n"));
81 | assertTrue(message.contains("Value: arg wasn't there\n"));
82 | assertTrue(message.contains("Line: 3\n"));
83 | assertTrue(message.contains("Namespace: raise_if_zero\n"));
84 | assertTrue(message.contains("File: "));
85 | }
86 | // ok
87 | raiserModule.call("raise_if_zero", 1);
88 | }
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/src/test/java/org/jpy/UseCases.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jpy;
18 |
19 | import org.junit.After;
20 | import org.junit.Before;
21 | import org.junit.Assert;
22 | import org.junit.Test;
23 |
24 | import java.util.Locale;
25 |
26 | import static org.junit.Assert.assertEquals;
27 | import static org.junit.Assert.assertTrue;
28 |
29 | /**
30 | * Some (more complex) tests that represent possible API use cases.
31 | *
32 | * @author Norman Fomferra
33 | */
34 | public class UseCases {
35 |
36 | @Before
37 | public void setUp() {
38 | PyLib.startPython();
39 | }
40 |
41 | @After
42 | public void tearDown() {
43 | PyLib.stopPython();
44 | }
45 |
46 | @Test
47 | public void modifyPythonSysPath() {
48 |
49 | PyModule builtinsMod = PyModule.getBuiltins();
50 |
51 | PyModule sysMod = PyModule.importModule("sys");
52 | PyObject pathObj = sysMod.getAttribute("path");
53 |
54 | PyObject lenObj1 = builtinsMod.call("len", pathObj);
55 | pathObj.call("append", "/usr/home/norman/");
56 | PyObject lenObj2 = builtinsMod.call("len", pathObj);
57 |
58 | int lenVal1 = lenObj1.getIntValue();
59 | int lenVal2 = lenObj2.getIntValue();
60 | String[] pathEntries = pathObj.getObjectArrayValue(String.class);
61 |
62 | /////////////////////////////////////////////////
63 |
64 | assertEquals(lenVal1 + 1, lenVal2);
65 | assertEquals(pathEntries.length, lenVal2);
66 | //for (int i = 0; i < pathEntries.length; i++) {
67 | // System.out.printf("pathEntries[%d] = %s%n", i, pathEntries[i]);
68 | //}
69 |
70 | /////////////////////////////////////////////////
71 | }
72 |
73 | @Test
74 | public void setAndGetGlobalPythonVariables() throws Exception {
75 |
76 | PyLib.startPython();
77 | PyLib.execScript("paramInt = 123");
78 | PyLib.execScript("paramStr = 'abc'");
79 | PyModule mainModule = PyModule.getMain();
80 | PyObject paramIntObj = mainModule.getAttribute("paramInt");
81 | PyObject paramStrObj = mainModule.getAttribute("paramStr");
82 | int paramIntValue = paramIntObj.getIntValue();
83 | String paramStrValue = paramStrObj.getStringValue();
84 |
85 | /////////////////////////////////////////////////
86 |
87 | assertEquals(123, paramIntValue);
88 | assertEquals("abc", paramStrValue);
89 |
90 | /////////////////////////////////////////////////
91 | }
92 |
93 | @Test
94 | public void defAndUseGlobalPythonFunction() throws Exception {
95 |
96 | PyLib.startPython();
97 | PyLib.execScript("def incByOne(x): return x + 1");
98 | PyModule mainModule = PyModule.getMain();
99 | PyObject eleven = mainModule.call("incByOne", 10);
100 |
101 | /////////////////////////////////////////////////
102 |
103 | assertEquals(11, eleven.getIntValue());
104 |
105 | /////////////////////////////////////////////////
106 | // Performance test for TheMegaTB:
107 |
108 | long t0 = System.nanoTime();
109 | long numCalls = 100000;
110 | PyObject num = eleven;
111 | for (long i = 0; i < numCalls; i++) {
112 | num = mainModule.call("incByOne", num);
113 | }
114 | long t1 = System.nanoTime();
115 |
116 | assertEquals(11 + numCalls, num.getIntValue());
117 |
118 | double millis = (t1 - t0) / 1000. / 1000.;
119 | double callsPerMilli = numCalls / millis;
120 | double millisPerCall = millis / numCalls;
121 |
122 | System.out.printf("Performance: %10.1f Python-calls/ms, %2.10f ms/Python-call%n", callsPerMilli, millisPerCall);
123 | assertTrue(callsPerMilli > 1.0);
124 |
125 | /////////////////////////////////////////////////
126 | }
127 |
128 | static {
129 | Locale.setDefault(Locale.ENGLISH);
130 | }
131 | }
132 |
--------------------------------------------------------------------------------
/src/test/java/org/jpy/fixtures/ConstructorOverloadTestFixture.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jpy.fixtures;
18 |
19 | /**
20 | * @author Norman Fomferra
21 | */
22 | @SuppressWarnings("UnusedDeclaration")
23 | public class ConstructorOverloadTestFixture {
24 | String state;
25 |
26 | public ConstructorOverloadTestFixture() {
27 | initState();
28 | }
29 |
30 | public ConstructorOverloadTestFixture(int a) {
31 | initState(a);
32 | }
33 |
34 | public ConstructorOverloadTestFixture(int a, int b) {
35 | initState(a, b);
36 | }
37 |
38 | public ConstructorOverloadTestFixture(float a) {
39 | initState(a);
40 | }
41 |
42 | public ConstructorOverloadTestFixture(float a, float b) {
43 | initState(a, b);
44 | }
45 |
46 | public ConstructorOverloadTestFixture(int a, float b) {
47 | initState(a, b);
48 | }
49 |
50 | public ConstructorOverloadTestFixture(float a, int b) {
51 | initState(a, b);
52 | }
53 |
54 | public String getState() {
55 | return state;
56 | }
57 |
58 | private void initState(Object... args) {
59 | state = MethodOverloadTestFixture.stringifyArgs(args);
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/src/test/java/org/jpy/fixtures/CovariantOverloadExtendTestFixture.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | * This file was modified by Illumon.
17 | */
18 |
19 | package org.jpy.fixtures;
20 |
21 | import java.lang.reflect.Array;
22 | import java.lang.reflect.Method;
23 |
24 | /**
25 | * Used as a test class for the test cases in jpy_overload_test.py
26 | *
27 | * @author Charles P. Wright
28 | */
29 | @SuppressWarnings("UnusedDeclaration")
30 | public class CovariantOverloadExtendTestFixture extends CovariantOverloadTestFixture {
31 | public CovariantOverloadExtendTestFixture(int x) {
32 | super(x * 2);
33 | }
34 |
35 | public CovariantOverloadExtendTestFixture foo(Number a, int b) {
36 | return new CovariantOverloadExtendTestFixture(a.intValue() - b);
37 | }
38 | }
--------------------------------------------------------------------------------
/src/test/java/org/jpy/fixtures/CovariantOverloadTestFixture.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | * This file was modified by Illumon.
17 | */
18 |
19 | package org.jpy.fixtures;
20 |
21 | import java.lang.reflect.Array;
22 | import java.lang.reflect.Method;
23 |
24 | /**
25 | * Used as a test class for the test cases in jpy_overload_test.py
26 | *
27 | * @author Charles P. Wright
28 | */
29 | @SuppressWarnings("UnusedDeclaration")
30 | public class CovariantOverloadTestFixture {
31 | int x;
32 |
33 | public CovariantOverloadTestFixture(int x) {
34 | this.x = x;
35 | }
36 |
37 | public CovariantOverloadTestFixture foo(Number a, int b) {
38 | return new CovariantOverloadTestFixture(a.intValue() + b);
39 | }
40 |
41 | public int getX() {
42 | return x;
43 | }
44 | }
--------------------------------------------------------------------------------
/src/test/java/org/jpy/fixtures/DefaultInterfaceImplTestFixture.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jpy.fixtures;
18 |
19 | import static org.jpy.fixtures.MethodOverloadTestFixture.stringifyArgs;
20 |
21 | /**
22 | * Used as a test class for the test cases in jpy_overload_test.py
23 | *
24 | * @author Charles Wright
25 | */
26 | @SuppressWarnings("UnusedDeclaration")
27 | public class DefaultInterfaceImplTestFixture implements DefaultInterfaceTestFixture {
28 | public int doIt() {
29 | return 2;
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/src/test/java/org/jpy/fixtures/DefaultInterfaceTestFixture.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jpy.fixtures;
18 |
19 | import static org.jpy.fixtures.MethodOverloadTestFixture.stringifyArgs;
20 |
21 | /**
22 | * Used as a test class for the test cases in jpy_overload_test.py
23 | *
24 | * @author Charles Wright
25 | */
26 | @SuppressWarnings("UnusedDeclaration")
27 | public interface DefaultInterfaceTestFixture {
28 | int doIt();
29 |
30 | default public int doItPlusOne() {
31 | return 1 + doIt();
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/test/java/org/jpy/fixtures/ExceptionTestFixture.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | * This file was modified by Illumon.
17 | *
18 | */
19 |
20 | package org.jpy.fixtures;
21 |
22 | import java.io.IOException;
23 |
24 | /**
25 | * Used as a test class for the test cases in jpy_exception_test.py
26 | *
27 | * @author Norman Fomferra
28 | */
29 | @SuppressWarnings("UnusedDeclaration")
30 | public class ExceptionTestFixture {
31 | public int throwNpeIfArgIsNull(String arg) {
32 | return arg.length();
33 | }
34 |
35 | public int throwNpeIfArgIsNull2(String arg) {
36 | return throwNpeIfArgIsNull(arg);
37 | }
38 |
39 | public int throwNpeIfArgIsNullNested(String arg) {
40 | try {
41 | return throwNpeIfArgIsNull(arg);
42 | } catch (Exception e) {
43 | throw new RuntimeException("Nested exception", e);
44 | }
45 | }
46 |
47 | public int throwNpeIfArgIsNullNested2(String arg) {
48 | return throwNpeIfArgIsNullNested(arg);
49 | }
50 |
51 | public int throwNpeIfArgIsNullNested3(String arg) {
52 | try {
53 | return throwNpeIfArgIsNullNested2(arg);
54 | } catch (Exception e) {
55 | throw new RuntimeException("Nested exception 3", e);
56 | }
57 | }
58 |
59 | public int throwAioobeIfIndexIsNotZero(int index) {
60 | int[] ints = new int[]{101};
61 | return ints[index];
62 | }
63 |
64 |
65 | public void throwRteIfMessageIsNotNull(String message) {
66 | if (message != null) {
67 | throw new RuntimeException(message);
68 | }
69 | }
70 |
71 | public void throwIoeIfMessageIsNotNull(String message) throws IOException {
72 | if (message != null) {
73 | throw new IOException(message);
74 | }
75 | }
76 |
77 | }
78 |
--------------------------------------------------------------------------------
/src/test/java/org/jpy/fixtures/FieldTestFixture.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jpy.fixtures;
18 |
19 | /**
20 | * Used as a test class for the test cases in jpy_field_test.py
21 | *
22 | * @author Norman Fomferra
23 | */
24 | @SuppressWarnings("UnusedDeclaration")
25 | public class FieldTestFixture {
26 |
27 | public static final boolean z_STATIC_FIELD = true;
28 | public static final char c_STATIC_FIELD = 'A';
29 | public static final byte b_STATIC_FIELD = (byte) 123;
30 | public static final short s_STATIC_FIELD = (short) 12345;
31 | public static final int i_STATIC_FIELD = 123456789;
32 | public static final long j_STATIC_FIELD = 1234567890123456789L;
33 | public static final float f_STATIC_FIELD = 0.12345F;
34 | public static final double d_STATIC_FIELD = 0.123456789;
35 |
36 | public static final String S_OBJ_STATIC_FIELD = "ABC";
37 | public static final Thing l_OBJ_STATIC_FIELD = new Thing(123);
38 |
39 | public boolean zInstField;
40 | public char cInstField;
41 | public byte bInstField;
42 | public short sInstField;
43 | public int iInstField;
44 | public long jInstField;
45 | public float fInstField;
46 | public double dInstField;
47 |
48 | public Boolean zObjInstField;
49 | public Character cObjInstField;
50 | public Byte bObjInstField;
51 | public Short sObjInstField;
52 | public Integer iObjInstField;
53 | public Long jObjInstField;
54 | public Float fObjInstField;
55 | public Double dObjInstField;
56 |
57 | public String SObjInstField;
58 | public Thing lObjInstField;
59 | }
60 |
--------------------------------------------------------------------------------
/src/test/java/org/jpy/fixtures/MethodOverloadTestFixture.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jpy.fixtures;
18 |
19 | import java.lang.reflect.Array;
20 |
21 | /**
22 | * Used as a test class for the test cases in jpy_overload_test.py
23 | *
24 | * @author Norman Fomferra
25 | */
26 | @SuppressWarnings("UnusedDeclaration")
27 | public class MethodOverloadTestFixture {
28 |
29 | public String join(int a, int b) {
30 | return stringifyArgs(a, b);
31 | }
32 |
33 | public String join(int a, double b) {
34 | return stringifyArgs(a, b);
35 | }
36 |
37 | public String join(int a, String b) {
38 | return stringifyArgs(a, b);
39 | }
40 |
41 | public String join(double a, int b) {
42 | return stringifyArgs(a, b);
43 | }
44 |
45 | public String join(double a, double b) {
46 | return stringifyArgs(a, b);
47 | }
48 |
49 | public String join(double a, String b) {
50 | return stringifyArgs(a, b);
51 | }
52 |
53 | public String join(String a, int b) {
54 | return stringifyArgs(a, b);
55 | }
56 |
57 | public String join(String a, double b) {
58 | return stringifyArgs(a, b);
59 | }
60 |
61 | public String join(String a, String b) {
62 | return stringifyArgs(a, b);
63 | }
64 |
65 | //////////////////////////////////////////////
66 |
67 | public String join(String a) {
68 | return stringifyArgs(a);
69 | }
70 |
71 | public String join(String a, String b, String c) {
72 | return stringifyArgs(a, b, c);
73 | }
74 |
75 | //////////////////////////////////////////////
76 | public String join2(Comparable a, int b, String c, String d) {
77 | return stringifyArgs(a, b, c, d);
78 | }
79 |
80 | //////////////////////////////////////////////
81 | public String join3(Number a, int b) {
82 | return stringifyArgs(a, b);
83 | }
84 |
85 | /**
86 | * Used to test that we also find overloaded methods in class hierarchies
87 | */
88 | public static class MethodOverloadTestFixture2 extends MethodOverloadTestFixture {
89 |
90 | public String join(String a, String b, String c, String d) {
91 | return stringifyArgs(a, b, c, d);
92 | }
93 | }
94 |
95 | //////////////////////////////////////////////
96 |
97 | // Should never been found, since 'float' is not present in Python
98 | public String join(int a, float b) {
99 | return stringifyArgs(a, b);
100 | }
101 |
102 | static String stringifyArgs(Object... args) {
103 | StringBuilder argString = new StringBuilder();
104 | for (int i = 0; i < args.length; i++) {
105 | if (i > 0) {
106 | argString.append(",");
107 | }
108 | Object arg = args[i];
109 | if (arg != null) {
110 | Class> argClass = arg.getClass();
111 | argString.append(argClass.getSimpleName());
112 | argString.append('(');
113 | if (argClass.isArray()) {
114 | stringifyArray(arg, argString);
115 | } else {
116 | stringifyObject(arg, argString);
117 | }
118 | argString.append(')');
119 | } else {
120 | argString.append("null");
121 | }
122 | }
123 | return argString.toString();
124 | }
125 |
126 | private static void stringifyObject(Object arg, StringBuilder argString) {
127 | argString.append(String.valueOf(arg));
128 | }
129 |
130 | private static void stringifyArray(Object arg, StringBuilder argString) {
131 | boolean primitive = arg.getClass().getComponentType().isPrimitive();
132 | int length = Array.getLength(arg);
133 | for (int i = 0; i < length; i++) {
134 | Object item = Array.get(arg, i);
135 | if (i > 0) {
136 | argString.append(",");
137 | }
138 | if (primitive) {
139 | argString.append(String.valueOf(item));
140 | } else {
141 | argString.append(stringifyArgs(item));
142 | }
143 | }
144 | }
145 |
146 |
147 | }
148 |
--------------------------------------------------------------------------------
/src/test/java/org/jpy/fixtures/MethodReturnValueTestFixture.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jpy.fixtures;
18 |
19 | /**
20 | * Used as a test class for the test cases in jpy_retval_test.py
21 | * Note: Please make sure to not add any method overloads to this class.
22 | * This is done in {@link MethodOverloadTestFixture}.
23 | *
24 | * @author Norman Fomferra
25 | */
26 | @SuppressWarnings("UnusedDeclaration")
27 | public class MethodReturnValueTestFixture {
28 |
29 | public void getVoid() {
30 | }
31 |
32 | public boolean getValue_boolean(boolean value) {
33 | return value;
34 | }
35 |
36 | public byte getValue_byte(byte value) {
37 | return value;
38 | }
39 |
40 | public short getValue_short(short value) {
41 | return value;
42 | }
43 |
44 | public int getValue_int(int value) {
45 | return value;
46 | }
47 |
48 | public long getValue_long(long value) {
49 | return value;
50 | }
51 |
52 | public float getValue_float(float value) {
53 | return value;
54 | }
55 |
56 | public double getValue_double(double value) {
57 | return value;
58 | }
59 |
60 | public String getString(String string) {
61 | return string;
62 | }
63 |
64 | public Thing getObject(Thing object) {
65 | return object;
66 | }
67 |
68 | ///////////////////////////////////////////////////////////////////////////////////
69 | // 1D-Array Return Values
70 |
71 | public boolean[] getArray1D_boolean(boolean item0, boolean item1, boolean item2) {
72 | return new boolean[]{item0, item1, item2};
73 | }
74 |
75 | public byte[] getArray1D_byte(byte item0, byte item1, byte item2) {
76 | return new byte[]{item0, item1, item2};
77 | }
78 |
79 | public short[] getArray1D_short(short item0, short item1, short item2) {
80 | return new short[]{item0, item1, item2};
81 | }
82 |
83 | public int[] getArray1D_int(int item0, int item1, int item2) {
84 | return new int[]{item0, item1, item2};
85 | }
86 |
87 | public long[] getArray1D_long(long item0, long item1, long item2) {
88 | return new long[]{item0, item1, item2};
89 | }
90 |
91 | public float[] getArray1D_float(float item0, float item1, float item2) {
92 | return new float[]{item0, item1, item2};
93 | }
94 |
95 | public double[] getArray1D_double(double item0, double item1, double item2) {
96 | return new double[]{item0, item1, item2};
97 | }
98 |
99 | public String[] getArray1D_String(String item0, String item1, String item2) {
100 | return new String[]{item0, item1, item2};
101 | }
102 |
103 | public Thing[] getArray1D_Object(Thing item0, Thing item1, Thing item2) {
104 | return new Thing[]{item0, item1, item2};
105 | }
106 |
107 | // add other variants
108 |
109 | ///////////////////////////////////////////////////////////////////////////////////
110 | // 2D-Array Return Values
111 |
112 | public boolean[][] getArray2D_boolean(boolean item00, boolean item01, boolean item10, boolean item11) {
113 | return new boolean[][]{{item00, item01}, {item10, item11}};
114 | }
115 |
116 | public byte[][] getArray2D_byte(byte item00, byte item01, byte item10, byte item11) {
117 | return new byte[][]{{item00, item01}, {item10, item11}};
118 | }
119 |
120 | public int[][] getArray2D_byte(int item00, int item01, int item10, int item11) {
121 | return new int[][]{{item00, item01}, {item10, item11}};
122 | }
123 |
124 | // add other variants
125 | }
126 |
--------------------------------------------------------------------------------
/src/test/java/org/jpy/fixtures/ModifyAndReturnParametersTestFixture.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jpy.fixtures;
18 |
19 | import org.jpy.annotations.Mutable;
20 | import org.jpy.annotations.Output;
21 | import org.jpy.annotations.Return;
22 |
23 | /**
24 | * Used as a test class for the test cases in jpy_modretparam_test.py
25 | *
26 | * @author Norman Fomferra
27 | */
28 | @SuppressWarnings("UnusedDeclaration")
29 | public class ModifyAndReturnParametersTestFixture {
30 |
31 | public void modifyThing(@Mutable Thing thing, int value) {
32 | thing.setValue(value);
33 | }
34 |
35 | public Thing returnThing(@Return Thing thing) {
36 | if (thing == null) {
37 | thing = new Thing();
38 | }
39 | return thing;
40 | }
41 |
42 | public Thing modifyAndReturnThing(@Mutable @Return Thing thing, int value) {
43 | if (thing == null) {
44 | thing = new Thing();
45 | }
46 | thing.setValue(value);
47 | return thing;
48 | }
49 |
50 | public void modifyIntArray(@Mutable int[] array, int item0, int item1, int item2) {
51 | array[0] = item0;
52 | array[1] = item1;
53 | array[2] = item2;
54 | }
55 |
56 | public int[] returnIntArray(@Return int[] array) {
57 | if (array == null) {
58 | array = new int[3];
59 | }
60 | return array;
61 | }
62 |
63 | public int[] modifyAndReturnIntArray(@Mutable @Return int[] array, int item0, int item1, int item2) {
64 | if (array == null) {
65 | array = new int[3];
66 | }
67 | array[0] = item0;
68 | array[1] = item1;
69 | array[2] = item2;
70 | return array;
71 | }
72 |
73 | public void modifyAndOutputIntArray(@Mutable @Output int[] array, int item0, int item1, int item2) {
74 | if (array == null) {
75 | return;
76 | }
77 | array[0] = item0;
78 | array[1] = item1;
79 | array[2] = item2;
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/src/test/java/org/jpy/fixtures/Processor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jpy.fixtures;
18 |
19 | /**
20 | * Stands for an image data processor.
21 | *
22 | * Created by Norman on 19.12.13.
23 | */
24 | public interface Processor {
25 | String initialize();
26 |
27 | String computeTile(int w, int h, float[] data);
28 |
29 | String dispose();
30 |
31 | void setVal(int n);
32 |
33 | int getVal();
34 |
35 | boolean check1234();
36 | }
37 |
--------------------------------------------------------------------------------
/src/test/java/org/jpy/fixtures/Thing.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jpy.fixtures;
18 |
19 | /**
20 | * Stands for any Java object.
21 | *
22 | * @author Norman Fomferra
23 | */
24 | @SuppressWarnings("UnusedDeclaration")
25 | public class Thing {
26 | private int value;
27 |
28 | public Thing() {
29 | }
30 |
31 | public Thing(int value) {
32 | this.value = value;
33 | }
34 |
35 | public int getValue() {
36 | return value;
37 | }
38 |
39 | public void setValue(int value) {
40 | this.value = value;
41 | }
42 |
43 | @Override
44 | public boolean equals(Object o) {
45 | if (this == o) return true;
46 | if (o == null || getClass() != o.getClass()) return false;
47 | Thing thing = (Thing) o;
48 | return value == thing.value;
49 | }
50 |
51 | @Override
52 | public int hashCode() {
53 | return value;
54 | }
55 |
56 | @Override
57 | public String toString() {
58 | return "Thing[value=" + value + "]";
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/src/test/java/org/jpy/fixtures/TypeConversionTestFixture.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jpy.fixtures;
18 |
19 | import static org.jpy.fixtures.MethodOverloadTestFixture.stringifyArgs;
20 |
21 | /**
22 | * Used as a test class for the test cases in jpy_typeconv_test.py
23 | *
24 | * @author Norman Fomferra
25 | */
26 | @SuppressWarnings("UnusedDeclaration")
27 | public class TypeConversionTestFixture {
28 |
29 | public String stringifyObjectArg(Object arg) {
30 | return stringifyArgs(arg);
31 | }
32 |
33 | public String stringifyIntArrayArg(int[] arg) {
34 | return stringifyArgs((Object) arg);
35 | }
36 |
37 | public String stringifyObjectArrayArg(Object[] arg) {
38 | return stringifyArgs((Object) arg);
39 | }
40 |
41 | public String stringifyStringArrayArg(String[] arg) {
42 | return stringifyArgs((Object) arg);
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/test/java/org/jpy/fixtures/TypeResolutionTestFixture.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jpy.fixtures;
18 |
19 | /**
20 | * Used as a test class for the test cases in jpy_typeres_test.py
21 | *
22 | * @author Norman Fomferra
23 | */
24 | @SuppressWarnings("UnusedDeclaration")
25 | public class TypeResolutionTestFixture {
26 |
27 | public SuperThing createSuperThing(int value) {
28 | return new SuperThing(value);
29 | }
30 |
31 |
32 | public static class SuperThing extends Thing {
33 | public SuperThing(int value) {
34 | super(value);
35 | }
36 |
37 | public void add(int val) {
38 | setValue(getValue() + val);
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/test/java/org/jpy/fixtures/TypeTranslationTestFixture.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | * This file was modified by Illumon.
17 | *
18 | */
19 |
20 | package org.jpy.fixtures;
21 |
22 | /**
23 | * Used as a test class for the test cases in jpy_retval_test.py
24 | * Note: Please make sure to not add any method overloads to this class.
25 | * This is done in {@link MethodOverloadTestFixture}.
26 | *
27 | * @author Norman Fomferra
28 | */
29 | @SuppressWarnings("UnusedDeclaration")
30 | public class TypeTranslationTestFixture {
31 | public Thing makeThing(int value) {
32 | return new Thing(value);
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/src/test/java/org/jpy/fixtures/VarArgsTestFixture.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | *
16 | * This file was modified by Illumon.
17 | *
18 | */
19 |
20 | package org.jpy.fixtures;
21 |
22 | import java.lang.reflect.Array;
23 |
24 | /**
25 | * Used as a test class for the test cases in jpy_overload_test.py
26 | *
27 | * @author Norman Fomferra
28 | */
29 | @SuppressWarnings("UnusedDeclaration")
30 | public class VarArgsTestFixture {
31 |
32 | public String join(String prefix, int ... a) {
33 | return stringifyArgs(prefix, a);
34 | }
35 |
36 | public String join(String prefix, double ... a) {
37 | return stringifyArgs(prefix, a);
38 | }
39 | public String join(String prefix, float ... a) {
40 | return stringifyArgs(prefix, a);
41 | }
42 | public String join(String prefix, String ... a) {
43 | return stringifyArgs(prefix, a);
44 | }
45 |
46 | public String joinFloat(String prefix, float ... a) {
47 | return stringifyArgs(prefix, a);
48 | }
49 |
50 | public String joinLong(String prefix, long ... a) {
51 | return stringifyArgs(prefix, a);
52 | }
53 | public String joinShort(String prefix, short ... a) {
54 | return stringifyArgs(prefix, a);
55 | }
56 | public String joinByte(String prefix, byte ... a) {
57 | return stringifyArgs(prefix, a);
58 | }
59 | public String joinChar(String prefix, char ... a) {
60 | return stringifyArgs(prefix, a);
61 | }
62 | public String joinBoolean(String prefix, boolean ... a) {
63 | return stringifyArgs(prefix, a);
64 | }
65 | public String joinObjects(String prefix, Object ... a) {
66 | return stringifyArgs(prefix, a);
67 | }
68 |
69 | public int chooseFixedArity(int... a) {
70 | return 2;
71 | }
72 |
73 | public int chooseFixedArity() {
74 | return 1;
75 | }
76 |
77 | public int stringOrObjectVarArgs(String ... a) {
78 | return 1 + a.length;
79 | }
80 | public int stringOrObjectVarArgs(Object ... a) {
81 | return 2 + a.length;
82 | }
83 |
84 | static String stringifyArgs(Object... args) {
85 | StringBuilder argString = new StringBuilder();
86 | for (int i = 0; i < args.length; i++) {
87 | if (i > 0) {
88 | argString.append(",");
89 | }
90 | Object arg = args[i];
91 | if (arg != null) {
92 | Class> argClass = arg.getClass();
93 | argString.append(argClass.getSimpleName());
94 | argString.append('(');
95 | if (argClass.isArray()) {
96 | stringifyArray(arg, argString);
97 | } else {
98 | stringifyObject(arg, argString);
99 | }
100 | argString.append(')');
101 | } else {
102 | argString.append("null");
103 | }
104 | }
105 | return argString.toString();
106 | }
107 |
108 | private static void stringifyObject(Object arg, StringBuilder argString) {
109 | argString.append(String.valueOf(arg));
110 | }
111 |
112 | private static void stringifyArray(Object arg, StringBuilder argString) {
113 | boolean primitive = arg.getClass().getComponentType().isPrimitive();
114 | int length = Array.getLength(arg);
115 | for (int i = 0; i < length; i++) {
116 | Object item = Array.get(arg, i);
117 | if (i > 0) {
118 | argString.append(",");
119 | }
120 | if (primitive) {
121 | argString.append(String.valueOf(item));
122 | } else {
123 | argString.append(stringifyArgs(item));
124 | }
125 | }
126 | }
127 |
128 |
129 | }
130 |
--------------------------------------------------------------------------------
/src/test/java/org/jpy/jsr223/Jsr223Test.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 Brockmann Consult GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.jpy.jsr223;
18 |
19 | import org.junit.Assert;
20 | import org.junit.Test;
21 |
22 | import javax.script.ScriptEngine;
23 | import javax.script.ScriptEngineFactory;
24 | import javax.script.ScriptEngineManager;
25 | import java.util.List;
26 |
27 | import static org.junit.Assert.assertEquals;
28 | import static org.junit.Assert.assertNotNull;
29 |
30 | public class Jsr223Test {
31 |
32 | @Test
33 | public void testThatScriptEngineFactoryIsRegistered() throws Exception {
34 | assertNotNull(getScriptEngineFactory());
35 | }
36 |
37 | @Test
38 | public void testThatScriptEngineFactorySupportsMinimumParameterKeys() throws Exception {
39 | ScriptEngineFactoryImpl scriptEngineFactory = getScriptEngineFactory();
40 | assertEquals("cpython", scriptEngineFactory.getParameter(ScriptEngine.NAME));
41 | assertEquals("jpy Python Engine", scriptEngineFactory.getParameter(ScriptEngine.ENGINE));
42 | assertEquals("0.1-alpha", scriptEngineFactory.getParameter(ScriptEngine.ENGINE_VERSION));
43 | assertEquals("python", scriptEngineFactory.getParameter(ScriptEngine.LANGUAGE));
44 | assertEquals("3.x", scriptEngineFactory.getParameter(ScriptEngine.LANGUAGE_VERSION));
45 | }
46 |
47 | private ScriptEngineFactoryImpl getScriptEngineFactory() {
48 | ScriptEngineManager engineManager = new ScriptEngineManager();
49 | List
51 | * PyLib.execScript("def incByOne(x): return x + 1");
52 | * PyModule mainModule = PyModule.getMain();
53 | * PyObject eleven = mainModule.call("incByOne", 10);
54 | *
55 | *
56 | * @return The Python main module's Java representation.
57 | * @since 0.8
58 | */
59 | public static PyModule getMain() {
60 | return importModule("__main__");
61 | }
62 |
63 | /**
64 | * Get the Python interpreter's buildins module and returns its Java representation.
65 | * It can be used to call functions such as {@code len()}, {@code type()}, {@code list()}, etc. For example:
66 | *
67 | * builtins = PyModule.getBuiltins();
68 | * PyObject size = builtins.call("len", pyList);
69 | *
70 | *
71 | * @return Java representation of Python's builtin module.
72 | * @see org.jpy.PyObject#call(String, Object...)
73 | * @since 0.8
74 | */
75 | public static PyModule getBuiltins() {
76 | try {
77 | //Python 3.3+
78 | return importModule("builtins");
79 | } catch (Exception e) {
80 | //Python 2.7
81 | return importModule("__builtin__");
82 | }
83 | }
84 |
85 | /**
86 | * Import a Python module into the Python interpreter and return its Java representation.
87 | *
88 | * @param name The Python module's name.
89 | * @return The Python module's Java representation.
90 | */
91 | public static PyModule importModule(String name) {
92 | assertPythonRuns();
93 | Objects.requireNonNull(name, "name must not be null");
94 | long pointer = PyLib.importModule(name);
95 | return pointer != 0 ? new PyModule(name, pointer) : null;
96 | }
97 |
98 | /**
99 | * Extends Python's 'sys.path' variable by the given module path.
100 | *
101 | * @param modulePath The new module path. Should be an absolute pathname.
102 | * @param prepend If true, the new path will be the new first element of 'sys.path', otherwise it will be the last.
103 | * @return The altered 'sys.path' list.
104 | * @since 0.8
105 | */
106 | public static PyObject extendSysPath(String modulePath, boolean prepend) {
107 | Objects.requireNonNull(modulePath, "path must not be null");
108 | PyModule sys = importModule("sys");
109 | PyObject sysPath = sys.getAttribute("path");
110 | if (prepend) {
111 | sysPath.call("insert", 0, modulePath);
112 | } else {
113 | sysPath.call("append", modulePath);
114 | }
115 | return sysPath;
116 | }
117 |
118 |
119 | /**
120 | * Create a Java proxy instance of this Python module which contains compatible functions to the ones provided in the
121 | * interface given by the {@code type} parameter.
122 | *
123 | * @param type The interface's type.
124 | * @param
20 | *
26 | *
27 | *
32 | * if (!PyLib.isPythonRunning()) {
33 | * PyLib.startPython(opt1, opt2, ...);
34 | * }
35 | *
36 | *
37 | * jpy API clients should first call {@link org.jpy.PyLib#isPythonRunning()} in order to check if a Python interpreter is already available.
38 | * If not, {@link org.jpy.PyLib#startPython(String...)} must be called before any other jpy API is used.
39 | *