Bug #1509
python unicode strings raise exceptions for c++ methods that require std::string arguments
| Status: | New | Start date: | 07/09/2015 | |
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assigned To: | - | % Done: | 0% | |
| Category: | - | |||
| Target version: | - | |||
| Duration: |
Description
Using a python string seems to be ok, but a python unicode string doesn’t seem to get converted to a std::string object, see below examples:
>>> import gammalib
>>> g = gammalib.GModelSky()
>>> g.name( 'blah' ) # works fine
>>> g.name( u'blah' ) # raises exception
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/nkelhos/Software/gammalib-git/lib/python2.7/site-packages/gammalib/model.py", line 413, in name
return _model.GModel_name(self, *args)
NotImplementedError: Wrong number or type of arguments for overloaded function 'GModel_name'.
Possible C/C++ prototypes are:
GModel::name() const
GModel::name(std::string const &)
>>> import gammalib
>>> g = gammalib.GCTAObservation('VERITAS')
>>> g.load( 'ctoolsdata/VR75960.chunk2.fits' ) # works fine
>>> g.load( u'ctoolsdata/VR75960.chunk2.fits' ) # raises exception
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/afs/ifh.de/user/n/nkelhos/scratch/gammalib/gammalib-git/lib64/python2.6/site-packages/gammalib/cta.py", line 231, in load
return _cta.GCTAObservation_load(self, *args)
NotImplementedError: Wrong number of arguments for overloaded function 'GCTAObservation_load'.
Possible C/C++ prototypes are:
load(GCTAObservation *,std::string const &)
load(GCTAObservation *,std::string const &,std::string const &,std::string const &,std::string const &)
A workaround is to encode the unicode string back to ascii, via:
n = u'mystring'
nascii = n.encode('ascii')
This may or may not be a bug that requires a fix, but I thought I’d document the behavior anyway.
Recurrence
No recurrence.