summaryrefslogtreecommitdiff
path: root/weave/conversion_code.py
diff options
context:
space:
mode:
authorEric Jones <eric@enthought.com>2002-01-17 03:45:05 +0000
committerEric Jones <eric@enthought.com>2002-01-17 03:45:05 +0000
commit1ed96f36cb3ecfceb682a9a96d3513ed5189a409 (patch)
tree54e36c9c32ff86bbb4fe95fa1f6d2e39cdbc3bb3 /weave/conversion_code.py
parent6c3253109e966cecb56a3132bc9bb1ec91889083 (diff)
downloadnumpy-1ed96f36cb3ecfceb682a9a96d3513ed5189a409.tar.gz
converted most conversion routines to be class methods so that exceptions won't croak on gcc.
Diffstat (limited to 'weave/conversion_code.py')
-rwxr-xr-xweave/conversion_code.py230
1 files changed, 125 insertions, 105 deletions
diff --git a/weave/conversion_code.py b/weave/conversion_code.py
index dbae86f54..656c352c2 100755
--- a/weave/conversion_code.py
+++ b/weave/conversion_code.py
@@ -78,25 +78,29 @@ void handle_conversion_error(PyObject* py_obj, char* good_type, char* var_name)
file_convert_code = \
"""
-FILE* convert_to_file(PyObject* py_obj, char* name)
+class file_handler
{
- if (!py_obj || !PyFile_Check(py_obj))
- handle_conversion_error(py_obj,"file", name);
-
- // Cleanup code should call DECREF
- Py_INCREF(py_obj);
- return PyFile_AsFile(py_obj);
-}
-
-FILE* py_to_file(PyObject* py_obj, char* name)
-{
- if (!py_obj || !PyFile_Check(py_obj))
- handle_bad_type(py_obj,"file", name);
-
- // Cleanup code should call DECREF
- Py_INCREF(py_obj);
- return PyFile_AsFile(py_obj);
-}
+public:
+ FILE* convert_to_file(PyObject* py_obj, char* name)
+ {
+ if (!py_obj || !PyFile_Check(py_obj))
+ handle_conversion_error(py_obj,"file", name);
+
+ // Cleanup code should call DECREF
+ Py_INCREF(py_obj);
+ return PyFile_AsFile(py_obj);
+ }
+
+ FILE* py_to_file(PyObject* py_obj, char* name)
+ {
+ if (!py_obj || !PyFile_Check(py_obj))
+ handle_bad_type(py_obj,"file", name);
+
+ // Cleanup code should call DECREF
+ Py_INCREF(py_obj);
+ return PyFile_AsFile(py_obj);
+ }
+};
PyObject* file_to_py(FILE* file, char* name, char* mode)
{
@@ -107,6 +111,7 @@ PyObject* file_to_py(FILE* file, char* name, char* mode)
"""
+
#############################################################
# Instance conversion code
#############################################################
@@ -114,27 +119,31 @@ PyObject* file_to_py(FILE* file, char* name, char* mode)
instance_convert_code = \
"""
-PyObject* convert_to_instance(PyObject* py_obj, char* name)
+class instance_handler
{
- if (!py_obj || !PyFile_Check(py_obj))
- handle_conversion_error(py_obj,"instance", name);
-
- // Should I INCREF???
- // Py_INCREF(py_obj);
- // just return the raw python pointer.
- return py_obj;
-}
-
-PyObject* py_to_instance(PyObject* py_obj, char* name)
-{
- if (!py_obj || !PyFile_Check(py_obj))
- handle_bad_type(py_obj,"instance", name);
-
- // Should I INCREF???
- // Py_INCREF(py_obj);
- // just return the raw python pointer.
- return py_obj;
-}
+public:
+ PyObject* convert_to_instance(PyObject* py_obj, char* name)
+ {
+ if (!py_obj || !PyFile_Check(py_obj))
+ handle_conversion_error(py_obj,"instance", name);
+
+ // Should I INCREF???
+ // Py_INCREF(py_obj);
+ // just return the raw python pointer.
+ return py_obj;
+ }
+
+ PyObject* py_to_instance(PyObject* py_obj, char* name)
+ {
+ if (!py_obj || !PyFile_Check(py_obj))
+ handle_bad_type(py_obj,"instance", name);
+
+ // Should I INCREF???
+ // Py_INCREF(py_obj);
+ // just return the raw python pointer.
+ return py_obj;
+ }
+};
PyObject* instance_to_py(PyObject* instance)
{
@@ -151,27 +160,31 @@ PyObject* instance_to_py(PyObject* instance)
callable_convert_code = \
"""
-PyObject* convert_to_callable(PyObject* py_obj, char* name)
-{
- if (!py_obj || !PyCallable_Check(py_obj))
- handle_conversion_error(py_obj,"callable", name);
-
- // Should I INCREF???
- // Py_INCREF(py_obj);
- // just return the raw python pointer.
- return py_obj;
-}
-
-PyObject* py_to_callable(PyObject* py_obj, char* name)
+class callable_handler
{
- if (!py_obj || !PyCallable_Check(py_obj))
- handle_bad_type(py_obj,"callable", name);
-
- // Should I INCREF???
- // Py_INCREF(py_obj);
- // just return the raw python pointer.
- return py_obj;
-}
+public:
+ PyObject* convert_to_callable(PyObject* py_obj, char* name)
+ {
+ if (!py_obj || !PyCallable_Check(py_obj))
+ handle_conversion_error(py_obj,"callable", name);
+
+ // Should I INCREF???
+ // Py_INCREF(py_obj);
+ // just return the raw python pointer.
+ return py_obj;
+ }
+
+ PyObject* py_to_callable(PyObject* py_obj, char* name)
+ {
+ if (!py_obj || !PyCallable_Check(py_obj))
+ handle_bad_type(py_obj,"callable", name);
+
+ // Should I INCREF???
+ // Py_INCREF(py_obj);
+ // just return the raw python pointer.
+ return py_obj;
+ }
+};
PyObject* callable_to_py(PyObject* callable)
{
@@ -187,27 +200,31 @@ PyObject* callable_to_py(PyObject* callable)
module_convert_code = \
"""
-PyObject* convert_to_module(PyObject* py_obj, char* name)
-{
- if (!py_obj || !PyModule_Check(py_obj))
- handle_conversion_error(py_obj,"module", name);
-
- // Should I INCREF???
- // Py_INCREF(py_obj);
- // just return the raw python pointer.
- return py_obj;
-}
-
-PyObject* py_to_module(PyObject* py_obj, char* name)
+class module_handler
{
- if (!py_obj || !PyModule_Check(py_obj))
- handle_bad_type(py_obj,"module", name);
-
- // Should I INCREF???
- // Py_INCREF(py_obj);
- // just return the raw python pointer.
- return py_obj;
-}
+public:
+ PyObject* convert_to_module(PyObject* py_obj, char* name)
+ {
+ if (!py_obj || !PyModule_Check(py_obj))
+ handle_conversion_error(py_obj,"module", name);
+
+ // Should I INCREF???
+ // Py_INCREF(py_obj);
+ // just return the raw python pointer.
+ return py_obj;
+ }
+
+ PyObject* py_to_module(PyObject* py_obj, char* name)
+ {
+ if (!py_obj || !PyModule_Check(py_obj))
+ handle_bad_type(py_obj,"module", name);
+
+ // Should I INCREF???
+ // Py_INCREF(py_obj);
+ // just return the raw python pointer.
+ return py_obj;
+ }
+};
PyObject* module_to_py(PyObject* module)
{
@@ -349,35 +366,38 @@ non_template_scalar_support_code = \
// Conversion Errors
-static int convert_to_int(PyObject* py_obj,char* name)
-{
- if (!py_obj || !PyInt_Check(py_obj))
- handle_conversion_error(py_obj,"int", name);
- return (int) PyInt_AsLong(py_obj);
-}
-
-static long convert_to_long(PyObject* py_obj,char* name)
-{
- if (!py_obj || !PyLong_Check(py_obj))
- handle_conversion_error(py_obj,"long", name);
- return (long) PyLong_AsLong(py_obj);
-}
-
-static double convert_to_float(PyObject* py_obj,char* name)
-{
- if (!py_obj || !PyFloat_Check(py_obj))
- handle_conversion_error(py_obj,"float", name);
- return PyFloat_AsDouble(py_obj);
-}
+class scalar_handler
+{
+public:
+ int convert_to_int(PyObject* py_obj,char* name)
+ {
+ if (!py_obj || !PyInt_Check(py_obj))
+ handle_conversion_error(py_obj,"int", name);
+ return (int) PyInt_AsLong(py_obj);
+ }
+ long convert_to_long(PyObject* py_obj,char* name)
+ {
+ if (!py_obj || !PyLong_Check(py_obj))
+ handle_conversion_error(py_obj,"long", name);
+ return (long) PyLong_AsLong(py_obj);
+ }
+
+ double convert_to_float(PyObject* py_obj,char* name)
+ {
+ if (!py_obj || !PyFloat_Check(py_obj))
+ handle_conversion_error(py_obj,"float", name);
+ return PyFloat_AsDouble(py_obj);
+ }
// complex not checked.
-static std::complex<double> convert_to_complex(PyObject* py_obj,char* name)
-{
- if (!py_obj || !PyComplex_Check(py_obj))
- handle_conversion_error(py_obj,"complex", name);
- return std::complex<double>(PyComplex_RealAsDouble(py_obj),
- PyComplex_ImagAsDouble(py_obj));
-}
+ std::complex<double> convert_to_complex(PyObject* py_obj,char* name)
+ {
+ if (!py_obj || !PyComplex_Check(py_obj))
+ handle_conversion_error(py_obj,"complex", name);
+ return std::complex<double>(PyComplex_RealAsDouble(py_obj),
+ PyComplex_ImagAsDouble(py_obj));
+ }
+};
/////////////////////////////////////
// The following functions are used for scalar conversions in msvc