From: Jaroslav Kysela Date: Tue, 31 May 2022 13:46:15 +0000 (+0200) Subject: various python3 fixes X-Git-Tag: v1.2.7~2 X-Git-Url: https://git.alsa-project.org/?a=commitdiff_plain;h=a1e64cc703a8a33d3acecf5f46cd40bebab88936;p=alsa-python.git various python3 fixes Fixes: https://github.com/alsa-project/alsa-python/issues/6 Fixes: https://github.com/alsa-project/alsa-python/pull/1 Signed-off-by: Jaroslav Kysela --- diff --git a/pyalsa/alsahcontrol.c b/pyalsa/alsahcontrol.c index 7c9321f..1ad0c29 100644 --- a/pyalsa/alsahcontrol.c +++ b/pyalsa/alsahcontrol.c @@ -621,7 +621,7 @@ static void pyalsahcontrolelement_dealloc(struct pyalsahcontrolelement *self) { if (self->elem) { - Py_DECREF(self->callback); + Py_XDECREF(self->callback); snd_hctl_elem_set_callback(self->elem, NULL); } Py_XDECREF(self->pyhandle); diff --git a/pyalsa/alsaseq.c b/pyalsa/alsaseq.c index 9d9b1a9..e3b76a2 100644 --- a/pyalsa/alsaseq.c +++ b/pyalsa/alsaseq.c @@ -1677,16 +1677,11 @@ static PyGetSetDef SeqEvent_getset[] = { static PyObject * SeqEvent_repr(SeqEventObject *self) { PyObject *key = PyInt_FromLong(self->event->type); - ConstantObject *constObject = (ConstantObject *) - PyDict_GetItem(TDICT(EVENT_TYPE), key); - const char *typestr = "UNKNOWN"; + ConstantObject *typeObject = (ConstantObject *)PyDict_GetItem(TDICT(EVENT_TYPE), key); const char *timemode = ""; unsigned int dtime = 0; unsigned int ntime = 0; Py_DECREF(key); - if (constObject != NULL) { - typestr = constObject->extra.name; - } if (snd_seq_ev_is_real(self->event)) { timemode = "real"; @@ -1697,17 +1692,18 @@ SeqEvent_repr(SeqEventObject *self) { dtime = self->event->time.tick; } - return PyUnicode_FromFormat("", - typestr, + typeObject, self->event->type, self->event->flags, self->event->tag, self->event->queue, timemode, dtime, ntime, (self->event->source).client, (self->event->source).port, (self->event->dest).client, - (self->event->dest).port, self); + (self->event->dest).port, + self); } /** alsaseq.SeqEvent get_data() method: __doc__ */ diff --git a/pyalsa/common.h b/pyalsa/common.h index 1ff66c0..ee50ba8 100644 --- a/pyalsa/common.h +++ b/pyalsa/common.h @@ -22,6 +22,8 @@ #ifndef __PYALSA_COMMON_H #define __PYALSA_COMMON_H +#define PY_SSIZE_T_CLEAN + #include "Python.h" #include "structmember.h" #include "frameobject.h" diff --git a/test/cardtest1.py b/test/cardtest1.py index 045707a..13a418b 100755 --- a/test/cardtest1.py +++ b/test/cardtest1.py @@ -2,9 +2,9 @@ # -*- Python -*- import sys -sys.path.insert(0, '../pyalsa') +sys.path.insert(0, '..') del sys -import alsacard +from pyalsa import alsacard print('asoundlibVersion:', alsacard.asoundlib_version()) print('cardLoad:', alsacard.card_load(0)) diff --git a/test/ctltest1.py b/test/ctltest1.py index 53abbe9..3670866 100755 --- a/test/ctltest1.py +++ b/test/ctltest1.py @@ -2,9 +2,9 @@ # -*- Python -*- import sys -sys.path.insert(0, '../pyalsa') +sys.path.insert(0, '..') del sys -import alsacontrol +from pyalsa import alsacontrol ctl = alsacontrol.Control() print('Card info:', ctl.card_info()) diff --git a/test/hctltest1.py b/test/hctltest1.py index 7d0bf15..9962af6 100755 --- a/test/hctltest1.py +++ b/test/hctltest1.py @@ -2,9 +2,9 @@ # -*- Python -*- import sys -sys.path.insert(0, '../pyalsa') +sys.path.insert(0, '..') del sys -import alsahcontrol +from pyalsa import alsahcontrol def info(element): info = alsahcontrol.Info(element) diff --git a/test/hctltest2.py b/test/hctltest2.py index d16873e..4b19816 100755 --- a/test/hctltest2.py +++ b/test/hctltest2.py @@ -2,10 +2,10 @@ # -*- Python -*- import sys -sys.path.insert(0, '../pyalsa') +sys.path.insert(0, '..') del sys import select -import alsahcontrol +from pyalsa import alsahcontrol def parse_event_mask(events): if events == 0: diff --git a/test/mixertest1.py b/test/mixertest1.py index 7693692..03dd58a 100755 --- a/test/mixertest1.py +++ b/test/mixertest1.py @@ -2,10 +2,10 @@ # -*- Python -*- import sys -sys.path.insert(0, '../pyalsa') +sys.path.insert(0, '..') del sys from alsamemdebug import debuginit, debug, debugdone -import alsamixer +from pyalsa import alsamixer def print_elem(e): direction = ["playback", "capture"] diff --git a/test/mixertest2.py b/test/mixertest2.py index 6c2d026..c373723 100755 --- a/test/mixertest2.py +++ b/test/mixertest2.py @@ -4,7 +4,7 @@ import sys sys.path.insert(0, '../pyalsa') del sys -import alsamixer +from pyalsa import alsamixer import select def parse_event_mask(events): @@ -20,15 +20,15 @@ def parse_event_mask(events): def event_callback(element, events): - print 'CALLBACK (DEF)! [%s] %s:%i' % (parse_event_mask(events), element.name, element.index) - print ' ', element.get_volume_tuple(), element.get_switch_tuple() + print('CALLBACK (DEF)! [%s] %s:%i' % (parse_event_mask(events), element.name, element.index)) + print(' ', element.get_volume_tuple(), element.get_switch_tuple()) class MyElementEvent: def callback(self, element, events): - print 'CALLBACK (CLASS)! [%s] %s:%i' % (parse_event_mask(events), element.name, element.index) - print ' ', element.get_volume_tuple(), element.get_switch_tuple() + print('CALLBACK (CLASS)! [%s] %s:%i' % (parse_event_mask(events), element.name, element.index)) + print(' ', element.get_volume_tuple(), element.get_switch_tuple()) mixer = alsamixer.Mixer() @@ -44,5 +44,5 @@ poller = select.poll() mixer.register_poll(poller) while True: poller.poll() - print 'Poll OK!' + print('Poll OK!') mixer.handle_events() diff --git a/test/seqtest1.py b/test/seqtest1.py old mode 100644 new mode 100755 index 83ad102..def0fa5 --- a/test/seqtest1.py +++ b/test/seqtest1.py @@ -8,20 +8,20 @@ # sequencer application. import sys -sys.path.insert(0, '../pyalsa') +sys.path.insert(0, '..') del sys from alsamemdebug import debuginit, debug, debugdone -import alsaseq +from pyalsa import alsaseq def dump_portinfo(dict): def dl(dict, key): - if dict.has_key(key): + if key in dict: return "'" + str(dict[key]) + "'" return "N/A" def da(dict, key, search): tmp = None - if dict.has_key(key): + if key in dict: for k in search: if k & dict[key]: if tmp == None: @@ -39,151 +39,155 @@ def dump_portinfo(dict): def dump_list(connections, simple=True): for clientports in connections: clientname, clientid, portlist = clientports - print " client: %3d %s" % (clientid, clientname), + print(" client: %3d %s" % (clientid, clientname)) if not simple: clientinfo = sequencer.get_client_info(clientid) - print "\t[%s]" % clientinfo + print("\t[%s]" % clientinfo) else: - print + print() for port in portlist: portname, portid, connections = port - print " port: %3d:%-2d +-%s" % (clientid, portid, portname), + print(" port: %3d:%-2d +-%s" % (clientid, portid, portname)) if not simple: portinfo = sequencer.get_port_info(portid, clientid) - print "\t[%s]" % (dump_portinfo(portinfo)) + print("\t[%s]" % (dump_portinfo(portinfo))) else: - print + print() readconn, writeconn = connections for c,p,i in readconn: if not simple: - print " connection to: %d:%d %s" % (c,p, i) + print(" connection to: %d:%d %s" % (c,p, i)) else: - print " connection to: %d:%d" % (c,p) + print(" connection to: %d:%d" % (c,p)) for c,p,i in writeconn: if not simple: - print " connection to: %d:%d %s" % (c,p, i) + print(" connection to: %d:%d %s" % (c,p, i)) else: - print " connection to: %d:%d" % (c,p) + print(" connection to: %d:%d" % (c,p)) debuginit() -print "01:Creating Sequencer ==============================" +print("01:Creating Sequencer ==============================") sequencer = alsaseq.Sequencer() # other examples: # sequencer = alsaseq.Sequencer("hw", "myfancyapplication", alsaseq.SEQ_OPEN_INPUT, alsaseq.SEQ_BLOCK) # sequencer = alsaseq.Sequencer(clientname='python-test', streams=alsaseq.SEQ_OPEN_OUTPUT) -print " sequencer: %s" % sequencer -print " name: %s" % sequencer.name -print " clientname: %s" % sequencer.clientname -print " streams: %d (%s)" % (sequencer.streams, str(sequencer.streams)) -print " mode: %d (%s)" % (sequencer.mode, str(sequencer.mode)) -print " client_id: %s" % sequencer.client_id -print - -print "02:Changing some parameters ========================" +print(" sequencer: %s" % sequencer) +print(" name: %s" % sequencer.name) +print(" clientname: %s" % sequencer.clientname) +print(" streams: %d (%s)" % (sequencer.streams, str(sequencer.streams))) +print(" mode: %d (%s)" % (sequencer.mode, str(sequencer.mode))) +print(" client_id: %s" % sequencer.client_id) +print() + +print("02:Changing some parameters ========================") sequencer.clientname = 'pepito' sequencer.mode = alsaseq.SEQ_BLOCK -print " clientname: %s" % sequencer.clientname -print " mode: %d (%s)" % (sequencer.mode, str(sequencer.mode)) -print +print(" clientname: %s" % sequencer.clientname) +print(" mode: %d (%s)" % (sequencer.mode, str(sequencer.mode))) +print() -print "03:Creating simple port ============================" +print("03:Creating simple port ============================") port_id = sequencer.create_simple_port("myport", alsaseq.SEQ_PORT_TYPE_APPLICATION,alsaseq.SEQ_PORT_CAP_WRITE | alsaseq.SEQ_PORT_CAP_SUBS_WRITE) -print " port_id: %s" % port_id -print +print(" port_id: %s" % port_id) +print() -print "04:Getting port info ===============================" +print("04:Getting port info ===============================") port_info = sequencer.get_port_info(port_id) -print " --> %s" % dump_portinfo(port_info) -print " --> %s" % port_info -print +print(" --> %s" % dump_portinfo(port_info)) +print(" --> %s" % port_info) +print() -print "05:Retrieving clients and connections (as list) ====" +print("05:Retrieving clients and connections (as list) ====") connections = sequencer.connection_list() -print " %s" % (connections) -print +print(" %s" % (connections)) +print() -print "06:Retrieving clients and connections (detailed) ===" +print("06:Retrieving clients and connections (detailed) ===") connections = sequencer.connection_list() dump_list(connections, False) -print +print() -print "07:Connecting 'arbitrary' ports... =================" +print("07:Connecting 'arbitrary' ports... =================") source = (alsaseq.SEQ_CLIENT_SYSTEM, alsaseq.SEQ_PORT_SYSTEM_ANNOUNCE) dest = (sequencer.client_id, port_id) -print "%s ---> %s" % (str(source), str(dest)) +print("%s ---> %s" % (str(source), str(dest))) sequencer.connect_ports(source, dest) -print +print() -print "08:Retrieving clients and connections (simple) =====" +print("08:Retrieving clients and connections (simple) =====") connections = sequencer.connection_list() dump_list(connections) -print +print() -print "09:Disconnecting previous 'arbitrary' port =========" -print "%s -X-> %s" % (str(source), str(dest)) +print("09:Disconnecting previous 'arbitrary' port =========") +print("%s -X-> %s" % (str(source), str(dest))) sequencer.disconnect_ports(source, dest) -print +print() -print "10:Retrieving clients and connections (simple) =====" +print("10:Retrieving clients and connections (simple) =====") connections = sequencer.connection_list() dump_list(connections) -print +print() -print "11:Listing known streams constants =================" -print "%s" % alsaseq._dstreams.values() -print +print("11:Listing known streams constants =================") +print("%s" % alsaseq._dstreams.values()) +print() -print "12:Listing known mode constants ====================" -print "%s" % alsaseq._dmode.values() -print +print("12:Listing known mode constants ====================") +print("%s" % alsaseq._dmode.values()) +print() -print "13:Listing known queue constants ===================" -print "%s" % alsaseq._dqueue.values() -print +print("13:Listing known queue constants ===================") +print("%s" % alsaseq._dqueue.values()) +print() -print "14:Listing known client type constants =============" -print "%s" % alsaseq._dclienttype.values() -print +print("14:Listing known client type constants =============") +print("%s" % alsaseq._dclienttype.values()) +print() -print "15:Listing known port caps constants ===============" -print "%s" % alsaseq._dportcap.values() -print +print("15:Listing known port caps constants ===============") +print("%s" % alsaseq._dportcap.values()) +print() -print "16:Listing known port types constants ==============" -print "%s" % alsaseq._dporttype.values() -print +print("16:Listing known port types constants ==============") +print("%s" % alsaseq._dporttype.values()) +print() -print "17:Listing known event type constants ==============" -print "%s" % alsaseq._deventtype.values() -print +print("17:Listing known event type constants ==============") +print("%s" % alsaseq._deventtype.values()) +print() -print "18:Listing known event timestamp constants =========" -print "%s" % alsaseq._deventtimestamp.values() -print +print("18:Listing known event timestamp constants =========") +print("%s" % alsaseq._deventtimestamp.values()) +print() -print "19:Listing known event timemode constants ==========" -print "%s" % alsaseq._deventtimemode.values() -print +print("19:Listing known event timemode constants ==========") +print("%s" % alsaseq._deventtimemode.values()) +print() -print "20:Listing known client addresses constants ========" -print "%s" % alsaseq._dclient.values() -print +print("20:Listing known client addresses constants ========") +print("%s" % alsaseq._dclient.values()) +print() -print "21:Listing known port addresses constants ==========" -print "%s" % alsaseq._dport.values() -print +print("21:Listing known port addresses constants ==========") +print("%s" % alsaseq._dport.values()) +print() +print("22:SeqEvent repr ===================================") +print("%s" % alsaseq.SeqEvent(alsaseq.SEQ_EVENT_NOTEON)) +print() -print "98:Removing sequencer ==============================" + +print("98:Removing sequencer ==============================") debug([sequencer]) del sequencer -print +print() -print "99:Listing sequencer (using new one) ===============" +print("99:Listing sequencer (using new one) ===============") dump_list(alsaseq.Sequencer().connection_list()) -print +print() debugdone() -print "seqtest1.py done." +print("seqtest1.py done.") diff --git a/test/seqtest2.py b/test/seqtest2.py old mode 100644 new mode 100755 index b7cbd4f..448ffec --- a/test/seqtest2.py +++ b/test/seqtest2.py @@ -8,9 +8,9 @@ # sequencer application. import sys -sys.path.insert(0, '../pyalsa') +sys.path.insert(0, '..') -import alsaseq +from pyalsa import alsaseq import time from alsamemdebug import debuginit, debug, debugdone @@ -19,24 +19,24 @@ debuginit() seq = alsaseq.Sequencer() def dump(event): - print "event: %s" % event - print " ", + print("event: %s" % event) + print(" ") for attr in alsaseq.SeqEvent.__dict__: if attr.startswith('is_'): t = event.__getattribute__(attr) if t: - print "%s" % attr, - print + print("%s" % attr) + print() data = event.get_data() - print " data=%s" % data + print(" data=%s" % data) -print "sequencer: %s" % seq +print("sequencer: %s" % seq) port_id = seq.create_simple_port('hola', alsaseq.SEQ_PORT_TYPE_APPLICATION, alsaseq.SEQ_PORT_CAP_SUBS_READ | alsaseq.SEQ_PORT_CAP_READ | alsaseq.SEQ_PORT_CAP_WRITE | alsaseq.SEQ_PORT_CAP_SUBS_WRITE ) -print "portid: %d" % port_id +print("portid: %d" % port_id) c=-2 wait = 5000 @@ -45,11 +45,11 @@ while True: if c == -1: src = (alsaseq.SEQ_CLIENT_SYSTEM,alsaseq.SEQ_PORT_SYSTEM_ANNOUNCE) dest = (seq.client_id, port_id) - print 'connecting %s -> %s' % (src, dest) + print('connecting %s -> %s' % (src, dest)) seq.connect_ports(src, dest) if c == 5: break - print 'waiting %s...' % wait + print('waiting %s...' % wait) events = seq.receive_events(wait) for event in events: c = 0 diff --git a/test/seqtest3.py b/test/seqtest3.py old mode 100644 new mode 100755 index 7d96aa4..90d36b9 --- a/test/seqtest3.py +++ b/test/seqtest3.py @@ -8,9 +8,9 @@ # sequencer application. import sys -sys.path.insert(0, '../pyalsa') +sys.path.insert(0, '..') -from alsaseq import * +from pyalsa.alsaseq import * import time from alsamemdebug import debuginit, debug, debugdone @@ -28,9 +28,9 @@ def findmidiport(): type = pinfo['type'] caps = pinfo['capability'] if type & SEQ_PORT_TYPE_MIDI_GENERIC and caps & (SEQ_PORT_CAP_WRITE): - print "Using port: %s:%s" % (cname, pname) + print("Using port: %s:%s" % (cname, pname)) return (cid, pid) - print "No midi port found -- install timidity or other software synth for testing!" + print("No midi port found -- install timidity or other software synth for testing!") sys.exit(0) # create sequencer @@ -43,7 +43,7 @@ cid, pid = findmidiport() queue = seq.create_queue() seq.start_queue(queue) tempo, ppq = seq.queue_tempo(queue) -print "tempo: %d ppq: %d" % (tempo, ppq) +print("tempo: %d ppq: %d" % (tempo, ppq)) # play notes: DO RE MI FA SOL LA notes = [0x40, 0x42, 0x44, 0x45, 0x47, 0x49] @@ -53,7 +53,7 @@ for note in notes: event.queue = queue event.time += ppq event.set_data({'note.note' : note, 'note.velocity' : 64, 'note.duration' : ppq , 'note.off_velocity' : 64}) - print 'event: %s %s' % (event, event.get_data()) + print('event: %s %s' % (event, event.get_data())) seq.output_event(event) seq.drain_output() seq.sync_output_queue()