[Trilinos-Users] PyTrilinos' Init_Argv doesn't account for unicode
Jonathan Guyer
guyer at nist.gov
Wed Oct 5 08:50:26 MDT 2011
I've encountered a problem when importing PyTrilinos from within an ipython session, more specifically when I %run a script in ipython that results in PyTrilinos being imported. E.g., given a file `importerr.py` containing
{{{
import PyTrilinos
}}}
if I `%run importerr.py` from within ipython 0.11, I get
{{{
TypeError: Init_Argv argument list contains non-string
}}}
(see traceback below [*])
The problem arises because ipython passes `importerr.py` as a unicode string and the `PyString_Check(item)` in TeuchosPYTHON_wrap.cpp fails because a unicode object is not a subclass of a string object.
Without this check `PyString_AsString(item)` would succeed because it accounts for casting from unicode.
The following patches resolve the issue for me in Trilinos 10.6.4 (I see the issue is still present in 10.8.1, although I haven't built it yet)
--- packages/PyTrilinos/src/Epetra_Comm.i.bu 2011-10-05 10:43:53.000000000 -0400
+++ packages/PyTrilinos/src/Epetra_Comm.i 2011-10-05 10:44:30.000000000 -0400
@@ -339,7 +339,7 @@
for (int i=0; i<argc; ++i)
{
PyObject * item = PySequence_GetItem(args, i);
- if (!PyString_Check(item))
+ if (!PyUnicode_Check(item) && !PyString_Check(item))
{
PyErr_SetString(PyExc_TypeError, "Init_Argv argument list contains non-string");
goto fail;
--- packages/PyTrilinos/src/Teuchos_Comm.i.bu 2011-10-05 10:44:09.000000000 -0400
+++ packages/PyTrilinos/src/Teuchos_Comm.i 2011-10-05 10:44:39.000000000 -0400
@@ -445,7 +445,7 @@
for (int i=0; i<argc; ++i)
{
PyObject * item = PySequence_GetItem(args, i);
- if (!PyString_Check(item))
+ if (!PyUnicode_Check(item) && !PyString_Check(item))
{
PyErr_SetString(PyExc_TypeError, "Init_Argv argument list contains non-string");
goto fail;
[*] Traceback:
{{{
In [1]: %run importerr.py
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/Users/guyer/.virtualenvs/virtualbrew/build/importerr.py in <module>()
----> 1 import PyTrilinos
/Users/guyer/.virtualenvs/virtualbrew/lib/python2.7/site-packages/PyTrilinos/__init__.py in <module>()
116 # PyTrilinos.NOX namespace completely before we need to use it.
117 if ('NOX' in __all__):
--> 118 import NOX
119 if (NOX.Have_Epetra):
120 import NOX.Epetra
/Users/guyer/.virtualenvs/virtualbrew/lib/python2.7/site-packages/PyTrilinos/NOX/__init__.py in <module>()
190 SwigPyIterator_swigregister(SwigPyIterator)
191
--> 192 import PyTrilinos.Teuchos
193 Have_Epetra = ___init__.Have_Epetra
194
/Users/guyer/.virtualenvs/virtualbrew/lib/python2.7/site-packages/PyTrilinos/Teuchos.py in <module>()
4569 # Call MPI_Init if appropriate
4570 import sys
-> 4571 Init_Argv(sys.argv)
4572 del sys
4573
/Users/guyer/.virtualenvs/virtualbrew/lib/python2.7/site-packages/PyTrilinos/Teuchos.py in Init_Argv(*args)
4200 def Init_Argv(*args):
4201 """Init_Argv(PyObject args) -> PyObject"""
-> 4202 return _Teuchos.Init_Argv(*args)
4203
4204 def Finalize(*args):
TypeError: Init_Argv argument list contains non-string
}}}
More information about the Trilinos-Users
mailing list