Skip to content

Commit 545f79e

Browse files
committed
More changes for #30 (improve gompy)
- added gompy.gom in "shortcuts" - "shortcuts" are added both in global module table and module's __getattr__
1 parent e065292 commit 545f79e

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

plugins/OGF/gompy/interpreter/python_interpreter.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,28 @@ namespace {
7575
// - We need this getattro override that returns something
7676
// when "__path__" is looked up, else Python runtime does not
7777
// see our module as a package (from which gompy.xxx can be imported).
78-
// - import gompy.xxx as yyy does not invoke get_attro(), it uses instead
79-
// the system-wide module dictionary, populated with what we want to be
80-
// able to import in PythonInterpreter::PythonInterpreter().
78+
// - import gompy.xxx as yyy does not invoke get_attro(), it uses
79+
// instead the system-wide module dictionary, populated with what we
80+
// want to be able to import in PythonInterpreter::PythonInterpreter().
81+
// (but it needs to be also handled here)
8182
std::string name = python_to_string(name_in);
8283
if(name == "__path__") {
8384
return string_to_python("");
8485
}
86+
if(name == "gom") {
87+
PyObject* py_gom = PyGraphiteObject_New(
88+
Interpreter::instance_by_language("Python")
89+
);
90+
Py_INCREF(py_gom);
91+
return py_gom;
92+
}
93+
if(name == "types") {
94+
PyObject* py_meta_types = PyGraphiteObject_New(
95+
Interpreter::instance_by_language("Python")->get_meta_types()
96+
);
97+
Py_INCREF(py_meta_types);
98+
return py_meta_types;
99+
}
85100
// Positioning this exception indicates that attribute lookup fallsback
86101
// to default behavior.
87102
PyErr_SetObject(PyExc_AttributeError, name_in);
@@ -165,19 +180,23 @@ namespace OGF {
165180

166181
if(!use_embedded_interpreter_) {
167182
// Shortcuts that one can import ... as ...
183+
// gompy.interpreter() -> gompy.gom
168184
// gompy.interpreter().meta_types -> gompy.types
169185
// gompy.interpreter().meta_types.OGF -> gompy.types.OGF
186+
// (they need also to be handled by module's __getattr__)
170187

171188
PyObject *sys_modules = PySys_GetObject("modules");
172189
Scope* meta_types = get_meta_types();
173190
Scope* OGF = nullptr;
174191
meta_types->resolve("OGF").get_value(OGF);
175192
geo_assert(OGF != nullptr);
176193

194+
PyObject* py_gom = PyGraphiteObject_New(this);
177195
PyObject* py_meta_types = PyGraphiteObject_New(meta_types);
178196
PyObject* py_OGF = PyGraphiteObject_New(OGF);
179197

180-
// Note: PyDict_SetItemString() increases refconut
198+
// Note: PyDict_SetItemString() increases refcount
199+
PyDict_SetItemString(sys_modules, "gompy.gom", py_gom);
181200
PyDict_SetItemString(sys_modules, "gompy.types", py_meta_types);
182201
PyDict_SetItemString(sys_modules, "gompy.types.OGF", py_OGF);
183202
return;

0 commit comments

Comments
 (0)