<commit_before>"""
Test that importing flexx subpackages does not pull in any more flexx
submodules than strictly necessary, and not any more 3d party
dependencies than expected.
"""

import os
import sys
import subprocess

from flexx.util.testing import run_tests_if_main, raises, skip

import flexx

if '__pypy__' in sys.builtin_module_names and os.getenv('TRAVIS', '') == 'true':
    skip('These import tests are slow on pypy')

# minimum that will be imported when importing flexx
PROJECT_MODULE = flexx
MIN_MODULES = ['flexx']
PROJECT_NAME = PROJECT_MODULE.__name__

## Generic code

def loaded_modules(import_module, depth=None, all_modules=False):
    """ Import the given module in subprocess and return loaded modules

    Import a certain module in a clean subprocess and return the
    projects modules that are subsequently loaded. The given depth
    indicates the module level (i.e. depth=1 will only yield 'X.app'
    but not 'X.app.backends').
    """

    project_dir = os.path.dirname(os.path.dirname(PROJECT_MODULE.__file__))

    # Get the loaded modules in a clean interpreter
    code = "import sys, %s; print(', '.join(sys.modules))" % import_module
    res = subprocess.check_output([sys.executable, '-c', code], cwd=project_dir,
                                  stderr=subprocess.STDOUT).decode()
    loaded_modules = [name.strip() for name in res.split(',')]

    if all_modules:
        return loaded_modules

    # Select project modules at the given depth
    project_modules = set()
    for m in loaded_modules:
        if m.startswith(PROJECT_NAME) and '__future__' not in m:
            if depth:
                parts = m.split('.')
                m = '.'.join(parts[:depth])
            project_modules.add(m)
    
    return project_modules
<commit_msg>fix import tests for legacy py<commit_after>"""
Test that importing flexx subpackages does not pull in any more flexx
submodules than strictly necessary, and not any more 3d party
dependencies than expected.
"""

import os
import sys
import subprocess

from flexx.util.testing import run_tests_if_main, raises, skip

import flexx

if '__pypy__' in sys.builtin_module_names and os.getenv('TRAVIS', '') == 'true':
    skip('These import tests are slow on pypy')

# minimum that will be imported when importing flexx
PROJECT_MODULE = flexx
MIN_MODULES = ['flexx']
PROJECT_NAME = 'flexx'

## Generic code

def loaded_modules(import_module, depth=None, all_modules=False):
    """ Import the given module in subprocess and return loaded modules

    Import a certain module in a clean subprocess and return the
    projects modules that are subsequently loaded. The given depth
    indicates the module level (i.e. depth=1 will only yield 'X.app'
    but not 'X.app.backends').
    """

    project_dir = os.path.dirname(os.path.dirname(PROJECT_MODULE.__file__))

    # Get the loaded modules in a clean interpreter
    code = "import sys, %s; print(', '.join(sys.modules))" % import_module
    res = subprocess.check_output([sys.executable, '-c', code], cwd=project_dir,
                                  stderr=subprocess.STDOUT).decode()
    loaded_modules = [name.strip() for name in res.split(',')]
    
    # Tweaks for legacy Python
    loaded_modules = [name.replace('flexx_legacy', 'flexx') for name in loaded_modules]
    if 'flexx.sys' in loaded_modules:
        loaded_modules.remove('flexx.sys')
    
    if all_modules:
        return loaded_modules

    # Select project modules at the given depth
    project_modules = set()
    for m in loaded_modules:
        if m.startswith(PROJECT_NAME) and '__future__' not in m:
            if depth:
                parts = m.split('.')
                m = '.'.join(parts[:depth])
            project_modules.add(m)
    
    return project_modules

{'fraction_non_alphanumeric': 0.09313984168865436, 'fraction_numerical': 0.0010554089709762533, 'mean_word_length': 3.2028824833702885, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4099044', 'n_tokens_mistral': 1122, 'n_tokens_neox': 1100, 'n_words': 379}