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
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
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/
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
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
Unicode objects (in Py3k: str) don't implement the buffer interface. You
have to apply a bytes or bytearray instance.
Christian
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
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
> 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