pcp
[Top] [All Lists]

Re: [pcp] JSON PMDA

To: David Smith <dsmith@xxxxxxxxxx>
Subject: Re: [pcp] JSON PMDA
From: Nathan Scott <nathans@xxxxxxxxxx>
Date: Sun, 12 Apr 2015 22:23:06 -0400 (EDT)
Cc: pcp <pcp@xxxxxxxxxxx>
Delivered-to: pcp@xxxxxxxxxxx
In-reply-to: <5527DA01.50301@xxxxxxxxxx>
References: <54F9F92D.4010202@xxxxxxxxxx> <448002717.7934024.1427683964254.JavaMail.zimbra@xxxxxxxxxx> <552699FE.7040801@xxxxxxxxxx> <2139482617.15593599.1428634701360.JavaMail.zimbra@xxxxxxxxxx> <2037935605.15616473.1428642606665.JavaMail.zimbra@xxxxxxxxxx> <5527DA01.50301@xxxxxxxxxx>
Reply-to: Nathan Scott <nathans@xxxxxxxxxx>
Thread-index: QTyXPXkh6xY/X5LaiecHJxJpRzbyxA==
Thread-topic: JSON PMDA

----- Original Message -----
> [...]
> > $ python
> > Python 2.7.5 (default, Nov  3 2014, 14:26:24)
> > [GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux2
> > Type "help", "copyright", "credits" or "license" for more information.
> >>>> from pcp import pmapi
> >>>> import cpmapi as c_api
> >>>> context = pmapi.pmContext()
> >>>> context.pmParseUnitsStr("count")
> > (<pcp.pmapi.pmUnits object at 0x7ff718e7c710>, 1.0)
> >>>> ^D
> > 
> > $ python3
> > Python 3.3.2 (default, Dec  4 2014, 12:49:00)
> > [GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux
> > Type "help", "copyright", "credits" or "license" for more information.
> >>>> from pcp import pmapi
> >>>> import cpmapi as c_api
> >>>> context = pmapi.pmContext()
> >>>> context.pmParseUnitsStr("count")
> > (<pcp.pmapi.pmUnits object at 0x7f9e3952fb00>, 1.0)
> >>>> ^D
> > 
> > 
> > I guess one of those fails with an exception for you locally?
> 
> Your example works fine:
> 
> >>> from pcp import pmapi
> >>> context = pmapi.pmContext()
> >>> context.pmParseUnitsStr("count")
> (<pcp.pmapi.pmUnits object at 0x7f5a105a1710>, 1.0)
> 
> But the following fails:
> 
> >>> context.pmParseUnitsStr(u'count')
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/usr/lib64/python2.7/site-packages/pcp/pmapi.py", line 1927, in
> pmParseUnitsStr
>     raise pmErr(c_api.PM_ERR_CONV, str(string))
> pcp.pmapi.pmErr: PM_ERR_CONV Impossible value or scale conversion count
> 
> You weren't passing in a unicode string.


In python3, strings are unicode.  So, I guess you are using Unicode strings
on python2?  (explicitly?)

$ python3
Python 3.3.2 (default, Dec  4 2014, 12:49:00) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> type('x')
<class 'str'>
>>> type(u'x')
<class 'str'>
>>> type(b'x')
<class 'bytes'>
>>> 
$ python
Python 2.7.5 (default, Nov  3 2014, 14:26:24) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> type('x')
<type 'str'>
>>> type(u'x')
<type 'unicode'>
>>> type(b'x')
<type 'str'>
>>> 


Anyway, we should certainly tackle this below the API; we'll need to audit
all the byte/unicode conversion points and make sure they explicitly check
for unicode, e.g. this:

    @staticmethod
    def pmParseUnitsStr(string):
        if type(string) != type('') and type(string) != type(b''):
            raise pmErr(c_api.PM_ERR_CONV, str(string))
        if type(string) != type(b''):
            string = string.encode('utf-8')

should become

    @staticmethod
    def pmParseUnitsStr(string):
        if type(string) != type(u'') and type(string) != type(b''):
            raise pmErr(c_api.PM_ERR_CONV, str(string))
        if type(string) != type(b''):
            string = string.encode('utf-8')


and the exception you're seeing will be avoided I think.  Thanks David!

cheers.

--
Nathan

<Prev in Thread] Current Thread [Next in Thread>