-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
bpo-29696 Use structseq in string.Formatter.parse iterator response #3980
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
Conversation
rhettinger
left a comment
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.
Can you please add tests.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
|
I have made the requested changes; please review again |
|
Thanks for making the requested changes! @rhettinger: please review the changes made to this pull request. |
facundobatista
left a comment
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.
Thanks for this work!
Patch applied OK and tests run fine.
I annotated some changes to do.
Thanks again!!
Lib/test/test_unicode.py
Outdated
| self.assertEqual(formatter.n_sequence_fields, 4) | ||
| self.assertNotEqual(formatter.__class__,tuple) | ||
| self.assertIsInstance(formatter,tuple) | ||
| self.assertEqual(formatter.__class__.__name__,"FormatterItem") |
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.
Please comply PEP-8 in these last three lines.
| @@ -0,0 +1,2 @@ | |||
| Use namedtuple (structseq ovbject) in string.Formatter.parse iterator | |||
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.
typo in "ovbject"
Objects/stringlib/unicode_format.h
Outdated
| PyObject *format_spec_str = NULL; | ||
| PyObject *conversion_str = NULL; | ||
| PyObject *tuple = NULL; | ||
| PyObject* res = NULL; |
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.
please follow the convention and write PyObject *res
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
|
I have made the requested changes; please review again. |
|
Thanks for making the requested changes! @rhettinger, @facundobatista: please review the changes made to this pull request. |
Objects/stringlib/unicode_format.h
Outdated
|
|
||
| tuple = PyTuple_Pack(4, literal_str, field_name_str, format_spec_str, | ||
| conversion_str); | ||
| Py_XINCREF(literal_str); |
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.
None of these pointers is NULL, isn't?
And why increment refcounters of non-borrowed references and later decrement them?
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.
Fixed in 814cb3b. That's right, none of the pointers is NULL. The reason for the increment and decrement is because I was getting segfaults so I tried to use idempotent incref-decref mirroring the ones that happen in PyTuple_Pack and after to check if any of the segfaults was due to incorrect incref/decref. I just removed both the increfs and the decrefs and everything is OK. Sorry for the confusion.
Objects/stringlib/unicode_format.h
Outdated
| static PyTypeObject FormatterIterResultType; | ||
|
|
||
| static PyStructSequence_Desc formatter_iter_result_desc = { | ||
| "FormatterItem", |
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.
Shouldn't a module name be added?
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.
Sorry, I am confused. Do you mean to expose the type in a module or to add a doc to the second field?
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.
Shouldn't the name of the type include the name of the module?
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.
Done in 3ee0266.
Lib/test/test_unicode.py
Outdated
| self.assertEqual(formatter, expected_formatter) | ||
|
|
||
| for result, expected in zip(formatter, expected_formatter): | ||
| self.assertEqual(expected, |
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.
First argument is an actual value, second argument is an expected value.
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.
Fixed in 814cb3b.
Lib/test/test_unicode.py
Outdated
| (result.literal_text, | ||
| result.field_name, | ||
| result.format_spec, | ||
| result.conversion,)) |
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.
Redundant trailing comma.
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.
Fixed in 814cb3b.
| @@ -0,0 +1,2 @@ | |||
| Use namedtuple (structseq object) in string.Formatter.parse iterator | |||
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.
"named tuple"
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.
Fixed in 814cb3b.
Lib/test/test_unicode.py
Outdated
| result.field_name, | ||
| result.format_spec, | ||
| result.conversion) | ||
| ,expected) |
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.
Oh, this comma looks just ugly.
I would use 4 separate tests:
self.assertEqual(result.literal_text, expected[0])
...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.
Done in ff5651c.
Objects/stringlib/unicode_format.h
Outdated
| Py_XDECREF(format_spec_str); | ||
| Py_XDECREF(conversion_str); | ||
| return tuple; | ||
|
|
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.
There is a leak in case of error.
Decrefs are needed only in case of error. If values are set to a named tuple, they are not needed.
Rename done to error and add return before it.
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.
Done in ff5651c.
| conversion_str); | ||
| PyStructSequence_InitType(&FormatterIterResultType, &formatter_iter_result_desc); | ||
| Py_INCREF((PyObject *) &FormatterIterResultType); | ||
| res = PyStructSequence_New(&FormatterIterResultType); |
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.
Check error.
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.
Done in ff5651c.
Objects/stringlib/unicode_format.h
Outdated
|
|
||
| tuple = PyTuple_Pack(4, literal_str, field_name_str, format_spec_str, | ||
| conversion_str); | ||
| PyStructSequence_InitType(&FormatterIterResultType, &formatter_iter_result_desc); |
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.
Wait, do you initialize the type every time when create an instance?
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.
Sorry, I cannot find the appropriate PyInit_{module} function to initialize the type so at this point I was doing it here so there is some implementation to discuss. My apologies 😞 . Where should I initialize the StructSeq?
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.
Sorry, I just found where _string is initialized and I am initializing the type in PyInit__string in f10d429. Please, correct me if this is not the correct place.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
|
I have made the requested changes; please review again |
|
Thanks for making the requested changes! @serhiy-storchaka, @facundobatista, @rhettinger: please review the changes made to this pull request. |
serhiy-storchaka
left a comment
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.
Just added yet few nitpicks.
But actually I'm not convinced that this change is needed.
Objects/stringlib/unicode_format.h
Outdated
| Py_XDECREF(format_spec_str); | ||
| Py_XDECREF(conversion_str); | ||
| return tuple; | ||
| return res; |
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.
Just return NULL. And initializing res with NULL at line 1035 is not needed.
| PyStructSequence_SET_ITEM(res, 3, conversion_str); | ||
|
|
||
| return res; | ||
| error: |
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.
It would look cleaner if add an empty line before a label.
Objects/stringlib/unicode_format.h
Outdated
| {"literal_text", "Span of literal text."}, | ||
| {"field_name", "Specifies the object whose value is to be formatted."}, | ||
| {"format_spec", "Contains a specification of how the value should be presented."}, | ||
| {"conversion", "The conversion to be used. One of: ‘s’ (str), ‘r’ (repr) and ‘a’ (ascii)."}, |
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 line looks too long. And maybe the previous line too.
|
IMHO this change makes things a bit more consistent. In lots of places when a tuple is returned, a
|
|
@rhettinger hello! This PR now has some tests.. are these enough for you? I just run all the tests, so if you're we could merge this... |
This PR solves bpo-29696 improving the readability of the return value of
Formatter.parse. Following the requirements of @rhettinger and @serhiy-storchaka, the c-code upstream of thestringlibrary was modified to maintain thestringmodule lightweight. This also improves thehelpsection of the result class with a brief description of the result fields.Now, instead of:
we obtain
Please, indicate any change that is needed and I will be more than happy to change it. 😄
https://bugs.python.org/issue29696