Python4j change namespace (org.eclipse->org.nd4j) (#493)
* org.eclipse->org.nd4j * Fix parent groupid Signed-off-by: Alex Black <blacka101@gmail.com> * memview->bytes * del test Co-authored-by: Alex Black <blacka101@gmail.com>
This commit is contained in:
		
							parent
							
								
									b4a4a78f21
								
							
						
					
					
						commit
						e9c13ca9f4
					
				| @ -25,7 +25,7 @@ | ||||
|     </parent> | ||||
|     <modelVersion>4.0.0</modelVersion> | ||||
| 
 | ||||
|     <groupId>org.eclipse</groupId> | ||||
|     <groupId>org.nd4j</groupId> | ||||
|     <artifactId>python4j-parent</artifactId> | ||||
|     <packaging>pom</packaging> | ||||
|     <modules> | ||||
|  | ||||
| @ -21,7 +21,7 @@ | ||||
|          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||||
|     <parent> | ||||
|         <artifactId>python4j-parent</artifactId> | ||||
|         <groupId>org.eclipse</groupId> | ||||
|         <groupId>org.nd4j</groupId> | ||||
|         <version>1.0.0-SNAPSHOT</version> | ||||
|     </parent> | ||||
|     <packaging>jar</packaging> | ||||
|  | ||||
| @ -15,7 +15,7 @@ | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| 
 | ||||
| package org.eclipse.python4j; | ||||
| package org.nd4j.python4j; | ||||
| 
 | ||||
| import org.bytedeco.cpython.PyObject; | ||||
| 
 | ||||
| @ -14,7 +14,7 @@ | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| package org.eclipse.python4j; | ||||
| package org.nd4j.python4j; | ||||
| 
 | ||||
| import javax.lang.model.SourceVersion; | ||||
| 
 | ||||
| @ -103,7 +103,18 @@ public class PythonContextManager { | ||||
|     } | ||||
| 
 | ||||
|     private static boolean validateContextName(String s) { | ||||
|         return SourceVersion.isIdentifier(s) && !s.startsWith(COLLAPSED_KEY); | ||||
|         for (int i=0; i<s.length(); i++){ | ||||
|             char c = s.toLowerCase().charAt(i); | ||||
|             if (i == 0){ | ||||
|                 if (c >= '0' && c <= '9'){ | ||||
|                     return false; | ||||
|                 } | ||||
|             } | ||||
|             if (!(c=='_' || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))){ | ||||
|                 return false; | ||||
|             } | ||||
|         } | ||||
|         return true; | ||||
|     } | ||||
| 
 | ||||
|     private static String getContextPrefix(String contextName) { | ||||
| @ -14,7 +14,7 @@ | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| package org.eclipse.python4j; | ||||
| package org.nd4j.python4j; | ||||
| 
 | ||||
| 
 | ||||
| /** | ||||
| @ -15,7 +15,7 @@ | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| 
 | ||||
| package org.eclipse.python4j; | ||||
| package org.nd4j.python4j; | ||||
| 
 | ||||
| import org.bytedeco.cpython.PyObject; | ||||
| 
 | ||||
| @ -25,7 +25,6 @@ import java.io.InputStream; | ||||
| import java.nio.charset.StandardCharsets; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Arrays; | ||||
| import java.util.Collections; | ||||
| import java.util.List; | ||||
| import java.util.concurrent.atomic.AtomicBoolean; | ||||
| 
 | ||||
| @ -15,7 +15,7 @@ | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| 
 | ||||
| package org.eclipse.python4j; | ||||
| package org.nd4j.python4j; | ||||
| 
 | ||||
| import org.bytedeco.cpython.PyObject; | ||||
| import org.bytedeco.javacpp.Pointer; | ||||
| @ -14,11 +14,10 @@ | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| package org.eclipse.python4j; | ||||
| package org.nd4j.python4j; | ||||
| 
 | ||||
| 
 | ||||
| import org.bytedeco.cpython.PyThreadState; | ||||
| import org.omg.SendingContext.RunTime; | ||||
| 
 | ||||
| import java.util.concurrent.atomic.AtomicBoolean; | ||||
| 
 | ||||
| @ -14,12 +14,11 @@ | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| package org.eclipse.python4j; | ||||
| package org.nd4j.python4j; | ||||
| 
 | ||||
| 
 | ||||
| import lombok.Builder; | ||||
| import lombok.Data; | ||||
| import lombok.NoArgsConstructor; | ||||
| import lombok.extern.slf4j.Slf4j; | ||||
| 
 | ||||
| import javax.annotation.Nonnull; | ||||
| @ -14,7 +14,7 @@ | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| package org.eclipse.python4j; | ||||
| package org.nd4j.python4j; | ||||
| 
 | ||||
| 
 | ||||
| import org.bytedeco.cpython.PyObject; | ||||
| @ -15,7 +15,7 @@ | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| 
 | ||||
| package org.eclipse.python4j; | ||||
| package org.nd4j.python4j; | ||||
| 
 | ||||
| import org.apache.commons.io.IOUtils; | ||||
| import org.bytedeco.javacpp.Loader; | ||||
| @ -14,7 +14,7 @@ | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| package org.eclipse.python4j; | ||||
| package org.nd4j.python4j; | ||||
| 
 | ||||
| 
 | ||||
| import java.io.File; | ||||
| @ -14,14 +14,12 @@ | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| package org.eclipse.python4j; | ||||
| package org.nd4j.python4j; | ||||
| 
 | ||||
| 
 | ||||
| import org.bytedeco.cpython.PyObject; | ||||
| import org.bytedeco.javacpp.BytePointer; | ||||
| import org.bytedeco.javacpp.Loader; | ||||
| import org.bytedeco.javacpp.Pointer; | ||||
| import sun.misc.Unsafe; | ||||
| import sun.nio.ch.DirectBuffer; | ||||
| 
 | ||||
| import java.lang.reflect.Field; | ||||
| @ -37,7 +35,7 @@ public class PythonTypes { | ||||
| 
 | ||||
| 
 | ||||
|     private static List<PythonType> getPrimitiveTypes() { | ||||
|         return Arrays.<PythonType>asList(STR, INT, FLOAT, BOOL, MEMORYVIEW); | ||||
|         return Arrays.<PythonType>asList(STR, INT, FLOAT, BOOL, BYTES); | ||||
|     } | ||||
| 
 | ||||
|     private static List<PythonType> getCollectionTypes() { | ||||
| @ -258,6 +256,10 @@ public class PythonTypes { | ||||
|                     int[] arr = (int[]) javaObject; | ||||
|                     for (int x : arr) ret.add(x); | ||||
|                     return ret; | ||||
|                 }else if (javaObject instanceof byte[]){ | ||||
|                     byte[] arr = (byte[]) javaObject; | ||||
|                     for (int x : arr) ret.add(x); | ||||
|                     return ret; | ||||
|                 } else if (javaObject instanceof long[]) { | ||||
|                     long[] arr = (long[]) javaObject; | ||||
|                     for (long x : arr) ret.add(x); | ||||
| @ -410,83 +412,125 @@ public class PythonTypes { | ||||
|     }; | ||||
| 
 | ||||
| 
 | ||||
|     public static final PythonType<BytePointer> MEMORYVIEW = new PythonType<BytePointer>("memoryview", BytePointer.class) { | ||||
|     public static final PythonType<byte[]> BYTES = new PythonType<byte[]>("bytes", byte[].class) { | ||||
|         @Override | ||||
|         public BytePointer toJava(PythonObject pythonObject) { | ||||
|         public byte[] toJava(PythonObject pythonObject) { | ||||
|             try (PythonGC gc = PythonGC.watch()) { | ||||
|                 if (!(Python.isinstance(pythonObject, Python.memoryviewType()))) { | ||||
|                     throw new PythonException("Expected memoryview. Received: " + pythonObject); | ||||
|                 if (!(Python.isinstance(pythonObject, Python.bytesType()))) { | ||||
|                     throw new PythonException("Expected bytes. Received: " + pythonObject); | ||||
|                 } | ||||
|                 PythonObject pySize = Python.len(pythonObject); | ||||
|                 PythonObject ctypes = Python.importModule("ctypes"); | ||||
|                 PythonObject charType = ctypes.attr("c_char"); | ||||
|                 PythonObject charArrayType = new PythonObject(PyNumber_Multiply(charType.getNativePythonObject(), | ||||
|                         pySize.getNativePythonObject())); | ||||
|                 PythonObject fromBuffer = charArrayType.attr("from_buffer"); | ||||
|                 if (pythonObject.attr("readonly").toBoolean()) { | ||||
|                     pythonObject = Python.bytearray(pythonObject); | ||||
|                 byte[] ret = new byte[pySize.toInt()]; | ||||
|                 for (int i = 0; i < ret.length; i++) { | ||||
|                     ret[i] = (byte)pythonObject.get(i).toInt(); | ||||
|                 } | ||||
|                 PythonObject arr = fromBuffer.call(pythonObject); | ||||
|                 PythonObject cast = ctypes.attr("cast"); | ||||
|                 PythonObject voidPtrType = ctypes.attr("c_void_p"); | ||||
|                 PythonObject voidPtr = cast.call(arr, voidPtrType); | ||||
|                 long address = voidPtr.attr("value").toLong(); | ||||
|                 long size = pySize.toLong(); | ||||
|                 try { | ||||
|                     Field addressField = Buffer.class.getDeclaredField("address"); | ||||
|                     addressField.setAccessible(true); | ||||
|                     Field capacityField = Buffer.class.getDeclaredField("capacity"); | ||||
|                     capacityField.setAccessible(true); | ||||
|                     ByteBuffer buff = ByteBuffer.allocateDirect(0).order(ByteOrder.nativeOrder()); | ||||
|                     addressField.setLong(buff, address); | ||||
|                     capacityField.setInt(buff, (int) size); | ||||
|                     BytePointer ret = new BytePointer(buff); | ||||
|                     ret.limit(size); | ||||
|                     return ret; | ||||
| 
 | ||||
|                 } catch (Exception e) { | ||||
|                     throw new RuntimeException(e); | ||||
|                 } | ||||
| 
 | ||||
|                 return ret; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         @Override | ||||
|         public PythonObject toPython(BytePointer javaObject) { | ||||
|             long address = javaObject.address(); | ||||
|             long size = javaObject.limit(); | ||||
|             try (PythonGC gc = PythonGC.watch()) { | ||||
|                 PythonObject ctypes = Python.importModule("ctypes"); | ||||
|                 PythonObject charType = ctypes.attr("c_char"); | ||||
|                 PythonObject pySize = new PythonObject(size); | ||||
|                 PythonObject charArrayType = new PythonObject(PyNumber_Multiply(charType.getNativePythonObject(), | ||||
|                         pySize.getNativePythonObject())); | ||||
|                 PythonObject fromAddress = charArrayType.attr("from_address"); | ||||
|                 PythonObject arr = fromAddress.call(new PythonObject(address)); | ||||
|                 PythonObject memoryView = Python.memoryview(arr).attr("cast").call("b"); | ||||
|                 PythonGC.keep(memoryView); | ||||
|                 return memoryView; | ||||
|         public PythonObject toPython(byte[] javaObject) { | ||||
|             try(PythonGC gc = PythonGC.watch()){ | ||||
|                 PythonObject ret = Python.bytes(LIST.toPython(LIST.adapt(javaObject))); | ||||
|                 PythonGC.keep(ret); | ||||
|                 return ret; | ||||
|             } | ||||
| 
 | ||||
|         } | ||||
| 
 | ||||
|         @Override | ||||
|         public boolean accepts(Object javaObject) { | ||||
|             return javaObject instanceof Pointer || javaObject instanceof DirectBuffer; | ||||
|             return javaObject instanceof byte[]; | ||||
|         } | ||||
|         @Override | ||||
|         public byte[] adapt(Object javaObject) { | ||||
|             if (javaObject instanceof byte[]){ | ||||
|                 return (byte[])javaObject; | ||||
|             } | ||||
|             throw new PythonException("Cannot cast object of type " + javaObject.getClass().getName() + " to byte[]"); | ||||
|         } | ||||
| 
 | ||||
|         @Override | ||||
|         public BytePointer adapt(Object javaObject) { | ||||
|             if (javaObject instanceof BytePointer) { | ||||
|                 return (BytePointer) javaObject; | ||||
|             } else if (javaObject instanceof Pointer) { | ||||
|                 return new BytePointer((Pointer) javaObject); | ||||
|             } else if (javaObject instanceof DirectBuffer) { | ||||
|                 return new BytePointer((ByteBuffer) javaObject); | ||||
|             } else { | ||||
|                 throw new PythonException("Cannot cast object of type " + javaObject.getClass().getName() + " to BytePointer"); | ||||
|             } | ||||
|         } | ||||
|     }; | ||||
| 
 | ||||
|     /** | ||||
|      * Crashes on Adopt OpenJDK | ||||
|      * Use implementation in python4j-numpy instead for zero-copy byte buffers. | ||||
|      */ | ||||
| //    public static final PythonType<BytePointer> MEMORYVIEW = new PythonType<BytePointer>("memoryview", BytePointer.class) { | ||||
| //        @Override | ||||
| //        public BytePointer toJava(PythonObject pythonObject) { | ||||
| //            try (PythonGC gc = PythonGC.watch()) { | ||||
| //                if (!(Python.isinstance(pythonObject, Python.memoryviewType()))) { | ||||
| //                    throw new PythonException("Expected memoryview. Received: " + pythonObject); | ||||
| //                } | ||||
| //                PythonObject pySize = Python.len(pythonObject); | ||||
| //                PythonObject ctypes = Python.importModule("ctypes"); | ||||
| //                PythonObject charType = ctypes.attr("c_char"); | ||||
| //                PythonObject charArrayType = new PythonObject(PyNumber_Multiply(charType.getNativePythonObject(), | ||||
| //                        pySize.getNativePythonObject())); | ||||
| //                PythonObject fromBuffer = charArrayType.attr("from_buffer"); | ||||
| //                if (pythonObject.attr("readonly").toBoolean()) { | ||||
| //                    pythonObject = Python.bytearray(pythonObject); | ||||
| //                } | ||||
| //                PythonObject arr = fromBuffer.call(pythonObject); | ||||
| //                PythonObject cast = ctypes.attr("cast"); | ||||
| //                PythonObject voidPtrType = ctypes.attr("c_void_p"); | ||||
| //                PythonObject voidPtr = cast.call(arr, voidPtrType); | ||||
| //                long address = voidPtr.attr("value").toLong(); | ||||
| //                long size = pySize.toLong(); | ||||
| //                try { | ||||
| //                    Field addressField = Buffer.class.getDeclaredField("address"); | ||||
| //                    addressField.setAccessible(true); | ||||
| //                    Field capacityField = Buffer.class.getDeclaredField("capacity"); | ||||
| //                    capacityField.setAccessible(true); | ||||
| //                    ByteBuffer buff = ByteBuffer.allocateDirect(0).order(ByteOrder.nativeOrder()); | ||||
| //                    addressField.setLong(buff, address); | ||||
| //                    capacityField.setInt(buff, (int) size); | ||||
| //                    BytePointer ret = new BytePointer(buff); | ||||
| //                    ret.limit(size); | ||||
| //                    return ret; | ||||
| // | ||||
| //                } catch (Exception e) { | ||||
| //                    throw new RuntimeException(e); | ||||
| //                } | ||||
| // | ||||
| //            } | ||||
| //        } | ||||
| // | ||||
| //        @Override | ||||
| //        public PythonObject toPython(BytePointer javaObject) { | ||||
| //            long address = javaObject.address(); | ||||
| //            long size = javaObject.limit(); | ||||
| //            try (PythonGC gc = PythonGC.watch()) { | ||||
| //                PythonObject ctypes = Python.importModule("ctypes"); | ||||
| //                PythonObject charType = ctypes.attr("c_char"); | ||||
| //                PythonObject pySize = new PythonObject(size); | ||||
| //                PythonObject charArrayType = new PythonObject(PyNumber_Multiply(charType.getNativePythonObject(), | ||||
| //                        pySize.getNativePythonObject())); | ||||
| //                PythonObject fromAddress = charArrayType.attr("from_address"); | ||||
| //                PythonObject arr = fromAddress.call(new PythonObject(address)); | ||||
| //                PythonObject memoryView = Python.memoryview(arr).attr("cast").call("b"); | ||||
| //                PythonGC.keep(memoryView); | ||||
| //                return memoryView; | ||||
| //            } | ||||
| // | ||||
| //        } | ||||
| // | ||||
| //        @Override | ||||
| //        public boolean accepts(Object javaObject) { | ||||
| //            return javaObject instanceof Pointer || javaObject instanceof DirectBuffer; | ||||
| //        } | ||||
| // | ||||
| //        @Override | ||||
| //        public BytePointer adapt(Object javaObject) { | ||||
| //            if (javaObject instanceof BytePointer) { | ||||
| //                return (BytePointer) javaObject; | ||||
| //            } else if (javaObject instanceof Pointer) { | ||||
| //                return new BytePointer((Pointer) javaObject); | ||||
| //            } else if (javaObject instanceof DirectBuffer) { | ||||
| //                return new BytePointer((ByteBuffer) javaObject); | ||||
| //            } else { | ||||
| //                throw new PythonException("Cannot cast object of type " + javaObject.getClass().getName() + " to BytePointer"); | ||||
| //            } | ||||
| //        } | ||||
| //    }; | ||||
| 
 | ||||
| } | ||||
| @ -14,7 +14,7 @@ | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| package org.eclipse.python4j; | ||||
| package org.nd4j.python4j; | ||||
| 
 | ||||
| @lombok.Data | ||||
| public class PythonVariable<T> { | ||||
| @ -14,7 +14,7 @@ | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| package org.eclipse.python4j; | ||||
| package org.nd4j.python4j; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| import java.util.Arrays; | ||||
| @ -15,9 +15,12 @@ | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| 
 | ||||
| import org.eclipse.python4j.*; | ||||
| import org.junit.Assert; | ||||
| import org.junit.Test; | ||||
| import org.nd4j.python4j.PythonContextManager; | ||||
| import org.nd4j.python4j.PythonExecutioner; | ||||
| import org.nd4j.python4j.PythonTypes; | ||||
| import org.nd4j.python4j.PythonVariable; | ||||
| 
 | ||||
| import javax.annotation.concurrent.NotThreadSafe; | ||||
| import java.util.*; | ||||
|  | ||||
| @ -1,113 +0,0 @@ | ||||
| /******************************************************************************* | ||||
|  * Copyright (c) 2020 Konduit K.K. | ||||
|  * | ||||
|  * This program and the accompanying materials are made available under the | ||||
|  * terms of the Apache License, Version 2.0 which is available at | ||||
|  * https://www.apache.org/licenses/LICENSE-2.0. | ||||
|  * | ||||
|  * Unless required by applicable law or agreed to in writing, software | ||||
|  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||||
|  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||||
|  * License for the specific language governing permissions and limitations | ||||
|  * under the License. | ||||
|  * | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| 
 | ||||
| import org.bytedeco.javacpp.BytePointer; | ||||
| import org.bytedeco.javacpp.Loader; | ||||
| import org.eclipse.python4j.*; | ||||
| import org.junit.Assert; | ||||
| import org.junit.Test; | ||||
| import sun.nio.ch.DirectBuffer; | ||||
| 
 | ||||
| import javax.annotation.concurrent.NotThreadSafe; | ||||
| import java.nio.Buffer; | ||||
| import java.nio.ByteBuffer; | ||||
| import java.util.*; | ||||
| 
 | ||||
| @NotThreadSafe | ||||
| public class PythonBufferTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void testBuffer() { | ||||
|         ByteBuffer buff = ByteBuffer.allocateDirect(3); | ||||
|         buff.put((byte) 97); | ||||
|         buff.put((byte) 98); | ||||
|         buff.put((byte) 99); | ||||
|         buff.rewind(); | ||||
| 
 | ||||
|         BytePointer bp = new BytePointer(buff); | ||||
| 
 | ||||
|         List<PythonVariable> inputs = new ArrayList<>(); | ||||
|         inputs.add(new PythonVariable<>("buff", PythonTypes.MEMORYVIEW, buff)); | ||||
| 
 | ||||
|         List<PythonVariable> outputs = new ArrayList<>(); | ||||
|         outputs.add(new PythonVariable<>("s1", PythonTypes.STR)); | ||||
|         outputs.add(new PythonVariable<>("s2", PythonTypes.STR)); | ||||
| 
 | ||||
|         String code = "s1 = ''.join(chr(c) for c in buff)\nbuff[2] += 2\ns2 = ''.join(chr(c) for c in buff)"; | ||||
| 
 | ||||
|         PythonExecutioner.exec(code, inputs, outputs); | ||||
|         Assert.assertEquals("abc", outputs.get(0).getValue()); | ||||
|         Assert.assertEquals("abe", outputs.get(1).getValue()); | ||||
|         Assert.assertEquals(101, buff.get(2)); | ||||
| 
 | ||||
|     } | ||||
|     @Test | ||||
|     public void testBuffer2() { | ||||
|         ByteBuffer buff = ByteBuffer.allocateDirect(3); | ||||
|         buff.put((byte) 97); | ||||
|         buff.put((byte) 98); | ||||
|         buff.put((byte) 99); | ||||
|         buff.rewind(); | ||||
| 
 | ||||
|         BytePointer bp = new BytePointer(buff); | ||||
| 
 | ||||
|         List<PythonVariable> inputs = new ArrayList<>(); | ||||
|         inputs.add(new PythonVariable<>("buff", PythonTypes.MEMORYVIEW, bp)); | ||||
| 
 | ||||
|         List<PythonVariable> outputs = new ArrayList<>(); | ||||
|         outputs.add(new PythonVariable<>("s1", PythonTypes.STR)); | ||||
|         outputs.add(new PythonVariable<>("s2", PythonTypes.STR)); | ||||
| 
 | ||||
|         String code = "s1 = ''.join(chr(c) for c in buff)\nbuff[2] += 2\ns2 = ''.join(chr(c) for c in buff)"; | ||||
| 
 | ||||
|         PythonExecutioner.exec(code, inputs, outputs); | ||||
|         Assert.assertEquals("abc", outputs.get(0).getValue()); | ||||
|         Assert.assertEquals("abe", outputs.get(1).getValue()); | ||||
|         Assert.assertEquals(101, buff.get(2)); | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void testBuffer3() { | ||||
|         ByteBuffer buff = ByteBuffer.allocateDirect(3); | ||||
|         buff.put((byte) 97); | ||||
|         buff.put((byte) 98); | ||||
|         buff.put((byte) 99); | ||||
|         buff.rewind(); | ||||
| 
 | ||||
|         BytePointer bp = new BytePointer(buff); | ||||
| 
 | ||||
|         List<PythonVariable> inputs = new ArrayList<>(); | ||||
|         inputs.add(new PythonVariable<>("buff", PythonTypes.MEMORYVIEW, bp)); | ||||
| 
 | ||||
|         List<PythonVariable> outputs = new ArrayList<>(); | ||||
|         outputs.add(new PythonVariable<>("s1", PythonTypes.STR)); | ||||
|         outputs.add(new PythonVariable<>("s2", PythonTypes.STR)); | ||||
|         outputs.add(new PythonVariable<>("buff2", PythonTypes.MEMORYVIEW)); | ||||
|         String code = "s1 = ''.join(chr(c) for c in buff)\nbuff[2] += 2\ns2 = ''.join(chr(c) for c in buff)\nbuff2=buff[1:]"; | ||||
|         PythonExecutioner.exec(code, inputs, outputs); | ||||
| 
 | ||||
|         Assert.assertEquals("abc", outputs.get(0).getValue()); | ||||
|         Assert.assertEquals("abe", outputs.get(1).getValue()); | ||||
|         Assert.assertEquals(101, buff.get(2)); | ||||
|         BytePointer outBuffer = (BytePointer) outputs.get(2).getValue(); | ||||
|         Assert.assertEquals(2, outBuffer.capacity()); | ||||
|         Assert.assertEquals((byte)98, outBuffer.get(0)); | ||||
|         Assert.assertEquals((byte)101, outBuffer.get(1)); | ||||
| 
 | ||||
|     } | ||||
| } | ||||
| @ -15,9 +15,9 @@ | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| 
 | ||||
| import org.eclipse.python4j.PythonException; | ||||
| import org.eclipse.python4j.PythonObject; | ||||
| import org.eclipse.python4j.PythonTypes; | ||||
| import org.nd4j.python4j.PythonException; | ||||
| import org.nd4j.python4j.PythonObject; | ||||
| import org.nd4j.python4j.PythonTypes; | ||||
| import org.junit.Assert; | ||||
| import org.junit.Test; | ||||
| 
 | ||||
|  | ||||
| @ -16,9 +16,9 @@ | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| 
 | ||||
| import org.eclipse.python4j.Python; | ||||
| import org.eclipse.python4j.PythonContextManager; | ||||
| import org.eclipse.python4j.PythonExecutioner; | ||||
| import org.nd4j.python4j.Python; | ||||
| import org.nd4j.python4j.PythonContextManager; | ||||
| import org.nd4j.python4j.PythonExecutioner; | ||||
| import org.junit.Assert; | ||||
| import org.junit.Test; | ||||
| import javax.annotation.concurrent.NotThreadSafe; | ||||
|  | ||||
| @ -14,9 +14,9 @@ | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| import org.eclipse.python4j.Python; | ||||
| import org.eclipse.python4j.PythonGC; | ||||
| import org.eclipse.python4j.PythonObject; | ||||
| import org.nd4j.python4j.Python; | ||||
| import org.nd4j.python4j.PythonGC; | ||||
| import org.nd4j.python4j.PythonObject; | ||||
| import org.junit.Assert; | ||||
| import org.junit.Test; | ||||
| 
 | ||||
|  | ||||
| @ -14,10 +14,10 @@ | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| import org.eclipse.python4j.PythonContextManager; | ||||
| import org.eclipse.python4j.PythonJob; | ||||
| import org.eclipse.python4j.PythonTypes; | ||||
| import org.eclipse.python4j.PythonVariable; | ||||
| import org.nd4j.python4j.PythonContextManager; | ||||
| import org.nd4j.python4j.PythonJob; | ||||
| import org.nd4j.python4j.PythonTypes; | ||||
| import org.nd4j.python4j.PythonVariable; | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
|  | ||||
| @ -14,10 +14,9 @@ | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| import org.eclipse.python4j.*; | ||||
| import org.nd4j.python4j.*; | ||||
| import org.junit.Assert; | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import javax.annotation.concurrent.NotThreadSafe; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Arrays; | ||||
|  | ||||
| @ -15,12 +15,13 @@ | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| 
 | ||||
| import org.eclipse.python4j.PythonException; | ||||
| import org.eclipse.python4j.PythonObject; | ||||
| import org.eclipse.python4j.PythonTypes; | ||||
| import org.nd4j.python4j.*; | ||||
| import org.junit.Assert; | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 
 | ||||
| public class PythonPrimitiveTypesTest { | ||||
| 
 | ||||
|     @Test | ||||
| @ -78,5 +79,18 @@ public class PythonPrimitiveTypesTest { | ||||
| 
 | ||||
|         Assert.assertEquals(b, b3); | ||||
|     } | ||||
|     @Test | ||||
|     public void testBytes() { | ||||
|         byte[] bytes = new byte[]{97, 98, 99}; | ||||
|         List<PythonVariable> inputs = new ArrayList<>(); | ||||
|         inputs.add(new PythonVariable<>("buff", PythonTypes.BYTES, bytes)); | ||||
|         List<PythonVariable> outputs = new ArrayList<>(); | ||||
|         outputs.add(new PythonVariable<>("s1", PythonTypes.STR)); | ||||
|         outputs.add(new PythonVariable<>("buff2", PythonTypes.BYTES)); | ||||
|         String code = "s1 = ''.join(chr(c) for c in buff)\nbuff2=b'def'"; | ||||
|         PythonExecutioner.exec(code, inputs, outputs); | ||||
|         Assert.assertEquals("abc", outputs.get(0).getValue()); | ||||
|         Assert.assertArrayEquals(new byte[]{100, 101, 102}, (byte[])outputs.get(1).getValue()); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -4,7 +4,7 @@ | ||||
|          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||||
|     <parent> | ||||
|         <artifactId>python4j-parent</artifactId> | ||||
|         <groupId>org.eclipse</groupId> | ||||
|         <groupId>org.nd4j</groupId> | ||||
|         <version>1.0.0-SNAPSHOT</version> | ||||
|     </parent> | ||||
|     <modelVersion>4.0.0</modelVersion> | ||||
| @ -29,7 +29,7 @@ | ||||
|             <scope>test</scope> | ||||
|         </dependency> | ||||
|         <dependency> | ||||
|             <groupId>org.eclipse</groupId> | ||||
|             <groupId>org.nd4j</groupId> | ||||
|             <artifactId>python4j-core</artifactId> | ||||
|             <version>1.0.0-SNAPSHOT</version> | ||||
|         </dependency> | ||||
|  | ||||
| @ -15,26 +15,22 @@ | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| 
 | ||||
| package org.eclipse.python4j; | ||||
| package org.nd4j.python4j; | ||||
| 
 | ||||
| import lombok.extern.slf4j.Slf4j; | ||||
| import org.apache.commons.lang3.ArrayUtils; | ||||
| import org.bytedeco.cpython.PyObject; | ||||
| import org.bytedeco.cpython.PyTypeObject; | ||||
| import org.bytedeco.javacpp.Pointer; | ||||
| import org.bytedeco.javacpp.SizeTPointer; | ||||
| import org.bytedeco.numpy.PyArrayObject; | ||||
| import org.bytedeco.numpy.global.numpy; | ||||
| import org.nd4j.linalg.api.buffer.BaseDataBuffer; | ||||
| import org.nd4j.linalg.api.buffer.DataBuffer; | ||||
| import org.nd4j.linalg.api.buffer.DataType; | ||||
| import org.nd4j.linalg.api.concurrency.AffinityManager; | ||||
| import org.nd4j.linalg.api.memory.MemoryWorkspace; | ||||
| import org.nd4j.linalg.api.memory.MemoryWorkspaceManager; | ||||
| import org.nd4j.linalg.api.ndarray.INDArray; | ||||
| import org.nd4j.linalg.api.shape.Shape; | ||||
| import org.nd4j.linalg.factory.Nd4j; | ||||
| import org.nd4j.nativeblas.NativeOps; | ||||
| import org.nd4j.nativeblas.NativeOpsHolder; | ||||
| 
 | ||||
| import java.io.File; | ||||
| @ -1 +0,0 @@ | ||||
| org.eclipse.python4j.NumpyArray | ||||
| @ -0,0 +1 @@ | ||||
| org.nd4j.python4j.NumpyArray | ||||
| @ -15,13 +15,12 @@ | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| 
 | ||||
| import org.eclipse.python4j.*; | ||||
| import org.nd4j.python4j.*; | ||||
| import org.junit.Assert; | ||||
| import org.junit.Test; | ||||
| import org.junit.runner.RunWith; | ||||
| import org.junit.runners.Parameterized; | ||||
| import org.nd4j.linalg.api.buffer.DataType; | ||||
| import org.nd4j.linalg.api.concurrency.AffinityManager; | ||||
| import org.nd4j.linalg.api.ndarray.INDArray; | ||||
| import org.nd4j.linalg.factory.Nd4j; | ||||
| import org.nd4j.nativeblas.OpaqueDataBuffer; | ||||
|  | ||||
| @ -15,9 +15,9 @@ | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| 
 | ||||
| import org.eclipse.python4j.PythonException; | ||||
| import org.eclipse.python4j.PythonObject; | ||||
| import org.eclipse.python4j.PythonTypes; | ||||
| import org.nd4j.python4j.PythonException; | ||||
| import org.nd4j.python4j.PythonObject; | ||||
| import org.nd4j.python4j.PythonTypes; | ||||
| import org.junit.Assert; | ||||
| import org.junit.Test; | ||||
| import org.junit.runner.RunWith; | ||||
|  | ||||
| @ -14,9 +14,9 @@ | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| import org.eclipse.python4j.Python; | ||||
| import org.eclipse.python4j.PythonGC; | ||||
| import org.eclipse.python4j.PythonObject; | ||||
| import org.nd4j.python4j.Python; | ||||
| import org.nd4j.python4j.PythonGC; | ||||
| import org.nd4j.python4j.PythonObject; | ||||
| import org.junit.Assert; | ||||
| import org.junit.Test; | ||||
| import org.nd4j.linalg.factory.Nd4j; | ||||
|  | ||||
| @ -1,7 +1,7 @@ | ||||
| import org.eclipse.python4j.NumpyArray; | ||||
| import org.eclipse.python4j.Python; | ||||
| import org.eclipse.python4j.PythonGC; | ||||
| import org.eclipse.python4j.PythonObject; | ||||
| import org.nd4j.python4j.NumpyArray; | ||||
| import org.nd4j.python4j.Python; | ||||
| import org.nd4j.python4j.PythonGC; | ||||
| import org.nd4j.python4j.PythonObject; | ||||
| import org.junit.Assert; | ||||
| import org.junit.Test; | ||||
| import org.nd4j.linalg.api.buffer.DataType; | ||||
|  | ||||
| @ -14,7 +14,6 @@ | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| import org.eclipse.python4j.*; | ||||
| import org.junit.Assert; | ||||
| import org.junit.Test; | ||||
| import org.junit.runner.RunWith; | ||||
| @ -22,6 +21,7 @@ import org.junit.runners.Parameterized; | ||||
| import org.nd4j.linalg.api.buffer.DataType; | ||||
| import org.nd4j.linalg.api.ndarray.INDArray; | ||||
| import org.nd4j.linalg.factory.Nd4j; | ||||
| import org.nd4j.python4j.*; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
|  | ||||
| @ -14,7 +14,7 @@ | ||||
|  * SPDX-License-Identifier: Apache-2.0 | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| import org.eclipse.python4j.*; | ||||
| import org.nd4j.python4j.*; | ||||
| import org.junit.Assert; | ||||
| import org.junit.Test; | ||||
| import org.junit.runner.RunWith; | ||||
|  | ||||
| @ -15,18 +15,14 @@ | ||||
|  ******************************************************************************/ | ||||
| 
 | ||||
| 
 | ||||
| import org.eclipse.python4j.*; | ||||
| import org.junit.Assert; | ||||
| import org.junit.Test; | ||||
| import org.junit.runner.RunWith; | ||||
| import org.junit.runners.Parameterized; | ||||
| import org.nd4j.linalg.api.buffer.DataType; | ||||
| import org.nd4j.linalg.api.ndarray.INDArray; | ||||
| import org.nd4j.linalg.factory.Nd4j; | ||||
| import org.nd4j.python4j.NumpyArray; | ||||
| import org.nd4j.python4j.PythonTypes; | ||||
| 
 | ||||
| import javax.annotation.concurrent.NotThreadSafe; | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| 
 | ||||
| @NotThreadSafe | ||||
| public class PythonNumpyServiceLoaderTest { | ||||
| @ -36,6 +32,4 @@ public class PythonNumpyServiceLoaderTest { | ||||
|         Assert.assertEquals(NumpyArray.INSTANCE, PythonTypes.<INDArray>get("numpy.ndarray")); | ||||
|         Assert.assertEquals(NumpyArray.INSTANCE, PythonTypes.getPythonTypeForJavaObject(Nd4j.zeros(1))); | ||||
|     } | ||||
| 
 | ||||
| 
 | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user