New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: Added field name to error message. Closes #6232 #6242
Conversation
We should add a test that validates the behavior. Also, for Python 3 |
@jaimefrio Ya, you were right. I have to convert Python3 Unicode string to ASCII else it fails in python3. Well, now I used PyUnicode_AsASCIIString to convert it. I first check whether its python3 or not, if it is I convert it to ASCII. I am now going to write the test and will update this PR as soon as possible. |
@jaimefrio I tested it with python 2.7.9 and python 3.4.3. Same for the test case written. |
Do we support non-ASCII field names, and if so do they make things blow up? On Mon, Aug 24, 2015 at 5:23 PM, Yash Mehrotra notifications@github.com
Nathaniel J. Smith -- http://vorpus.org |
@njsmith I made the changes as required, and I used |
PyErr_SetString(PyExc_ValueError, | ||
"two fields with the same name"); | ||
#if defined(NPY_PY3K) | ||
name = PyUnicode_AsUTF8String(name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PyUnicode_AsUTF8String
returns a new reference, see here, so you will need to Py_DECREF(next)
afterwards. There may be other ways, but a possible solution could look like:
#if defined(NPY_PY3K)
name = PyUnicode_AsUTF8String(name);
#else
Py_INCREF(next);
#endif
PyErr_Format(PyExc_ValueError,
"field '%s' occurs more than once", PyString_AsString(name));
Py_DECREF(next);
goto fail;
Also, we are assuming that PyUnicode_AsUTF8String
will always succeed. This seems to be consistent with other uses of the same function in the NumPy code base, but I am not 100% sure it is a good practice.
Python 2's
So I guess we do not need to worry about Python 3 is happy:
|
@jaimefrio Thanks for the help. |
#endif | ||
PyErr_Format(PyExc_ValueError, | ||
"field '%s' occurs more than once", PyString_AsString(name)); | ||
Py_DECREF(name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Come to think of it, if we only put this Py_DECREF
in an #if defined(NPY_PY3K)
block, the line count of the code is going to be the same, and we spare ourselves a spurious Py_INCREF
/ Py_DECREF
pair in Python 2... Sorry for the noise.
…nting for multiple field names error message
@jaimefrio I hope the latest commit is good to go. I removed the extra |
"two fields with the same name"); | ||
#if defined(NPY_PY3K) | ||
name = PyUnicode_AsUTF8String(name); | ||
Py_DECREF(name); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will not work: once you decref next
, it could be garbage collected before you call PyString_AsString
on it. What I had in mind on my other comment was something like:
#if defined(NPY_PY3K)
name = PyUnicode_AsUTF8String(name);
#endif
PyErr_Format(PyExc_ValueError,
"field '%s' occurs more than once", PyString_AsString(name));
#if defined(NPY_PY3K)
Py_DECREF(next);
#endif
goto fail;
It ain't pretty, but neither was the previous version, and at least it puts all the weirdness in the Python 3 code path, and leaves the Python 2 one nice and clean.
Take a look at the comments I inlined with your code, I think that should be it, and we can merge. Thanks! |
@jaimefrio Thanks for your feedback. I have made the changes accordingly. |
BUG: Added field name to error message. Closes #6232
Thanks for your patience, Yash, in it goes. Seems to be your first contribution to NumPy: welcome, we hope to see many more! ;-) |
@jaimefrio Thanks Jaime. It was a good experience. Yes, its my first contibution, and yes, I plan to contribute more 😄 |
Changed error message (Added field name occurring more than once) as discussed in #6232