[Trilinos-Users] PyTrilinos' Init_Argv doesn't account for unicode

Bill Spotz wfspotz at sandia.gov
Wed Oct 5 10:20:58 MDT 2011


Jonathan,

Thanks for the heads up.  I did a quick test, and it appears that you can do a

    if (!PyUnicode_Check(item))

instead of

    if (!PyUnicode_Check(item) && !PyString_Check(item))

because the string type does inherit from the unicode type.  I will implement this change in the development branch and it will propagate to a future release.

-Bill

On Oct 5, 2011, at 8:50 AM, Jonathan Guyer wrote:

> 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
> }}}
> 
> 
> 
> _______________________________________________
> Trilinos-Users mailing list
> Trilinos-Users at software.sandia.gov
> http://software.sandia.gov/mailman/listinfo/trilinos-users

** Bill Spotz                                              **
** Sandia National Laboratories  Voice: (505)845-0170      **
** P.O. Box 5800                 Fax:   (505)284-0154      **
** Albuquerque, NM 87185-0370    Email: wfspotz at sandia.gov **




More information about the Trilinos-Users mailing list