Mercurial > pytave
comparison __py_struct_from_dict__.cc @ 405:478d83448b0b
Refactor Python initialization into a common function
* oct-py-init.cc, oct-py-init.h: New files defining a pytave::py_init function.
* __py_struct_from_dict__.cc, pycall.cc, pyeval.cc, pyexec.cc: Use it instead
of initializing Python differently in each entry point.
* Makefile.am (COMMON_SOURCE_FILES): Include oct-py-init.cc in the list.
(PYTAVE_HEADER_FILES): Include oct-py-init.h in the list.
author | Mike Miller <mtmiller@octave.org> |
---|---|
date | Sat, 29 Apr 2017 15:43:42 -0700 |
parents | aef165ff92b0 |
children | f833e29b2c12 |
comparison
equal
deleted
inserted
replaced
404:aef165ff92b0 | 405:478d83448b0b |
---|---|
25 #endif | 25 #endif |
26 | 26 |
27 #include <Python.h> | 27 #include <Python.h> |
28 #include <octave/oct.h> | 28 #include <octave/oct.h> |
29 | 29 |
30 #define PYTAVE_DO_DECLARE_SYMBOL | |
31 #include "arrayobjectdefs.h" | |
32 #include "exceptions.h" | 30 #include "exceptions.h" |
31 #include "oct-py-init.h" | |
33 #include "oct-py-object.h" | 32 #include "oct-py-object.h" |
34 #include "oct-py-types.h" | 33 #include "oct-py-types.h" |
35 #include "oct-py-util.h" | 34 #include "oct-py-util.h" |
36 #include "octave_to_python.h" | 35 #include "octave_to_python.h" |
37 | 36 |
47 print_usage (); | 46 print_usage (); |
48 | 47 |
49 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) | 48 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) |
50 error ("__py_class_name__: argument must be a valid Python object"); | 49 error ("__py_class_name__: argument must be a valid Python object"); |
51 | 50 |
52 Py_Initialize (); | 51 pytave::py_init (); |
53 | 52 |
54 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0)); | 53 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0)); |
55 std::string name = pytave::py_object_class_name (obj); | 54 std::string name = pytave::py_object_class_name (obj); |
56 | 55 |
57 return ovl (name); | 56 return ovl (name); |
85 print_usage (); | 84 print_usage (); |
86 | 85 |
87 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) | 86 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) |
88 error ("pyobject.int64: argument must be a Python object"); | 87 error ("pyobject.int64: argument must be a Python object"); |
89 | 88 |
90 Py_Initialize (); | 89 pytave::py_init (); |
91 | 90 |
92 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0)); | 91 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0)); |
93 if (! obj) | 92 if (! obj) |
94 error ("pyobject.int64: argument must be a valid Python object"); | 93 error ("pyobject.int64: argument must be a valid Python object"); |
95 | 94 |
137 print_usage (); | 136 print_usage (); |
138 | 137 |
139 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) | 138 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) |
140 error ("pyobject.uint64: argument must be a Python object"); | 139 error ("pyobject.uint64: argument must be a Python object"); |
141 | 140 |
142 Py_Initialize (); | 141 pytave::py_init (); |
143 | 142 |
144 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0)); | 143 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0)); |
145 if (! obj) | 144 if (! obj) |
146 error ("pyobject.uint64: argument must be a valid Python object"); | 145 error ("pyobject.uint64: argument must be a valid Python object"); |
147 | 146 |
185 @end deftypefn") | 184 @end deftypefn") |
186 { | 185 { |
187 if (args.length () != 1) | 186 if (args.length () != 1) |
188 print_usage (); | 187 print_usage (); |
189 | 188 |
190 Py_Initialize (); | 189 pytave::py_init (); |
191 | 190 |
192 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0)); | 191 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0)); |
193 | 192 |
194 return ovl (obj.is_none ()); | 193 return ovl (obj.is_none ()); |
195 } | 194 } |
234 if (! ((typestr.size () > 3) && (typestr.compare (0, 3, "py.") == 0))) | 233 if (! ((typestr.size () > 3) && (typestr.compare (0, 3, "py.") == 0))) |
235 error ("pyobject.isa: TYPE must be a string naming a Python type (py.*)"); | 234 error ("pyobject.isa: TYPE must be a string naming a Python type (py.*)"); |
236 | 235 |
237 typestr = typestr.substr (3); | 236 typestr = typestr.substr (3); |
238 | 237 |
239 Py_Initialize (); | 238 pytave::py_init (); |
240 | 239 |
241 try | 240 try |
242 { | 241 { |
243 // FIXME: PyObject *obj = look up stored pyobject reference (args(0)); | 242 // FIXME: PyObject *obj = look up stored pyobject reference (args(0)); |
244 boost::python::object arg; | 243 boost::python::object arg; |
270 @end deftypefn") | 269 @end deftypefn") |
271 { | 270 { |
272 if (args.length () != 1) | 271 if (args.length () != 1) |
273 print_usage (); | 272 print_usage (); |
274 | 273 |
275 Py_Initialize (); | 274 pytave::py_init (); |
275 | |
276 uint64_t key = args(0).xuint64_scalar_value ("__py_objstore_del__: KEY must be an integer"); | 276 uint64_t key = args(0).xuint64_scalar_value ("__py_objstore_del__: KEY must be an integer"); |
277 pytave::py_objstore_del (key); | 277 pytave::py_objstore_del (key); |
278 | 278 |
279 return ovl (); | 279 return ovl (); |
280 } | 280 } |
288 @end deftypefn") | 288 @end deftypefn") |
289 { | 289 { |
290 if (args.length () != 1) | 290 if (args.length () != 1) |
291 print_usage (); | 291 print_usage (); |
292 | 292 |
293 Py_Initialize (); | 293 pytave::py_init (); |
294 | |
294 uint64_t key = args(0).xuint64_scalar_value ("__py_objstore_get__: KEY must be an integer"); | 295 uint64_t key = args(0).xuint64_scalar_value ("__py_objstore_get__: KEY must be an integer"); |
295 PyObject *obj = pytave::py_objstore_get (key); | 296 PyObject *obj = pytave::py_objstore_get (key); |
296 | 297 |
297 if (! obj) | 298 if (! obj) |
298 error ("__py_objstore_get__: no existing Python object found for key %ju", key); | 299 error ("__py_objstore_get__: no existing Python object found for key %ju", key); |
311 @end deftypefn") | 312 @end deftypefn") |
312 { | 313 { |
313 if (args.length () != 1) | 314 if (args.length () != 1) |
314 print_usage (); | 315 print_usage (); |
315 | 316 |
316 Py_Initialize (); | 317 pytave::py_init (); |
317 boost::python::numeric::array::set_module_and_type ("numpy", "ndarray"); | 318 |
318 _import_array (); | |
319 // FIXME: PyObject *obj = convert argument to Python (args(0)); | 319 // FIXME: PyObject *obj = convert argument to Python (args(0)); |
320 PyObject *obj = nullptr; | 320 PyObject *obj = nullptr; |
321 try | 321 try |
322 { | 322 { |
323 boost::python::object arg; | 323 boost::python::object arg; |
350 print_usage (); | 350 print_usage (); |
351 | 351 |
352 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) | 352 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) |
353 error ("pyobject.char: argument must be a valid Python object"); | 353 error ("pyobject.char: argument must be a valid Python object"); |
354 | 354 |
355 Py_Initialize (); | 355 pytave::py_init (); |
356 | 356 |
357 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0)); | 357 pytave::python_object obj = pytave::pyobject_unwrap_object (args(0)); |
358 if (! obj) | 358 if (! obj) |
359 error ("pyobject.char: argument must be a valid Python object"); | 359 error ("pyobject.char: argument must be a valid Python object"); |
360 | 360 |
393 } | 393 } |
394 | 394 |
395 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) | 395 if (! (args(0).is_object () && args(0).class_name () == "pyobject")) |
396 error ("pyobject.struct: argument must be a Python object"); | 396 error ("pyobject.struct: argument must be a Python object"); |
397 | 397 |
398 Py_Initialize (); | 398 pytave::py_init (); |
399 | 399 |
400 try | 400 try |
401 { | 401 { |
402 // FIXME: PyObject *obj = look up stored pyobject reference (args(0)); | 402 // FIXME: PyObject *obj = look up stored pyobject reference (args(0)); |
403 boost::python::object arg; | 403 boost::python::object arg; |