aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Barry <dave@psax.org>2013-05-15 02:10:26 -0700
committerDave Barry <dave@psax.org>2013-05-15 02:10:26 -0700
commit88c53cd44a626ede3b07dab0b548f8bcfda42867 (patch)
treeb1af5d98509e9c3f4fbcbc1165a93cdafaaffce5
parent65c3b3f1c054e79e3ac85b8c47f403e3c4c2e181 (diff)
parente1104818d795bde79e203ba750ce37fbba9a8e90 (diff)
downloadpycanberra-88c53cd44a626ede3b07dab0b548f8bcfda42867.tar
pycanberra-88c53cd44a626ede3b07dab0b548f8bcfda42867.tar.gz
Merge pull request #2 from bochecha/master
Make pycanberra work with both Python 2 and 3
-rw-r--r--pycanberra.py27
1 files changed, 23 insertions, 4 deletions
diff --git a/pycanberra.py b/pycanberra.py
index 6b1a064..0d219ba 100644
--- a/pycanberra.py
+++ b/pycanberra.py
@@ -4,9 +4,20 @@
# License: LGPL 2.1
##########################################################################
from ctypes import *
-import exceptions
import time
+# This is inspired by the six module: http://pypi.python.org/pypi/six
+import sys
+if sys.version_info.major == 3:
+ string_types = str,
+ def b(s):
+ return s.encode("latin-1")
+else:
+ string_types = basestring,
+ def b(s):
+ return s
+
+
# /**
# * CA_PROP_MEDIA_NAME:
# *
@@ -519,16 +530,16 @@ def GetApi():
# int ca_proplist_set(ca_proplist *p, const char *key, const void *data, size_t nbytes);
-class CanberraException(exceptions.Exception):
+class CanberraException(Exception):
def __init__(self, err, *args, **kwargs):
self._err = err
- super(exceptions.Exception, self).__init__(*args, **kwargs)
+ super(Exception, self).__init__(*args, **kwargs)
def get_error(self):
return self._err
def __str__(self):
- return super(exceptions.Exception, self).__str__() + " (error %d)" % self._err
+ return super(Exception, self).__str__() + " (error %d)" % self._err
class Canberra(object):
@@ -557,6 +568,8 @@ class Canberra(object):
raise CanberraException(res, "Failed to destroy context")
def change_props(self, *args):
+ args = tuple(b(arg) if isinstance(arg, string_types) else arg
+ for arg in args)
res = GetApi().ca_context_change_props(self._handle, *args)
if res != CA_SUCCESS:
raise CanberraException(res, "Failed to change props")
@@ -574,11 +587,15 @@ class Canberra(object):
pass
def play(self, playId, *args):
+ args = tuple(b(arg) if isinstance(arg, string_types) else arg
+ for arg in args)
res = GetApi().ca_context_play(self._handle, playId, *args)
if res != CA_SUCCESS:
raise CanberraException(res, "Failed to play!")
def cache(self, *args):
+ args = tuple(b(arg) if isinstance(arg, string_types) else arg
+ for arg in args)
res = GetApi().ca_context_cache(self._handle, *args)
if res != CA_SUCCESS:
raise CanberraException(res, "Failed to cache")
@@ -597,6 +614,8 @@ class Canberra(object):
def easy_play_sync(self, eventName):
""" play an event sound synchronously """
+ if isinstance(eventName, string_types):
+ eventName = b(eventName)
self.play(1,
CA_PROP_EVENT_ID, eventName,
None)