Skip to content

Commit 033f0b0

Browse files
committed
Commit to Python3 only
Remove py23 and reimplement associated functions in Python 3.
1 parent d7456f2 commit 033f0b0

9 files changed

Lines changed: 42 additions & 91 deletions

File tree

src/cothread/cadef.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747

4848
import ctypes
4949
from .load_ca import libca
50-
from . import py23
5150

5251

5352

@@ -187,13 +186,31 @@ def convert_py_object(object, function, args):
187186
# libca function definitions
188187

189188

189+
# String encoding and decoding helpers for ctypes string arguments
190+
191+
class auto_encode(ctypes.c_char_p):
192+
@classmethod
193+
def from_param(cls, value):
194+
if value is None:
195+
return value
196+
else:
197+
return value.encode('UTF-8')
198+
199+
def auto_decode(result, func, args):
200+
if result is None:
201+
return result
202+
else:
203+
return result.decode('UTF-8', 'replace')
204+
205+
206+
190207
# error_message = ca_message(status_code)
191208
#
192209
# Converts channel access status code (an int) into a printable error message.
193210
ca_message = libca.ca_message
194211
ca_message.argtypes = [ctypes.c_long]
195212
ca_message.restype = ctypes.c_char_p
196-
ca_message.errcheck = py23.auto_decode
213+
ca_message.errcheck = auto_decode
197214

198215

199216
# channel_name = ca_name(channel)
@@ -202,7 +219,7 @@ def convert_py_object(object, function, args):
202219
ca_name = libca.ca_name
203220
ca_name.argtypes = [ctypes.c_void_p]
204221
ca_name.restype = ctypes.c_char_p
205-
ca_name.errcheck = py23.auto_decode
222+
ca_name.errcheck = auto_decode
206223

207224

208225
# @exception_handler
@@ -247,7 +264,7 @@ def convert_py_object(object, function, args):
247264
# recovered by calling ca_puser(channel_id).
248265
ca_create_channel = libca.ca_create_channel
249266
ca_create_channel.argtypes = [
250-
py23.auto_encode, connection_handler, ctypes.py_object,
267+
auto_encode, connection_handler, ctypes.py_object,
251268
ctypes.c_int, ctypes.c_void_p]
252269
ca_create_channel.errcheck = expect_ECA_NORMAL
253270

@@ -401,7 +418,7 @@ def convert_py_object(object, function, args):
401418
ca_host_name = libca.ca_host_name
402419
ca_host_name.argtypes = [ctypes.c_void_p]
403420
ca_host_name.restype = ctypes.c_char_p
404-
ca_host_name.errcheck = py23.auto_decode
421+
ca_host_name.errcheck = auto_decode
405422

406423

407424
# read = ca_read_access(channel_id)

src/cothread/catools.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646
See the documentation for the individual functions for more details on using
4747
them.'''
4848

49-
from __future__ import print_function
50-
5149
import os
5250
import sys
5351
import atexit
@@ -58,7 +56,6 @@
5856
from . import cothread
5957
from . import cadef
6058
from . import dbr
61-
from . import py23
6259

6360
from .dbr import *
6461
from .cadef import *
@@ -154,7 +151,7 @@ def ca_timeout(event, timeout, name):
154151
try:
155152
return event.Wait(timeout)
156153
except cothread.Timedout as timeout:
157-
py23.raise_from(ca_nothing(name, cadef.ECA_TIMEOUT), timeout)
154+
raise ca_nothing(name, cadef.ECA_TIMEOUT) from timeout
158155

159156

160157
# ----------------------------------------------------------------------------
@@ -371,7 +368,7 @@ def __signal(self, value):
371368
if isinstance(value, tuple):
372369
# This should only happen if the asynchronous callback
373370
# caught an exception for us to re-raise here.
374-
py23.raise_with_traceback(value)
371+
raise value[1].with_traceback(value[2])
375372
else:
376373
self.callback(value)
377374
except:

src/cothread/coselect.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
'''Support for cooperative select functions. Replaces the functionality of
3030
the standard select module.'''
3131

32-
from __future__ import absolute_import
33-
3432
import select as _select
3533
import cothread
3634

src/cothread/cothread.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@
6666
# It might be worth taking a close look at:
6767
# http://wiki.secondlife.com/wiki/Eventlet
6868

69-
from __future__ import print_function
70-
7169
import sys
7270
import os
7371
import time
@@ -77,7 +75,6 @@
7775
import threading
7876

7977
from . import _coroutine
80-
from . import py23
8178

8279
if os.environ.get('COTHREAD_CHECK_STACK'):
8380
_coroutine.enable_check_stack(True)
@@ -442,7 +439,7 @@ def poll_scheduler(self, ready_list):
442439

443440
if isinstance(result, tuple):
444441
# This case arises if we are main and the scheduler just died.
445-
py23.raise_with_traceback(result)
442+
raise result[1].with_traceback(result[2])
446443
else:
447444
return result
448445

@@ -492,7 +489,7 @@ def wait_until(self, until, suspend_queue, wakeup):
492489
# to die. Make sure our wakeup is cancelled, and then
493490
# re-raise the offending exception.
494491
wakeup.wakeup(result)
495-
py23.raise_with_traceback(result)
492+
raise result[1].with_traceback(result[2])
496493
else:
497494
return result == _WAKEUP_TIMEOUT
498495

@@ -711,7 +708,7 @@ def Wait(self, timeout = None):
711708
try:
712709
# Re-raise the exception that actually killed the task here
713710
# where it can be received by whoever waits on the task.
714-
py23.raise_with_traceback(result)
711+
raise result[1].with_traceback(result[2])
715712
finally:
716713
# In this case result and self.__result contain a traceback. To
717714
# avoid circular references which will delay garbage collection,
@@ -1018,7 +1015,7 @@ def do_action():
10181015
if ok:
10191016
return result
10201017
else:
1021-
py23.raise_with_traceback(result)
1018+
raise result[1].with_traceback(result[2])
10221019

10231020
# Note: raising entire stack backtrace context might be dangerous, need
10241021
# to think about this carefully, particularly if the corresponding stack

src/cothread/dbr.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import datetime
3838

3939
from . import cadef
40-
from . import py23
4140

4241

4342
__all__ = [
@@ -215,6 +214,11 @@ class ca_timestamp(ctypes.Structure):
215214
# ----------------------------------------------------------------------------
216215
# DBR type definitions
217216

217+
# Default string decoding method
218+
def decode(s):
219+
return s.decode('UTF-8', 'replace')
220+
221+
218222
# All the following types are used to overlay dbr data returned from channel
219223
# access or passed into channel access.
220224

@@ -241,7 +245,7 @@ def copy_attributes_ctrl(self, other):
241245
other.status = self.status
242246
other.severity = self.severity
243247

244-
other.units = py23.decode(ctypes.string_at(self.units))
248+
other.units = decode(ctypes.string_at(self.units))
245249
other.upper_disp_limit = self.upper_disp_limit
246250
other.lower_disp_limit = self.lower_disp_limit
247251
other.upper_alarm_limit = self.upper_alarm_limit
@@ -431,7 +435,7 @@ def copy_attributes(self, other):
431435
other.status = self.status
432436
other.severity = self.severity
433437
other.enums = [
434-
py23.decode(ctypes.string_at(s))
438+
decode(ctypes.string_at(s))
435439
for s in self.raw_strs[:self.no_str]]
436440

437441
class dbr_ctrl_char(ctypes.Structure):
@@ -631,10 +635,9 @@ def _datatype_to_dbr(datatype):
631635
# with filtering through our array of acceptable types.
632636
return NumpyCharCodeToDbr[numpy.dtype(datatype).char]
633637
except Exception as error:
634-
py23.raise_from(
635-
InvalidDatatype(
636-
'Datatype "%s" not supported for channel access' % datatype),
637-
error)
638+
raise InvalidDatatype(
639+
'Datatype "%s" not supported for channel access' % datatype
640+
) from error
638641

639642
def _type_to_dbrcode(datatype, format):
640643
'''Converts a datatype and format request to a dbr value, or raises an
@@ -704,7 +707,7 @@ def _string_at(raw_value, count):
704707

705708
# Conversion from char array to strings
706709
def _convert_char_str(raw_dbr, count):
707-
return ca_str(py23.decode(_string_at(raw_dbr.raw_value, count)))
710+
return ca_str(decode(_string_at(raw_dbr.raw_value, count)))
708711

709712
# Conversion from char array to bytes strings
710713
def _convert_char_bytes(raw_dbr, count):
@@ -717,9 +720,9 @@ def _convert_char_unicode(raw_dbr, count):
717720

718721
# Arrays of standard strings.
719722
def _convert_str_str(raw_dbr, count):
720-
return ca_str(py23.decode(_make_strings(raw_dbr, count)[0]))
723+
return ca_str(decode(_make_strings(raw_dbr, count)[0]))
721724
def _convert_str_str_array(raw_dbr, count):
722-
strings = [py23.decode(s) for s in _make_strings(raw_dbr, count)]
725+
strings = [decode(s) for s in _make_strings(raw_dbr, count)]
723726
return _string_array(strings, count, str_char_code)
724727

725728
# Arrays of bytes strings.

src/cothread/load_ca.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
# This file can also be run as a standalone script to discover the path to
3333
# libca.
3434

35-
from __future__ import print_function
36-
3735
import ctypes
3836
import platform
3937
import os

src/cothread/py23.py

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/cothread/tools/pvtree.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
# Simple tool for viewing the chain of PV dependencies.
44

5-
from __future__ import print_function
6-
75
import sys
86
import re
97
import os

tests/counittest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from cothread import coselect, cosocket
1414
from cothread import Spawn, Sleep, Event, Timedout
1515
from cothread.load_ca import epics_host_arch
16-
from cothread import py23
1716

1817
__all__ = [
1918
'IOCTestCaseMixin',
@@ -292,7 +291,7 @@ def _tty_read(self):
292291
# seperate cothread
293292
try:
294293
while True:
295-
T = py23.decode(self._tty.read(1024))
294+
T = self._tty.read(1024).decode('UTF-8', 'replace')
296295
if not T:
297296
return
298297
self._tty_in += T

0 commit comments

Comments
 (0)