%s is a string. %U is unicode? If so, then %s should only be used when it is certain the string in question has no unicode in it? -- ~Ethan~
On Wed, 26 Mar 2014 11:48:44 +0100 Antoine Pitrou <solipsis@pitrou.net> wrote:
On Tue, 25 Mar 2014 18:43:30 -0700 Ethan Furman <ethan@stoneleaf.us> wrote:
%s is a string.
%U is unicode?
What is the context?
Ok, I suppose it's PyUnicode_Format? :-) (I was initially thinking PyArg_ParseTuple, but it doesn't use "%") Regards Antoine.
2014-03-26 12:02 GMT+01:00 Antoine Pitrou <solipsis@pitrou.net>:
Ok, I suppose it's PyUnicode_Format? :-)
I don't think that PyUnicode_Format supports %U. For PyUnicode_FromFormatV(), %U expects a Python Unicode object, whereas "%s" expects a ASCII (or latin1?) encoded byte string "const char*". For PyArg_ParseTuple, "s" accepts str and bytes and encodes str to UTF-8, whereas "U" expects str. Victor
On 03/26/2014 03:48 AM, Antoine Pitrou wrote:
On Tue, 25 Mar 2014 18:43:30 -0700 Ethan Furman <ethan@stoneleaf.us> wrote:
%s is a string.
%U is unicode?
What is the context?
A patch I'm adapting has these lines: static PyObject* module_getattr(PyObject *m, PyObject *name) { char *mod_name_str; ... PyErr_Format(PyExc_AttributeError, "module '%s' has no attribute '%U'", mod_name_str, name); So it looks like %s is referring to a simple string, and %U is referring to a PyObject, but I was hoping for verification. -- ~Ethan~
[Assuming you are talking about PyUnicode_FromFormatV]
%s is a string.
No. %s is a char*; C does not have a "string" type. The string behind the pointer should be UTF-8 encoded; other encodings are tolerated through the "replace" error handler.
%U is unicode?
No. This is a PyObject* whose Python type is 'str' (i.e. an object for which PyUnicode_Check succeeds)
If so, then %s should only be used when it is certain the string in question has no unicode in it?
No. If you have a char*, use %s; using %U would crash. Regards, Martin
On 03/26/2014 06:22 AM, � wrote:
[Assuming you are talking about PyUnicode_FromFormatV]
%s is a string.
No. %s is a char*; C does not have a "string" type. The string behind the pointer should be UTF-8 encoded; other encodings are tolerated through the "replace" error handler.
%U is unicode?
No. This is a PyObject* whose Python type is 'str' (i.e. an object for which PyUnicode_Check succeeds)
If so, then %s should only be used when it is certain the string in question has no unicode in it?
No. If you have a char*, use %s; using %U would crash.
Many thanks! -- ~Ethan~
participants (5)
-
"Martin v. Löwis" -
Antoine Pitrou -
Ethan Furman -
Serhiy Storchaka -
Victor Stinner