What about this?
Tested in python 2.7 and 3.3

a = array.array("B")
s=u"abcꬦ"
try:
    a.fromstring(s.encode('ascii'))
except UnicodeEncodeError:
    a.fromstring(s.encode('utf8'))
print(a)
array('B', [97, 98, 99, 234, 172, 166])

a = array.array("B")
s="abc"
try:
    a.fromstring(s.encode('ascii'))
except UnicodeEncodeError:
    a.fromstring(s.encode('utf8'))


On Jan 26, 2013, at 11:04 AM, wander.lair...@gmail.com wrote:

> From: Wander Lairson Costa <wander.lair...@gmail.com>
> 
> In usb/_interop.py in the as_array function, the array typecodes 'c' and
> 'u' are used. The 'c' typecode was removed from python 3.3 and 'u' will
> is deprecated. The solution is to use the fromstring array method, but
> it has the side effect of adding only the first byte of Unicode strings.
> ---
> usb/_interop.py |    7 +++----
> 1 file changed, 3 insertions(+), 4 deletions(-)
> 
> diff --git a/usb/_interop.py b/usb/_interop.py
> index 5abdcdb..d6d0a6c 100644
> --- a/usb/_interop.py
> +++ b/usb/_interop.py
> @@ -131,8 +131,7 @@ def as_array(data=None):
>     except TypeError:
>         # When you pass a unicode string or a character sequence,
>         # you get a TypeError if first parameter does not match
> -        try:
> -            return array.array('c', data)
> -        except TypeError:
> -            return array.array('u', data)
> +        a = array.array('B')
> +        a.fromstring(data)
> +        return a
> 
> -- 
> 1.7.10.4
> 
> 
> ------------------------------------------------------------------------------
> Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
> MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
> with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
> MVPs and experts. ON SALE this month only -- learn more at:
> http://p.sf.net/sfu/learnnow-d2d
> _______________________________________________
> pyusb-users mailing list
> pyusb-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/pyusb-users


------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnnow-d2d
_______________________________________________
pyusb-users mailing list
pyusb-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pyusb-users

Reply via email to