1 | // -*- c++ -*- |
---|
2 | //***************************************************************************** |
---|
3 | /** @file pyobject_setup.cc |
---|
4 | * |
---|
5 | * @author Alexander Dreyer |
---|
6 | * @date 2010-12-15 |
---|
7 | * |
---|
8 | * This header file defines the @c blackbox setup operations for the pyobject |
---|
9 | * |
---|
10 | * @par Copyright: |
---|
11 | * (c) 2010 by The Singular Team, see LICENSE file |
---|
12 | **/ |
---|
13 | //***************************************************************************** |
---|
14 | |
---|
15 | #include "config.h" |
---|
16 | #include <kernel/mod2.h> |
---|
17 | #include <kernel/febase.h> |
---|
18 | #include <Singular/blackbox.h> |
---|
19 | #include <Singular/ipshell.h> |
---|
20 | |
---|
21 | /* whether pyobject module is linked statically or dynamically */ |
---|
22 | |
---|
23 | #ifdef EMBED_PYTHON // Case: we include the pyobject interface in the binary |
---|
24 | |
---|
25 | #ifdef HAVE_PYTHON |
---|
26 | #include "pyobject.cc" |
---|
27 | static BOOLEAN pyobject_load() |
---|
28 | { |
---|
29 | pyobject_init(iiAddCproc); |
---|
30 | return FALSE; |
---|
31 | } |
---|
32 | |
---|
33 | #else // Forced embedding, but no (development version of) python available! |
---|
34 | static BOOLEAN pyobject_load() { return TRUE; } |
---|
35 | #endif |
---|
36 | |
---|
37 | |
---|
38 | # else // Case: pyobject may be loaded from a dynamic module (prefered variant) |
---|
39 | // Note: we do not need python at compile time. |
---|
40 | static BOOLEAN pyobject_load() |
---|
41 | { |
---|
42 | return jjLOAD("pyobject.so", TRUE); |
---|
43 | } |
---|
44 | #endif |
---|
45 | |
---|
46 | |
---|
47 | |
---|
48 | /// blackbox support - initialization via autoloading |
---|
49 | void* pyobject_autoload(blackbox* bbx) |
---|
50 | { |
---|
51 | assume(bbx != NULL); |
---|
52 | return (pyobject_load()? NULL: bbx->blackbox_Init(bbx)); |
---|
53 | } |
---|
54 | |
---|
55 | void pyobject_default_destroy(blackbox *b, void *d) |
---|
56 | { |
---|
57 | Werror("Python-based functionality not available!"); |
---|
58 | } |
---|
59 | |
---|
60 | // Setting up an empty blackbox type, which can be filled with pyobject |
---|
61 | void pyobject_setup() |
---|
62 | { |
---|
63 | blackbox *bbx = (blackbox*)omAlloc0(sizeof(blackbox)); |
---|
64 | bbx->blackbox_Init = pyobject_autoload; |
---|
65 | bbx->blackbox_destroy = pyobject_default_destroy; |
---|
66 | setBlackboxStuff(bbx, "pyobject"); |
---|
67 | } |
---|
68 | |
---|
69 | /// Explicitely load, if not loaded already |
---|
70 | BOOLEAN pyobject_ensure() { |
---|
71 | |
---|
72 | int tok = -1; |
---|
73 | blackbox* bbx = (blackboxIsCmd("pyobject", tok) == ROOT_DECL? |
---|
74 | getBlackboxStuff(tok): (blackbox*)NULL); |
---|
75 | if (bbx == NULL) return TRUE; |
---|
76 | return (bbx->blackbox_Init == pyobject_autoload? pyobject_load(): FALSE); |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | |
---|