Skip to content

Commit

Permalink
cy3: Changes to get pyjnius and pyobjus working with cy3.
Browse files Browse the repository at this point in the history
  • Loading branch information
renpytom committed Dec 17, 2024
1 parent aa37245 commit 38a6fb4
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Avoid searching for the java home on Windows.
Avoid searching for the JDK home on windows.

From: Tom Rothamel <[email protected]>

Expand All @@ -8,15 +8,15 @@ From: Tom Rothamel <[email protected]>
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/jnius/__init__.py b/jnius/__init__.py
index 251beb4..376c3e7 100644
index c1300ef..855c0c8 100644
--- a/jnius/__init__.py
+++ b/jnius/__init__.py
@@ -13,7 +13,7 @@ from .env import get_jnius_lib_location, get_jdk_home

import os
import sys
-if sys.platform == 'win32' and sys.version_info >= (3, 8):
+if False and sys.platform == 'win32' and sys.version_info >= (3, 8):
path = os.path.dirname(__file__)
jdk_home = get_jdk_home(sys.platform)
with os.add_dll_directory(path):
@@ -13,7 +13,7 @@ from .env import get_java_setup

import os
import sys
-if sys.platform == 'win32' and sys.version_info >= (3, 8):
+if False and sys.platform == 'win32' and sys.version_info >= (3, 8):
path = os.path.dirname(__file__)
java = get_java_setup(sys.platform)
jdk_home = java.get_javahome()
87 changes: 87 additions & 0 deletions patches/pyjnius-1.6.1/py3-division.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
Change division to match Python 3 semantics.

From: Tom Rothamel <[email protected]>


---
jnius/jnius_conversion.pxi | 22 +++++++++++-----------
1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/jnius/jnius_conversion.pxi b/jnius/jnius_conversion.pxi
index 2e0b48d..d9fb0cd 100644
--- a/jnius/jnius_conversion.pxi
+++ b/jnius/jnius_conversion.pxi
@@ -112,13 +112,13 @@ cdef void populate_args(JNIEnv *j_env, tuple definition_args, jvalue *j_args, ar

# lambda or function
elif callable(py_arg):
-
+
# we need to make a java object in python
py_arg = convert_python_callable_to_jobject(argtype, py_arg)

- # TODO: this line should not be needed to prevent py_arg from being GCd
+ # TODO: this line should not be needed to prevent py_arg from being GCd
activeLambdaJavaProxies.add(py_arg)
-
+
# next few lines is from "isinstance(py_arg, PythonJavaClass)" above
# except jc is None is removed, as we know it has been called by
# convert_python_callable_to_jobject()
@@ -399,7 +399,7 @@ def get_param_signature(m):
return rtr

def convert_python_callable_to_jobject(definition, pyarg):
-
+
objmethods = set(["equals", "notify", "notifyAll", "toString", "wait", "getClass"])
# we assume that definition is java/util/function/Function
# definition = "Ljava/util/function/Function;"
@@ -411,7 +411,7 @@ def convert_python_callable_to_jobject(definition, pyarg):

if not clz.isInterface():
raise JavaException('%s is not an interface that can be instantiated with a callable' % classname)
-
+
# A functional interface is an interface that has just one
# abstract method (aside from the methods of Object)
# https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.8
@@ -427,16 +427,16 @@ def convert_python_callable_to_jobject(definition, pyarg):
continue
# ignore methods that are in Object
if m.getName() in objmethods:
- continue
+ continue
candidateFunctionalMethods.append(m)
if len(candidateFunctionalMethods) != 1:
- raise JavaException("%s is not a functional interface (%d methods) that can be instantiated with a callable: %s"
+ raise JavaException("%s is not a functional interface (%d methods) that can be instantiated with a callable: %s"
% (classname, len(candidateFunctionalMethods), str(list(map(lambda m : m.getName(), candidateFunctionalMethods )))))
-
+
# functional method has been identified
functionalMethod = candidateFunctionalMethods[0]
functionalMethodName = functionalMethod.getName()
-
+
# we need a new Python class that will implement the correct interface
class PythonLambdaArg(PythonJavaClass):
__javainterfaces__ = [classname]
@@ -449,7 +449,7 @@ def convert_python_callable_to_jobject(definition, pyarg):

# finally add the method to the instance. we use the same name
setattr(intfInstance, functionalMethodName, pyarg)
-
+
# we have added a method after __init__ () was called
# so we need to re-run introspection
intfInstance._init_j_self_ptr()
@@ -606,7 +606,7 @@ cdef jstring convert_pystr_to_java(JNIEnv *j_env, unicode py_uni) except NULL:
py_bytes = py_uni.encode('utf-16')
# skip byte-order mark
buff = (<char *>py_bytes) + sizeof(jchar)
- j_strlen = int(len(py_bytes) / sizeof(jchar) - 1)
+ j_strlen = int(len(py_bytes) // sizeof(jchar) - 1)
j_str = j_env[0].NewString(j_env, <jchar *>buff, j_strlen)

if j_str == NULL:
3 changes: 3 additions & 0 deletions patches/pyjnius-1.6.1/series
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# This series applies on GIT commit f5653e70b1e81c8d9df76e5ffde665fbfc5d85cc
no-win-jdk-home.diff
py3-division.diff
22 changes: 0 additions & 22 deletions patches/pyjnius/py3-division.diff

This file was deleted.

3 changes: 0 additions & 3 deletions patches/pyjnius/series

This file was deleted.

25 changes: 25 additions & 0 deletions patches/pyobjus/ffi-h.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
diff --git a/pyobjus/_runtime.h b/pyobjus/_runtime.h
index c31b7ba..00ede0e 100644
--- a/pyobjus/_runtime.h
+++ b/pyobjus/_runtime.h
@@ -1,6 +1,6 @@
#include <objc/runtime.h>
#include <objc/message.h>
-#include <ffi/ffi.h>
+#include <ffi.h>
#include <stdio.h>
#include <dlfcn.h>
#include <string.h>
diff --git a/pyobjus/common.pxi b/pyobjus/common.pxi
index 61a835d..5daa50b 100644
--- a/pyobjus/common.pxi
+++ b/pyobjus/common.pxi
@@ -110,7 +110,7 @@ cdef extern from "objc/runtime.h":
objc_method_description* protocol_copyMethodDescriptionList(Protocol *p, BOOL isRequiredMethod, BOOL isInstanceMethod, unsigned int *outCount)


-cdef extern from "ffi/ffi.h":
+cdef extern from "ffi.h":
ctypedef unsigned long ffi_arg
ctypedef signed long ffi_sarg
ctypedef enum: FFI_TYPE_STRUCT
2 changes: 2 additions & 0 deletions patches/pyobjus/series
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This series applies on GIT commit 573bd69b857ca4a64c7154a27f0bff6850851797
ffi-h.diff
Binary file removed source/pyjnius-1.2.1.tar.gz
Binary file not shown.
Binary file added source/pyjnius-1.6.1.tar.gz
Binary file not shown.
Binary file removed source/pyobjus-1.1.0.tar.gz
Binary file not shown.
9 changes: 4 additions & 5 deletions tasks/cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
import sys


@task(kind="python", always=True)
@task(kind="host-python", always=True)
def check(c: Context):
"""
Check that cython works.
"""

c.clean()
c.clean()
c.run("""touch test.pyx""")

try:
c.run("""cython test.pyx""")
c.run("""cython --3str test.pyx""")
except:
print("", file=sys.stderr)
print("Cython could not be run.", file=sys.stderr)
raise SystemExit(1)
raise SystemExit("Cython could not be run.")
12 changes: 6 additions & 6 deletions tasks/pyjnius.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from renpybuild.task import task
import re

version = "1.2.1"
version = "1.6.1"


@task(kind="host-python")
Expand All @@ -17,8 +17,8 @@ def patch(c: Context):
c.var("version", version)
c.chdir("pyjnius-{{version}}/")

c.patch("pyjnius/py3-division.diff")
c.patch("pyjnius/no-win-jdk-home.diff")
c.patch("pyjnius-{{version}}/py3-division.diff")
c.patch("pyjnius-{{version}}/no-win-jdk-home.diff")

@task(kind="host-python")
def build(c: Context):
Expand All @@ -28,11 +28,11 @@ def build(c: Context):

with open(c.path("config.pxi"), "w") as f:
f.write(c.expand("""\
DEF JNIUS_PLATFORM = 'android'
# cython: language_level={{ c.python }}
DEF JNIUS_PYTHON3 = {{ c.python == '3' }}
DEF JNIUS_PLATFORM = 'android'
DEF JNIUS_CYTHON_3 = True
"""))

# on android, rely on SDL to get the JNI env
Expand Down
15 changes: 6 additions & 9 deletions tasks/pyobjus.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@ def unpack(c: Context):
c.run("git clone https://github.com/kivy/pyobjus pyobjus")
c.chdir("pyobjus")


if c.platform == "mac" and c.arch == "arm64":
c.run("git checkout 9c0ca61c0cd67efd5371dba8deb36b6dbccddb64")
else:
c.run("git checkout ea4ef7c96dcc83d5f1f18d4b15f3709f32c47a24")

c.run("git checkout 573bd69b857ca4a64c7154a27f0bff6850851797")
c.patch("pyobjus/ffi-h.diff")


@task(kind="host-python")
Expand All @@ -33,7 +29,9 @@ def host_unpack(c: Context):

c.run("git clone https://github.com/kivy/pyobjus pyobjus")
c.chdir("pyobjus")
c.run("git checkout ea4ef7c96dcc83d5f1f18d4b15f3709f32c47a24")

c.run("git checkout 573bd69b857ca4a64c7154a27f0bff6850851797")
c.patch("pyobjus/ffi-h.diff")


@task(kind="python", platforms="mac,ios")
Expand All @@ -47,6 +45,7 @@ def build(c: Context):
f.write(c.expand("""\
DEF PLATFORM = "{{ 'ios' if c.platform == 'ios' else 'darwin' }}"
DEF ARCH = "{{ c.arch.replace('sim-', '') }}"
DEF PYOBJUS_CYTHON_3 = True
"""))

c.run("""cython --3str pyobjus.pyx""")
Expand All @@ -59,8 +58,6 @@ def build(c: Context):
with open(c_fn, 'r') as f:
ccode = f.read()

ccode = ccode.replace("ffi/ffi.h", "ffi.h")

ccode = re.sub(r'Py_InitModule4\("([^"]+)"', 'Py_InitModule4("' + parent_module + '.\\1"', ccode)
ccode = re.sub(r'^__Pyx_PyMODINIT_FUNC init', '__Pyx_PyMODINIT_FUNC init' + parent_module_identifier + '_', ccode, 0, re.MULTILINE) # Cython 0.28.2
ccode = re.sub(r'^PyMODINIT_FUNC init', 'PyMODINIT_FUNC init' + parent_module_identifier + '_', ccode, 0, re.MULTILINE) # Cython 0.25.2
Expand Down

0 comments on commit 38a6fb4

Please sign in to comment.