Repository navigation
Conversation
There was a problem hiding this comment.
I like the API, but I think encoding issues need more consideration. On Python 2, when you use .decode(), you'll get a unicode back, so you need to make sure that your functions can handle these as input. Checking for isinstance(string, str) is too simplistic.
Also, you need to think about whether a C-string may contain non-ASCII chars. Then you'll need to pass an encoding to decode(). The same when you pass a Python str or Python 2 unicode to C and .encode() it.
| # Get metadata constants from library | ||
| for name in dir(_lib): | ||
| if name.startswith('JACK_METADATA_'): | ||
| globals()[name[5:]] = _decode(getattr(_lib, name)) |
There was a problem hiding this comment.
It would probably be a bit more effective to update the module globals in one go:
globals().update((name[5:], _decode(getattr(_lib, name)))
for name in dir(_lib) if name.startswith('JACK_METADATA_'))
This doesn't leak the the loop variable into the surrounding namespace without having to delete it manually.
(Saw this trick in the standard library module multiprocessing.)
There was a problem hiding this comment.
Thanks, that's a great trick!
However, I think i would like to be a bit more defensive in case those constants are not defined in the library. I wanted to do that at a later point, but I guess I can already do it now: 5d9b565
| remove_property | ||
| remove_properties | ||
| remove_all_properties | ||
| set_property_change_callback |
There was a problem hiding this comment.
Should these be marked up with backticks?
There was a problem hiding this comment.
Those are converted to links correctly, so I don't think that's necessary. I guess the sphinx.ext.napoleon extension takes care of that.
| """ | ||
| subject = _uuid_parse(subject) | ||
| if isinstance(value, str): | ||
| value = value.encode() |
There was a problem hiding this comment.
utf-8 should be given as an argument to encode() here. UTF-8 is not the default encoding in all Python installations.
| get_all_properties | ||
| remove_properties | ||
| remove_all_properties | ||
| set_property_change_callback |
There was a problem hiding this comment.
Should these be marked up with backticks?
|
|
||
| """ | ||
| subject = _uuid_parse(subject) | ||
| if isinstance(value, str): |
There was a problem hiding this comment.
This is problematic on Python 2. The str type is essentially the same as bytes on Python 3 there. It's value should not be encoded, it already is. unicode instances, on the other hand, should be encoded. One solution is to use isinstance(s, six.text_type) or have something like this at the top of your module:
if bytes is str:
text_type = unicode
else:
text_type = str
(But I'm not sure whether the latter works on Python 2.6.)
There was a problem hiding this comment.
I've used this in the past:
try:
_unicode = unicode # doesn't exist in Python 3.x
except NameError:
_unicode = strI think adding a dependency on six is overkill here.
| get_all_properties | ||
| remove_property | ||
| remove_all_properties | ||
| set_property_change_callback |
There was a problem hiding this comment.
Should these be marked up with backticks?
| get_all_properties | ||
| remove_property | ||
| remove_properties | ||
| set_property_change_callback |
There was a problem hiding this comment.
Should these be marked up with backticks?
| Client.remove_property | ||
| Client.remove_properties | ||
| Client.remove_all_properties | ||
| set_property_change_callback |
There was a problem hiding this comment.
Should these be marked up with backticks?
| Client.remove_property | ||
| Client.remove_properties | ||
| Client.remove_all_properties | ||
| set_property_change_callback |
There was a problem hiding this comment.
Should these be marked up with backticks?
|
|
||
|
|
||
| def _decode(cdata): | ||
| return _ffi.string(cdata).decode() |
There was a problem hiding this comment.
This handles only ASCII strings, doesn't it?
Maybe allow to pass an encoding?
def _decode(cdata, encoding='utf-8'):
return _ffi.string(cdata).decode(encoding)
There was a problem hiding this comment.
I was assuming Python 3, where encoding="utf-8", errors="strict" is the default.
I guess for Python 2 support we would have to make this explicit. But I don't think we have to make it configurable.
There was a problem hiding this comment.
The default can still be changed at runtime, even in Python 3, though.
You're right. In Python 3 'utf-8' is the default. I though sys.setdefaultencoding was still available in Python 3, but it isn't.
|
@SpotlightKid Thanks for the review! I don't have any motivation to actively write code for Python 2 support anymore. This PR assumes Python 3. Do you need Python 2 support? If yes, could you please make a new PR based on my PR that takes care of Python 2? |
|
No, I myself decided to drop Python 2 support in my own projects successivley as well. It's just that the readme still mentions even Python 2.6 support :-). |
|
@SpotlightKid That's a good point! To fix this, I've just created #65, which I'm planning to merge before this PR. Assuming only Python 3 compatibility, are there still open issues? Any suggestions for API improvements? |
if isinstance(uuid, (Client, Port)):
uuid = uuid.uuid
diff --git a/src/jack.py b/src/jack.py
index 6313e05..b41db6f 100644
--- a/src/jack.py
+++ b/src/jack.py
@@ -2560,21 +2560,29 @@ def _uuid_parse(uuid):
raise TypeError('Invalid UUID: {!r}'.format(uuid))
-def _description_to_dict(desc):
+def _decode_property(prop, encoding='utf-8'):
+ type_ = _decode(prop.type) if prop.type else None
+ value = _ffi.string(prop.data)
+
+ if not type_ or type_.startswith('text/') and encoding:
+ value = value.decode(encoding, errors='ignore')
+
+ return value, type_
+
+
+def _description_to_dict(desc, encoding):
assert desc != _ffi.NULL
prop_dict = {}
for i in range(desc.property_cnt):
prop = desc.properties[i]
key = _decode(prop.key)
- prop_dict[key] = (
- _ffi.string(prop.data),
- _decode(prop.type) if prop.type else '')
+ prop_dict[key] = _decode_property(prop, encoding)
free_description_itself = 0
_lib.jack_free_description(desc, free_description_itself)
return prop_dict
-def get_property(subject, key):
+def get_property(subject, key, encoding='utf-8'):
"""Get a metadata property on *subject*.
Parameters
@@ -2610,11 +2618,17 @@ def get_property(subject, key):
if _lib.jack_get_property(subject, key.encode(), data, type) != 0:
return None
data = _ffi.gc(data[0], _lib.jack_free)
+ data = _ffi.string(data)
type = _ffi.gc(type[0], _lib.jack_free)
- return _ffi.string(data), _decode(type) if type else ''
+ type = _decode(type) if type else None
+
+ if not type or type.startswith('text/') and encoding:
+ data = data.decode(encoding, errors='ignore')
+
+ return data, type
-def get_properties(subject):
+def get_properties(subject, encoding='utf8'):
"""Get all metadata properties of *subject*.
Parameters
@@ -2647,10 +2661,10 @@ def get_properties(subject):
raise RuntimeError(
'Unable to get properties for subject {!r}'.format(subject))
assert number == desc.property_cnt
- return _description_to_dict(desc)
+ return _description_to_dict(desc, encoding)
-def get_all_properties():
+def get_all_properties(encoding='utf8'):
"""Get all properties for all subjects with metadata.
Returns
@@ -2679,7 +2693,7 @@ def get_all_properties():
for idx in range(number):
desc = descs + idx
assert desc.subject not in prop_dict
- prop_dict[desc.subject] = _description_to_dict(desc)
+ prop_dict[desc.subject] = _description_to_dict(desc, encoding)
return prop_dictYou could still pass |
|
Thanks a lot for those suggestions!
I was considering this, but even when I was passing Probably I did something wrong. Did you try this?
You already mentioned some problems with this. Since I need a I think it is more consistent if the "subject" always has to be explicit. Yet another problem is that changing properties on
Good idea, I've added your suggestion in d79be4a.
That's true, but I think Also, we shouldn't expose an I don't really like the Finally, I'm skeptical whether it is a good idea to return different types from a function depending on external factors. The JACK docs state that when
But then users wouldn't be able to use tuple unpacking and I think the handling would be more complicated. I think this is easier to handle:
If I understood correctly, your suggestion would return one of those:
Wouldn't this make user code more complicated because they always have to check if they got a |
Yes, when using ctypes, I get a null pointer back, when the type is not set. I think that's just an implementation detail of how cffi works? |
|
I concur with the rest of your points, though having some more user-friendly wrapper code would probably be nice. |
OK, I tried it again, and it turns out I was mistaken about getting If I set I think that was the reason why I chose to not support Are you getting an empty string back?
I agree, but I think adding this to the I think I would prefer providing a very basic low-level API, and users can create their own wrappers based on their specific use cases. |
Huh? I don't understand the logic behind that. Anyway, setting |
If I set the If I allow both Therefore, I think it's better to allow only one of them. Since disallowing Does that argument make sense?
According to http://jackaudio.org/api/structjack__property__t.html#a298b7c97464c5ac3a6070c467732bd9c: "If type is NULL or empty, the data is assumed to be a UTF-8 encoded string (text/plain)." (emphasis mine) Do you have a different spec? |
No, I just didn't interpret "empty" as an empty string ;)
OTOH, I would find it surprising that if no
Which strengthens my belief that the intention of the JACK developers was, that if type is set, it should be a string containing something (a mime-type or a URI). Maybe this should be raised as an issue with JACK? Have you tested the behavior of Jack2 (current git)? |
The function signature of There is no reason for a user to believe that Would you expect
I don't think so. I think that's just them dealing with an idiosyncrasy of C, where a "string" can be either a There is no reason to provide two different representations for "empty" ( No other language has to worry about those two cases and can simply use the empty string to signify "empty".
I don't think that's an issue. But feel free to open an issue there if you think there is one!
No, I didn't. But whatever its behavior is, I think my current solution would work (and return |
|
Sorry for not having answered yet. I think we we both made our views clear now and it's up to you to decide what the API should look like in the end. I'm looking forward to using it. |
|
Just a little ping... anything still preventing this from being merged? |
|
@SpotlightKid Sorry for the late response and thanks for the reminder! I've just merged the current state, but if you have further suggestions for changes feel free to comment here or make a new PR! |
See #40.