Repository navigation
Expand file tree
/
Copy pathpython_import.i
More file actions
88 lines (78 loc) · 2.89 KB
/
Copy pathpython_import.i
File metadata and controls
88 lines (78 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* python-exiv2 - Python interface to libexiv2
* http://github.com/jim-easterbrook/python-exiv2
* Copyright (C) 2025-26 Jim Easterbrook [email protected]
*
* This file is part of python-exiv2.
*
* python-exiv2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* python-exiv2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with python-exiv2. If not, see <http://www.gnu.org/licenses/>.
*/
// Macro to declare a variable to store an imported object
%define DECLARE_IMPORT(full_name)
%fragment("declare_import"{full_name}, "header") {
static PyObject* Python_%mangle(full_name) = NULL;
}
%enddef // DECLARE_IMPORT
%fragment("import_from_python", "header") {
static PyObject* import_from_python(const char* package, const char* name) {
PyObject* mod = PyImport_ImportModule(package);
if (!mod)
return NULL;
PyObject* result = PyObject_GetAttrString(mod, name);
Py_DECREF(mod);
return result;
};
}
// Macro to import an object defined in a general Python (sub)package
%define IMPORT_PYTHON_OBJECT(package, name, cpp_name)
DECLARE_IMPORT(cpp_name)
%fragment("import_python_object"{cpp_name}, "init",
fragment="import_from_python",
fragment="declare_import"{cpp_name}) {
Python_%mangle(cpp_name) = import_from_python(#package, #name);
if (!Python_%mangle(cpp_name))
return -1;
}
%enddef // IMPORT_PYTHON_OBJECT
// Macro to import an object defined in a python-exiv2 module
%define IMPORT_MODULE_OBJECT(module, name)
DECLARE_IMPORT(Exiv2::name)
%fragment("import_module_object"{Exiv2::name}, "init",
fragment="import_from_python",
fragment="declare_import"{Exiv2::name}) {
Python_%mangle(Exiv2::name) = import_from_python("exiv2."#module, #name);
if (!Python_%mangle(Exiv2::name))
return -1;
}
%enddef // IMPORT_MODULE_OBJECT
// Macro to import an object defined in a python-exiv2 class
%define IMPORT_CLASS_OBJECT(module, class, name)
DECLARE_IMPORT(Exiv2::class::name)
%fragment("import_class_object"{Exiv2::class::name}, "init",
fragment="declare_import"{Exiv2::class::name}) {
{
PyObject* mod = PyImport_ImportModule("exiv2.module");
if (!mod)
return -1;
PyObject* parent = PyObject_GetAttrString(mod, "class");
Py_DECREF(mod);
if (!parent)
return -1;
Python_%mangle(Exiv2::class::name) = PyObject_GetAttrString(
parent, "name");
Py_DECREF(parent);
if (!Python_%mangle(Exiv2::class::name))
return -1;
}
}
%enddef // IMPORT_CLASS_OBJECT