Skip to content

Commit

Permalink
gnome: Add a test consuming Python
Browse files Browse the repository at this point in the history
  • Loading branch information
amyspark committed Feb 12, 2025
1 parent 490b0a0 commit 5f8546d
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 9 deletions.
51 changes: 51 additions & 0 deletions test cases/frameworks/7 gnome/gir/meson-python-sample.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "meson-python-sample.h"

#include <Python.h>

struct _MesonPythonSample
{
GObject parent_instance;
};

G_DEFINE_TYPE (MesonPythonSample, meson_python_sample, G_TYPE_OBJECT)

/**
* meson_python_sample_new:
*
* Allocates a new #MesonPythonSample.
*
* Returns: (transfer full): a #MesonPythonSample.
*/
MesonPythonSample *
meson_python_sample_new (void)
{
return g_object_new (MESON_TYPE_PYTHON_SAMPLE, NULL);
}

static void
meson_python_sample_class_init (MesonPythonSampleClass *klass)
{
if (!Py_IsInitialized ()) {
Py_Initialize ();
Py_Finalize ();
}
}

static void
meson_python_sample_init (MesonPythonSample *self)
{
}

/**
* meson_python_sample_print_message:
* @self: a #MesonSample2.
*
* Prints Hello.
*
* Returns: Nothing.
*/
void
meson_python_sample_print_message (MesonPythonSample *self)
{
g_print ("Message: Hello\n");
}
4 changes: 4 additions & 0 deletions test cases/frameworks/7 gnome/gir/meson-python-sample.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
EXPORTS
meson_python_sample_new
meson_python_sample_print_message
meson_python_sample_get_type
17 changes: 17 additions & 0 deletions test cases/frameworks/7 gnome/gir/meson-python-sample.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef MESON_PYTHON_SAMPLE_H
#define MESON_PYTHON_SAMPLE_H

#include <glib-object.h>

G_BEGIN_DECLS

#define MESON_TYPE_PYTHON_SAMPLE (meson_python_sample_get_type())

G_DECLARE_FINAL_TYPE (MesonPythonSample, meson_python_sample, MESON, SAMPLE, GObject)

MesonPythonSample *meson_python_sample_new (void);
void meson_python_sample_print_message (MesonPythonSample *self);

G_END_DECLS

#endif /* MESON_PYTHON_SAMPLE_H */
33 changes: 30 additions & 3 deletions test cases/frameworks/7 gnome/gir/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ subdir('dep1')

libsources = ['meson-sample.c', 'meson-sample.h']
lib2sources = ['meson-sample2.c', 'meson-sample2.h']
pythonsources = ['meson-python-sample.c', 'meson-python-sample.h']

gen_source = custom_target(
'meson_sample3.h',
Expand All @@ -28,6 +29,23 @@ girlib2 = shared_library(
install : true
)

if get_option('b_sanitizer') == 'none'
py3_dep = py3.dependency(embed: true)
else
warning('Python 3 test not supported with b_sanitizer')
py3_dep = disabler()
endif

if py3_dep.found()
pythongirlib = shared_library(
'python_gir_lib',
sources: pythonsources,
dependencies: [gobj, py3_dep],
vs_module_defs: 'meson-python-sample.def',
install: true
)
endif

girexe = executable(
'girprog',
sources : 'prog.c',
Expand All @@ -44,17 +62,26 @@ flags_dep_for_msvc = declare_dependency(
compile_args: ['-DMESON_TEST_2']
)

girs = [girlib, girlib2]
girs_sources = [libsources, lib2sources, gen_source]
girs_deps = [fake_dep, dep1_dep, flags_dep_for_msvc]
if py3_dep.found()
girs += [pythongirlib]
girs_sources += [pythonsources]
girs_deps += [py3_dep]
endif

gnome.generate_gir(
girlib, girlib2,
sources : [libsources, lib2sources, gen_source],
girs,
sources : girs_sources,
env : envdata,
nsversion : '1.0',
namespace : 'Meson',
symbol_prefix : 'meson',
identifier_prefix : 'Meson',
includes : ['GObject-2.0', 'MesonDep1-1.0'],
# dep1_dep pulls in dep2_dep for us
dependencies : [[fake_dep, dep1_dep, flags_dep_for_msvc]],
dependencies : girs_deps,
install : true,
build_by_default : true,
)
Expand Down
6 changes: 3 additions & 3 deletions test cases/frameworks/7 gnome/meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project('gobject-introspection', 'c')
project('gobject-introspection', 'c', meson_version: '>= 1.2.0')

copyfile = find_program('copyfile.py')
copyfile_gen = generator(copyfile,
Expand All @@ -15,8 +15,8 @@ if not gir.found()
error('MESON_SKIP_TEST gobject-introspection not found.')
endif

python3 = import('python3')
py3 = python3.find_python()
python3 = import('python')
py3 = python3.find_installation()
if run_command(py3, '-c', 'import gi;', check: false).returncode() != 0
error('MESON_SKIP_TEST python3-gi not found')
endif
Expand Down
4 changes: 1 addition & 3 deletions test cases/frameworks/7 gnome/resources-data/meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
subdir('subdir')

python3 = import('python3').find_python()

fake_generator_script = '''
import os, sys
assert os.path.exists(sys.argv[1]), "File %s not found" % sys.argv[1]
Expand All @@ -13,6 +11,6 @@ print("This is a generated resource.")
res3_txt = custom_target('res3',
input: 'res3.txt.in',
output: 'res3.txt',
command: [python3, '-c', fake_generator_script, '@INPUT@'],
command: [py3, '-c', fake_generator_script, '@INPUT@'],
capture: true,
)

0 comments on commit 5f8546d

Please sign in to comment.