Skip to content

Commit

Permalink
refactor self-test.py to use python3
Browse files Browse the repository at this point in the history
  • Loading branch information
lyskov committed Feb 1, 2024
1 parent ee2ecff commit 874e030
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
19 changes: 16 additions & 3 deletions source/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,11 @@ void request_bindings(clang::QualType const &qt, Context &context)
/// return standard string representation of a given type
std::string standard_name(clang::QualType const &qt)
{
//qt.dump();
//qt.getUnqualifiedType().dump();
//qt->getCanonicalTypeInternal().dump();
//qt.getTypePtr()->dump();
//outs() << qt->getTypeClassName() << '\n';

string r = qt.getAsString();

// if( r == "std::array::size_type" ) {
Expand All @@ -468,9 +472,18 @@ std::string standard_name(clang::QualType const &qt)
// }

// if( begins_with(r, "std::") ) return r; //standard_name(r);
static std::set<string> standard_names {"std::size_t"};

if( standard_names.count(r) ) return r;
// static std::set<string> standard_names {"std::size_t"};
// if( standard_names.count(r) ) return r;
// else return standard_name(qt.getCanonicalType().getAsString());


// static std::set<string> standard_names {"std::size_t"};
// if( standard_names.count(r) ) return r;
// else return standard_name(qt.getCanonicalType().getAsString());

static std::set<string> standard_names_to_ignore {"std::size_t", }; // "std::array::size_type",
if( standard_names_to_ignore.count(r) ) return r;
else return standard_name(qt.getCanonicalType().getAsString());
}

Expand Down
40 changes: 36 additions & 4 deletions test/self-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ def execute(message, command_line, return_='status', until_successes=False, term
else: return exit_code


class NT: # named tuple
def __init__(self, **entries): self.__dict__.update(entries)
def __repr__(self):
r = 'NT: |'
for i in dir(self):
print(i)
if not i.startswith('__') and i != '_as_dict' and not isinstance(getattr(self, i), types_module.MethodType): r += '%s --> %s, ' % (i, getattr(self, i))
return r[:-2]+'|'

@property
def _as_dict(self):
return { a: getattr(self, a) for a in dir(self) if not a.startswith('__') and a != '_as_dict' and not isinstance(getattr(self, a), types_module.MethodType)}


def get_test_files(dir_):
return [dir_ + '/' + f for f in os.listdir(dir_) if f.startswith('T') and f.endswith('.hpp')]

Expand All @@ -57,7 +71,23 @@ def remover_absolute_paths(source):
with open(source, 'w') as f: f.write(data.replace(path, 'TEST'))


def run_test(test_path, build_dir):
def get_python_environment():
''' calculate python include dir and lib dir from given python executable path
'''
python = sys.executable
python_bin_dir = python.rpartition('/')[0]
python_config = f'{python} {python}-config' if python.endswith('2.7') else f'{python}-config'

info = execute('Getting python configuration info...', f'unset __PYVENV_LAUNCHER__ && cd {python_bin_dir} && PATH=.:$PATH && {python_config} --prefix --includes', return_='output').replace('\r', '').split('\n') # Python-3 only: --abiflags
python_prefix = info[0]
python_include_dir = info[1].split()[0][len('-I'):]
python_lib_dir = python_prefix + '/lib'

return NT(python=python, python_include_dir=python_include_dir, python_lib_dir=python_lib_dir)



def run_test(test_path, build_dir, pyenv):
if not os.path.isfile(test_path): print('Could not find test file: {} ... Exiting...'.format(test_path)); sys.exit(1)

test = os.path.basename(test_path)
Expand All @@ -79,7 +109,8 @@ def run_test(test_path, build_dir):
with open(cli_file_name) as f: cli = ' ' + f.read()
except FileNotFoundError as e: cli = ''

python_includes = '-I/usr/include/python2.7'
python = pyenv.python
python_includes = '-I'+pyenv.python_include_dir #'-I/usr/include/python2.7'

command_line = '{binder} --bind "" --root-module {root_module} --prefix {build_dir} --single-file --annotate-includes {config}{cli} {source} -- -x c++ -std=c++11 -I {source_dir} -I {source_dir}/.. -isystem {pybind11} {python_includes}' \
.format(binder=Options.binder, root_module=root_module, build_dir=build_dir, source_dir=source_dir, cli=cli, source=source_include,
Expand All @@ -90,7 +121,7 @@ def run_test(test_path, build_dir):
command_line = 'cd {build_dir} && clang++ -O3 -shared -std=c++11 -isystem {pybind11} {python_includes} -I./.. -I./../.. -I./../../source {root_module}.cpp -o {root_module}.so -fPIC'.format(pybind11=Options.pybind11, root_module=root_module, build_dir=build_dir, python_includes=python_includes)
execute('{} Compiling binding results...'.format(test), command_line);

command_line = "cd {build_dir} && python2.7 -c 'import {root_module}'".format(root_module=root_module, build_dir=build_dir)
command_line = f"cd {build_dir} && {python} -c 'import {root_module}'"
execute('{} Testing imports...'.format(test), command_line);

ref = source_dir + '/' + test_name + '.ref.cpp'
Expand Down Expand Up @@ -134,7 +165,8 @@ def main():
os.makedirs(build_dir)
#if not os.path.isdir(build_dir): os.makedirs(build_dir)

for t in tests: run_test(t, build_dir)
pyenv = get_python_environment()
for t in tests: run_test(t, build_dir, pyenv)

print('Done!')

Expand Down

0 comments on commit 874e030

Please sign in to comment.