Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert PySerial to python 3.0

9 views
Skip to first unread message

Seth

unread,
Feb 24, 2009, 10:46:19 PM2/24/09
to
I am just messing around trying to get pyserial to work with 3.0.

I am stuck on this line:

if type(port) in [type(''), type(u'')]


how can I convert this to 3.0? I tried changing the u to a d that did
not do anything.

Thanks

Chris Rebert

unread,
Feb 24, 2009, 10:55:44 PM2/24/09
to Seth, pytho...@python.org

Looks like it's doing the equivalent of the pre-3.0-ism
`isinstance(port, basestring)`, but in a roundabout way.
However, since basestring doesn't exist in 3.0, either:

if isinstance(port, str):

Or

if isinstance(port, bytes):

would be the appropriate replacement. Depends (obviously) on whether
'port' is supposed to be unicode or a byte sequence.
Without more context, I can't really say which is what you want.

Cheers,
Chris

--
Follow the path of the Iguana...
http://rebertia.com

Steve Holden

unread,
Feb 24, 2009, 11:00:56 PM2/24/09
to pytho...@python.org
How about

if type(port) in (str, bytes):

Untested.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Grant Edwards

unread,
Feb 24, 2009, 11:22:34 PM2/24/09
to
On 2009-02-25, Chris Rebert <cl...@rebertia.com> wrote:
> On Tue, Feb 24, 2009 at 7:46 PM, Seth <king...@gmail.com> wrote:
>> I am just messing around trying to get pyserial to work with 3.0.
>>
>> I am stuck on this line:
>>
>> if type(port) in [type(''), type(u'')]
>>
>> how can I convert this to 3.0? I tried changing the u to a d that did
>> not do anything.
>
> Looks like it's doing the equivalent of the pre-3.0-ism
> `isinstance(port, basestring)`, but in a roundabout way.
> However, since basestring doesn't exist in 3.0, either:
>
> if isinstance(port, str):
>
> Or
>
> if isinstance(port, bytes):
>
> would be the appropriate replacement. Depends (obviously) on
> whether 'port' is supposed to be unicode or a byte sequence.

Port can either be an integer or a file/device name.

> Without more context, I can't really say which is what you
> want.

In the line of code in question, the conditional is inteded to
be true if "port" is someting that could be passed to the OS as
a file or device name.

--
Grant


Seth

unread,
Feb 25, 2009, 7:37:19 AM2/25/09
to
On Feb 24, 10:55 pm, Chris Rebert <c...@rebertia.com> wrote:

I implemented "if isinstance(port, str): " that seems to work for now.

Currently I am running into:

err, n = win32file.WriteFile(self.hComPort, data,
self._overlappedWrite)
TypeError: expected an object with a buffer interface

Thanks

Christian Heimes

unread,
Feb 25, 2009, 8:47:03 AM2/25/09
to pytho...@python.org
Seth wrote:
> I implemented "if isinstance(port, str): " that seems to work for now.
>
> Currently I am running into:
>
> err, n = win32file.WriteFile(self.hComPort, data,
> self._overlappedWrite)
> TypeError: expected an object with a buffer interface

Unicode objects (in Py3k: str) don't implement the buffer interface. You
have to apply a bytes or bytearray instance.

Christian

Seth

unread,
Feb 25, 2009, 10:07:51 AM2/25/09
to
I tried all three ways you guys listed nothing seems to convert the
string to bytes.

It may have to do with the makeDeviceName function, but I can't find
where that is defined.

Any thoughts??

Here is the whole block of code:

if type(port) in (str, bytes): #strings are taken directly
Originally: if type(port) in [type(''), type(u'')]
self.portstr = port
else:
self.portstr = self.makeDeviceName(port)

Christian Heimes

unread,
Feb 25, 2009, 10:16:11 AM2/25/09
to pytho...@python.org
Seth wrote:
> I tried all three ways you guys listed nothing seems to convert the
> string to bytes.
>
> It may have to do with the makeDeviceName function, but I can't find
> where that is defined.
>
> Any thoughts??
>
> Here is the whole block of code:
>
> if type(port) in (str, bytes): #strings are taken directly
> Originally: if type(port) in [type(''), type(u'')]
> self.portstr = port
> else:
> self.portstr = self.makeDeviceName(port)

str and bytes are two totally unrelated things in Python 3.0. You can no
longer treat them equally like str and unicode in Python 2.x. Your could
should not work under Python 3.0 anyway. u'' is invalid in 3.x.

Also please don't use ugly type checks like type(something) == type('').
Starting with Python 2.2 you should use isinstance, for example
isinstance(number, (int, long)).

Christian

Seth

unread,
Feb 25, 2009, 11:07:30 AM2/25/09
to
This is not my code and I am fairly new to Python. I did not know how
much it would take to convert pyserial to 3.0. Someone more
knowledgeable than me could do it better and faster. I just want to
see if I could help get it to work.

I was wrong, it seems that if type(port) in (str, bytes): or isinstance
(port, str) works just fine for setting the ports.

The issue now is reading and writing the data.

I got the read() to kind of work with:
bytes(buf[:n]) replacing str(buf[:n])

but with readline() it seems to be missing the '\n' because it just
runs indefinitely( I can see the \n if I read enough bytes with read()


write seems to be related to a win32 issue. win32file.WriteFile does
not like anything I convert it to:
str gives the buffer error
bytes gives a "string argument without an encoding" error

Sorry for the confusion, I just want to be able to use all Py3k on
this project that I am working on and pyserial has not been converted
so I just started messing around with it.

Thanks for the help.

Seth

Gabriel Genellina

unread,
Feb 25, 2009, 11:23:32 AM2/25/09
to pytho...@python.org
En Wed, 25 Feb 2009 14:07:30 -0200, Seth <king...@gmail.com> escribió:

> This is not my code and I am fairly new to Python. I did not know how
> much it would take to convert pyserial to 3.0. Someone more
> knowledgeable than me could do it better and faster. I just want to
> see if I could help get it to work.

The last commit to pyserial repository was 8 days ago, so it's not like an
abandoned project.
Have you contacted the author?

That said, Python 3.0 is still very recent and doesn't have a big set of
3rd party libraries as earlier versions. Maybe you should stick to 2.6 or
even 2.5 for a while.

--
Gabriel Genellina

0 new messages