Skip to content

Commit 3b658d1

Browse files
authored
Merge pull request #59 from DiamondLightSource/remove-sys-version
Remove all references to sys.version_info and associated fixups
2 parents f498c71 + 9b0e749 commit 3b658d1

5 files changed

Lines changed: 30 additions & 97 deletions

File tree

src/cothread/catools.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,6 @@ def _check_env(name, default):
8383
CA_ACTION_STACK = _check_env('CATOOLS_ACTION_STACK', 0)
8484

8585

86-
if sys.version_info < (3,):
87-
pv_string_types = (str, unicode)
88-
else:
89-
pv_string_types = str
90-
91-
9286
class ca_nothing(Exception):
9387
'''This value is returned as a success or failure indicator from caput,
9488
as a failure indicator from caget, and may be raised as an exception to
@@ -567,7 +561,7 @@ def camonitor(pvs, callback, **kargs):
567561
if notify_disconnect is False, and that if the PV subsequently connects
568562
it will update as normal.
569563
'''
570-
if isinstance(pvs, pv_string_types):
564+
if isinstance(pvs, str):
571565
return _Subscription(pvs, callback, **kargs)
572566
else:
573567
return [
@@ -754,7 +748,7 @@ def caget(pvs, **kargs):
754748
The format of values returned depends on the number of values requested
755749
for each PV. If only one value is requested then the value is returned
756750
as a scalar, otherwise as a numpy array.'''
757-
if isinstance(pvs, pv_string_types):
751+
if isinstance(pvs, str):
758752
return caget_one(pvs, **kargs)
759753
else:
760754
return caget_array(pvs, **kargs)
@@ -827,7 +821,7 @@ def caput_one(pv, value, datatype=None, wait=False, timeout=5, callback=None):
827821

828822
def caput_array(pvs, values, repeat_value=False, **kargs):
829823
# Bring the arrays of pvs and values into alignment.
830-
if repeat_value or isinstance(values, pv_string_types):
824+
if repeat_value or isinstance(values, str):
831825
# If repeat_value is requested or the value is a string then we treat
832826
# it as a single value.
833827
values = [values] * len(pvs)
@@ -898,7 +892,7 @@ def caput(pvs, values, **kargs):
898892
If caput completed succesfully then .ok is True and .name is the
899893
corresponding PV name. If throw=False was specified and a put failed
900894
then .errorcode is set to the appropriate ECA_ error code.'''
901-
if isinstance(pvs, pv_string_types):
895+
if isinstance(pvs, str):
902896
return caput_one(pvs, values, **kargs)
903897
else:
904898
return caput_array(pvs, values, **kargs)
@@ -1003,7 +997,7 @@ def connect(pvs, **kargs):
1003997
connected to. If this is set to False then instead for each failing
1004998
PV a sentinel value with .ok == False is returned.
1005999
'''
1006-
if isinstance(pvs, pv_string_types):
1000+
if isinstance(pvs, str):
10071001
return connect_one(pvs, **kargs)
10081002
else:
10091003
return connect_array(pvs, **kargs)

src/cothread/coserver.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,8 @@
2424

2525
import sys
2626

27-
if sys.version_info < (3,):
28-
from SocketServer import BaseServer, TCPServer, UDPServer, ThreadingMixIn
29-
from BaseHTTPServer import HTTPServer, test as _test
30-
from SimpleHTTPServer import SimpleHTTPRequestHandler
31-
32-
else:
33-
from socketserver import BaseServer, TCPServer, UDPServer, ThreadingMixIn
34-
from http.server import HTTPServer, SimpleHTTPRequestHandler, test as _test
27+
from socketserver import BaseServer, TCPServer, UDPServer, ThreadingMixIn
28+
from http.server import HTTPServer, SimpleHTTPRequestHandler, test as _test
3529

3630
from . import cothread
3731
from . import cosocket

src/cothread/cosocket.py

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -186,32 +186,17 @@ def sendall(self, data, *flags):
186186
def dup(self):
187187
return cosocket(_sock=self.__socket.dup())
188188

189-
if sys.version_info < (3,):
190-
@wrap
191-
def makefile(self, *args, **kws):
192-
# At this point the actual socket '_socket.socket' is wrapped by
193-
# either two layers: 'socket.socket' and this class. or a single
194-
# layer: this class. In order to handle close() properly we must
195-
# copy all wrappers, but not the underlying actual socket.
196-
sock = getattr(self.__socket, '_sock', None)
197-
if sock: # double wrapped
198-
copy0 = _socket_socket(None, None, None, sock)
199-
copy1 = cosocket(None, None, None, copy0)
200-
else: # single wrapped
201-
copy1 = cosocket(None, None, None, self.__socket)
202-
return _socket._fileobject(copy1, *args, **kws)
203-
else:
204-
@property
205-
def _io_refs(self):
206-
return self.__socket._io_refs
207-
208-
@_io_refs.setter
209-
def _io_refs(self, value):
210-
self.__socket._io_refs = value
211-
212-
# Can use the original makefile just so long as we provide the _io_refs
213-
# property above.
214-
makefile = _socket_socket.makefile
189+
@property
190+
def _io_refs(self):
191+
return self.__socket._io_refs
192+
193+
@_io_refs.setter
194+
def _io_refs(self, value):
195+
self.__socket._io_refs = value
196+
197+
# Can use the original makefile just so long as we provide the _io_refs
198+
# property above.
199+
makefile = _socket_socket.makefile
215200

216201

217202
del wrap

src/cothread/cothread.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import traceback
7474
import collections
7575
import threading
76+
import _thread
7677

7778
from . import _coroutine
7879

@@ -81,11 +82,6 @@
8182

8283
from . import coselect
8384

84-
if sys.version_info >= (3,):
85-
import _thread
86-
else:
87-
import thread as _thread
88-
8985

9086
__all__ = [
9187
'Spawn', # Spawn new task

src/cothread/dbr.py

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
'DBR_DOUBLE', # 64 bit float
5252

5353
'DBR_CHAR_STR', # Long strings as char arrays
54-
'DBR_CHAR_UNICODE', # Long unicode strings as char arrays
5554
'DBR_ENUM_STR', # Enums as strings, default otherwise
5655
'DBR_CHAR_BYTES', # Long byte strings as char arrays
5756

@@ -163,29 +162,11 @@ class ca_str(str):
163162
def __pos__(self):
164163
return str(self)
165164

166-
167-
# Overlapping handling for python 2 and python 3. We have three types with two
168-
# different semantics: str, bytes, unicode. In python2 str is bytes, while in
169-
# python3 str is unicode. We walk a delicate balancing act to get the right
170-
# behaviour in both environments!
171-
if sys.version_info < (3,):
172-
ca_bytes = ca_str
173-
class ca_unicode(bytes):
174-
__doc__ = ca_doc_string
175-
datetime = timestamp_to_datetime
176-
def __pos__(self):
177-
return unicode(self)
178-
str_char_code = 'S'
179-
else:
180-
class ca_bytes(bytes):
181-
__doc__ = ca_doc_string
182-
datetime = timestamp_to_datetime
183-
def __pos__(self):
184-
return bytes(self)
185-
ca_unicode = ca_str
186-
str_char_code = 'U'
187-
unicode = str
188-
165+
class ca_bytes(bytes):
166+
__doc__ = ca_doc_string
167+
datetime = timestamp_to_datetime
168+
def __pos__(self):
169+
return bytes(self)
189170

190171
class ca_int(int):
191172
__doc__ = ca_doc_string
@@ -544,7 +525,6 @@ def copy_attributes(self, other):
544525
# Special value for DBR_CHAR as str special processing.
545526
DBR_ENUM_STR = 996
546527
DBR_CHAR_BYTES = 997
547-
DBR_CHAR_UNICODE = 998
548528
DBR_CHAR_STR = 999
549529

550530

@@ -604,7 +584,7 @@ def copy_attributes(self, other):
604584
'I': DBR_LONG, # uintc = uint32
605585

606586
# Unicode is supported by decoding from DBR_STRING
607-
'U': DBR_STRING, # str => unicode
587+
'U': DBR_STRING, # str
608588

609589
# We translate machine native integers to DBR_LONG as EPICS has no support
610590
# for 64-bit integers, but not allowing int as an argument is too confusing.
@@ -652,7 +632,7 @@ def _type_to_dbrcode(datatype, format):
652632
- FORMAT_CTRL: retrieve limit and control data
653633
'''
654634
if datatype not in BasicDbrTypes:
655-
if datatype in [DBR_CHAR_STR, DBR_CHAR_BYTES, DBR_CHAR_UNICODE]:
635+
if datatype in [DBR_CHAR_STR, DBR_CHAR_BYTES]:
656636
datatype = DBR_CHAR # Retrieve this type using char array
657637
elif datatype in [DBR_STSACK_STRING, DBR_CLASS_NAME]:
658638
return (datatype, datatype) # format is meaningless in this case
@@ -713,31 +693,20 @@ def _convert_char_str(raw_dbr, count):
713693
def _convert_char_bytes(raw_dbr, count):
714694
return ca_bytes(_string_at(raw_dbr.raw_value, count))
715695

716-
# Conversion from char array to unicode strings
717-
def _convert_char_unicode(raw_dbr, count):
718-
return ca_unicode(_string_at(raw_dbr.raw_value, count).decode('UTF-8'))
719-
720696

721697
# Arrays of standard strings.
722698
def _convert_str_str(raw_dbr, count):
723699
return ca_str(decode(_make_strings(raw_dbr, count)[0]))
724700
def _convert_str_str_array(raw_dbr, count):
725701
strings = [decode(s) for s in _make_strings(raw_dbr, count)]
726-
return _string_array(strings, count, str_char_code)
702+
return _string_array(strings, count, 'U')
727703

728704
# Arrays of bytes strings.
729705
def _convert_str_bytes(raw_dbr, count):
730706
return ca_bytes(_make_strings(raw_dbr, count)[0])
731707
def _convert_str_bytes_array(raw_dbr, count):
732708
return _string_array(_make_strings(raw_dbr, count), count, 'S')
733709

734-
# Arrays of unicode strings.
735-
def _convert_str_unicode(raw_dbr, count):
736-
return ca_str(_make_strings(raw_dbr, count)[0].decode('UTF-8'))
737-
def _convert_str_unicode_array(raw_dbr, count):
738-
strings = [s.decode('UTF-8') for s in _make_strings(raw_dbr, count)]
739-
return _string_array(strings, count, 'U')
740-
741710

742711
# For everything that isn't a string we either return a scalar or a ca_array
743712
def _convert_other(raw_dbr, count):
@@ -790,16 +759,11 @@ def type_to_dbr(channel, datatype, format):
790759
elif dtype is numpy.uint8 and datatype == DBR_CHAR_BYTES:
791760
# Conversion from char array to bytes strings
792761
convert = _convert_char_bytes
793-
elif dtype is numpy.uint8 and datatype == DBR_CHAR_UNICODE:
794-
# Conversion from char array to unicode strings
795-
convert = _convert_char_unicode
796762
else:
797763
if dtype is str_dtype:
798-
# String arrays, either unicode or normal.
764+
# String arrays
799765
if isinstance(datatype, type) and issubclass(datatype, bytes):
800766
convert = (_convert_str_bytes, _convert_str_bytes_array)
801-
elif isinstance(datatype, type) and issubclass(datatype, unicode):
802-
convert = (_convert_str_unicode, _convert_str_unicode_array)
803767
else:
804768
convert = (_convert_str_str, _convert_str_str_array)
805769
else:
@@ -867,7 +831,7 @@ def value_to_dbr(channel, datatype, value):
867831

868832
# If no datatype specified then use the target datatype.
869833
if datatype is None:
870-
if isinstance(value, (str, bytes, unicode)):
834+
if isinstance(value, (str, bytes)):
871835
# Give strings with no datatype special treatment, let the IOC do
872836
# the decoding. It's safer this way.
873837
datatype = DBR_STRING
@@ -893,7 +857,7 @@ def value_to_dbr(channel, datatype, value):
893857
# We'll let numpy do most of the heavy lifting.
894858
result = _require_value(value, str_dtype)
895859
except UnicodeEncodeError:
896-
# Whoops, looks like unicode, need to encode each string.
860+
# Whoops, looks like we need to encode each string.
897861
value = _require_value(value, None)
898862
result = numpy.empty(value.shape, str_dtype)
899863
for n, s in enumerate(value):

0 commit comments

Comments
 (0)