Skip to content

Commit

Permalink
All runtime tests passing on arm64 except test_importlib_resources
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsmith committed Nov 22, 2023
1 parent 47da664 commit 6924956
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
13 changes: 12 additions & 1 deletion demo/app/src/utils/java/com/chaquo/python/utils/App.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.chaquo.python.utils;

import android.app.*;
import android.content.*;

import androidx.preference.*;

import com.chaquo.python.*;
import com.chaquo.python.android.*;


public class App extends PyApplication {
public class App extends Application {

public static App context;
public static SharedPreferences prefs;
Expand All @@ -15,6 +19,13 @@ public void onCreate() {
super.onCreate();
context = this;
prefs = PreferenceManager.getDefaultSharedPreferences(this);
AndroidPlatform platform = new AndroidPlatform(this);

// This is disabled by default to allow test_stream to check the non-redirected
// state. Re-enable it to see any errors that happen during Python startup.
// platform.redirectStdioToLogcat();

Python.start(platform);
}

}
8 changes: 6 additions & 2 deletions product/runtime/src/main/python/java/android/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from calendar import timegm
from contextlib import contextmanager, nullcontext
import ctypes
import imp
from importlib import _bootstrap, _bootstrap_external, machinery, util
from inspect import getmodulename
import io
Expand Down Expand Up @@ -36,7 +35,8 @@ def initialize(context, build_json, app_path):
nativeLibraryDir = context.getApplicationInfo().nativeLibraryDir
initialize_importlib(context, build_json, app_path)
initialize_ctypes()
initialize_imp()
if sys.version_info < (3, 12):
initialize_imp()


def initialize_importlib(context, build_json, app_path):
Expand Down Expand Up @@ -178,6 +178,8 @@ def CDLL_init_override(self, name, *args, **kwargs):


def initialize_imp():
import imp

# The standard implementations of imp.find_module and imp.load_module do not use the PEP
# 302 import system. They are therefore only capable of loading from directory trees and
# built-in modules, and will ignore both sys.path_hooks and sys.meta_path. To accommodate
Expand All @@ -190,6 +192,8 @@ def initialize_imp():


def find_module_override(base_name, path=None):
import imp

# When calling find_module_original, we can't just replace None with sys.path, because None
# will also search built-in modules.
path_original = path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from contextlib import contextmanager
import ctypes
from ctypes.util import find_library
import imp
from importlib import import_module, metadata, reload, resources
import importlib.util
from importlib.util import cache_from_source, MAGIC_NUMBER
Expand All @@ -15,6 +14,7 @@
from shutil import rmtree
import sys
from traceback import format_exc
from unittest import expectedFailure, skipIf
import types
from warnings import catch_warnings, filterwarnings

Expand All @@ -41,6 +41,10 @@ def asset_path(zip_name, *paths):

class TestAndroidImport(FilterWarningsCase):

# This will be fixed in Python 3.12.1 (https://github.com/python/cpython/pull/110247)
expectedFailure312 = expectedFailure if sys.version_info >= (3, 12) else lambda x: x

@expectedFailure312
def test_bootstrap(self):
chaquopy_dir = join(str(context.getFilesDir()), "chaquopy")
self.assertCountEqual(["AssetFinder", "bootstrap-native", "bootstrap.imy",
Expand All @@ -49,11 +53,18 @@ def test_bootstrap(self):
bn_dir = f"{chaquopy_dir}/bootstrap-native"
self.assertCountEqual([ABI], os.listdir(bn_dir))

stdlib_bootstrap_expected = {
# This is the list from our minimum Python version. For why each of these
# modules is needed, see BOOTSTRAP_NATIVE_STDLIB in PythonTasks.kt.
"java", "_bz2.so", "_ctypes.so", "_datetime.so", "_lzma.so", "_random.so",
"_sha512.so", "_struct.so", "binascii.so", "math.so", "mmap.so", "zlib.so",
}
if sys.version_info >= (3, 12):
stdlib_bootstrap_expected -= {"_sha512.so"}
stdlib_bootstrap_expected |= {"_sha2.so"}

for subdir, entries in [
# For why each of these modules are needed, see BOOTSTRAP_NATIVE_STDLIB
# in PythonTasks.kt.
(ABI, ["java", "_bz2.so", "_ctypes.so", "_datetime.so", "_lzma.so", "_random.so",
"_sha512.so", "_struct.so", "binascii.so", "math.so", "mmap.so", "zlib.so"]),
(ABI, stdlib_bootstrap_expected),
(f"{ABI}/java", ["chaquopy.so"]),
]:
with self.subTest(subdir=subdir):
Expand Down Expand Up @@ -425,7 +436,7 @@ def test_exception(self):
test_frame +
fr' File "{asset_path(REQS_COMMON_ZIP)}/murmurhash/__init__.py", '
fr'line 5, in get_include\n'
fr"NameError: name '__file__' is not defined\n$")
fr"NameError: name '__file__' is not defined")
else:
self.fail()
finally:
Expand All @@ -447,7 +458,10 @@ def test_exception(self):
else:
self.fail()

@skipIf(sys.version_info >= (3, 12), "imp was removed in Python 3.12")
def test_imp(self):
import imp

with catch_warnings():
filterwarnings("default", category=DeprecationWarning)

Expand Down Expand Up @@ -522,9 +536,13 @@ def test_imp(self):
else:
self.assertEqual(expected_type, actual_type)

# This trick was used by Electron Cash to load modules under a different name. The Electron
# Cash Android app no longer needs it, but there may be other software which does.
# This trick was used by Electron Cash to load modules under a different name. The
# Electron Cash Android app no longer needs it, but there may be other software
# which does.
@skipIf(sys.version_info >= (3, 12), "imp was removed in Python 3.12")
def test_imp_rename(self):
import imp

with catch_warnings():
filterwarnings("default", category=DeprecationWarning)

Expand Down Expand Up @@ -646,12 +664,17 @@ def check_iter_modules(mod, expected):
self.assertEqual([], mod_infos)

def test_pr_distributions(self):
import pkg_resources as pr
with catch_warnings():
filterwarnings("default", category=DeprecationWarning)
import pkg_resources as pr

self.assertCountEqual(REQUIREMENTS, [dist.project_name for dist in pr.working_set])
self.assertEqual("0.28.0", pr.get_distribution("murmurhash").version)

def test_pr_resources(self):
import pkg_resources as pr
with catch_warnings():
filterwarnings("default", category=DeprecationWarning)
import pkg_resources as pr

# App ZIP
pkg = "android1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from os.path import dirname, exists, join, realpath
import subprocess
import sys
from unittest import expectedFailure
from warnings import catch_warnings, filterwarnings

from android.os import Build
Expand All @@ -21,6 +22,10 @@ def test_datetime(self):
# imported.
self.assertTrue(datetime.datetime_CAPI)

# This will be fixed in Python 3.12.1 (https://github.com/python/cpython/pull/110247)
expectedFailure312 = expectedFailure if sys.version_info >= (3, 12) else lambda x: x

@expectedFailure312
def test_hashlib(self):
import hashlib
INPUT = b"The quick brown fox jumps over the lazy dog"
Expand Down Expand Up @@ -224,10 +229,11 @@ def test_sysconfig(self):
ldlibrary = "libpython{}.{}.so".format(*sys.version_info[:2])
self.assertEqual(ldlibrary, sysconfig.get_config_vars()["LDLIBRARY"])

with catch_warnings():
filterwarnings("default", category=DeprecationWarning)
import distutils.sysconfig
self.assertEqual(ldlibrary, distutils.sysconfig.get_config_vars()["LDLIBRARY"])
if sys.version_info < (3, 12):
with catch_warnings():
filterwarnings("default", category=DeprecationWarning)
import distutils.sysconfig
self.assertEqual(ldlibrary, distutils.sysconfig.get_config_vars()["LDLIBRARY"])

def test_tempfile(self):
import tempfile
Expand Down

0 comments on commit 6924956

Please sign in to comment.