-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
Enhance Object/structseq.c to match namedtuple and tuple api #46145
Comments
Raymond Hettinger wrote: Here's a couple more if you want to proceed down that path:
NOTE: |
Here is a patch for #1. I ran make test, and nothing was broken that Yes, it's small, it's my first one here. I'll get to the other two |
Thanks for the patch. I removed the whitespace changes and added some Am getting a sporadic segfault in test_zipimport when running "make test_zipimport |
I just svn upped (it updated zipimport) and applied your patch, and |
Run, "make test" a few times to make sure it doesn't bomb. The problem may be due to needing a deferred_type instead of assigning |
It worked fine on a fresh check-out. Committed in revision 59967. |
Is there something else to be done for this to be closed? |
All three items are still open. The second one is the easiest. |
See also bpo-2308 |
Jack, do you have any interest in putting this one over the goal line? |
In Py3.x, this fails: The reason is that PyUnicode_Format() expects a real tuple, not a tuple if (PyCheck_StructSeq(args)) {
newargs = PyTuple_FromSequence(args);
if (newargs == NULL)
return NULL;
result = PyUncode_Format(format, newargs);
Py_DECREF(newargs);
return result;
} |
Raymond Hettinger wrote:
-1 The special-casing of tuples vs. non-tuples for % is already What's so hard about writing "%s.%s.%s-%s-%s" % tuple(sys.version_info) anyway ? |
ISTM that structseq should have been a tuple subclass anyway. Isn't it |
For those who missed it, the patch that was committed in r59967 was "Temporarily revert 59967 until GC can be added." Raymond, can you please explain what was missing from the patch? |
See also bpo-8413, which would be addressed by this change. |
I agree that the priority is higher now that we have a demonstrable regression. Getting structseq to subclass from tuple will take some effort (tuples have many methods that would need to be overriden). |
structseq now does subclass tuple, so if there's any interest in adding namedtuple APIs, now it should be easier. |
Would whatever remains of this be deferred by the PEP-3003 moratorium? |
I don't think it would be covered by the moratorium, since it's not a language change. The change to make structseq derive from tuple was not subject to the moratorium, for example. |
This is definitely not covered by the language moratorium. Guido has requested this change and it needs to go forward. |
bpo-5907 would benefit of this change. I suggest to change the constructor to something equivalent to: |
Also see bpo-11698 |
New patch for 3.4 adds the following:
Now the issues:
This is my first real C patch. So some of the issues might just be a |
Oops, the correct issue for improving the repr is bpo-11698. |
See bpo-20230, which includes a patch implementing just the first part of part 2 (adding _fields, but not _asdict or _replace). Since this one has been open for 5+ years, if it's not going to be done soon, any chance of the easy (_fields) change being committed and putting off the harder parts until later? |
Good question. I don't think we can import OrderedDict from collections |
Yes, it is not an all-or-nothing exercise.
A regular dict would work just fine for now (there is a patch to introduce an OrderedDict in C, but that transition could be done later. |
Is accessing _fields a common operation? Personally I'd use a |
Not common at all -- it is used for introspection. That said, there is nearly zero savings from generating the tuple upon look-up. I would say that using a PyGetSetDef is a false optimization. |
Okay, if no one else wants this, I'll go ahead with the _fields part. Andrew, could you sign a contributor agreement? |
Hi Andrew. Did you by any chance sign the contributor agreement? [It's perfectly okay if you don't want to, but then we cannot use |
Hi Stefan, I've added a new patch which only adds _fields, combining parts from my earlier patch and Andrew's (his patch does not account for visible unnamed fields). |
Sunny, is there a definition of "visible positional fields"? Currently, For example, st_atime_ns is visible but not indexable: os.stat_result(st_mode=33188, st_ino=524840, st_dev=64513, st_nlink=1, st_uid=0, st_gid=0, st_size=2113, st_atime=1401225421, st_mtime=1398786163, st_ctime=1398786163)
>>> x[9]
1398786163
>>> x.st_atime_ns
1401225421887116783
>>> x[10]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range |
Hi Stefan, There is a comment at the top in structseq.c /* Fields with this name have only a field index, not a field name.
They are only allowed for indices < n_visible_fields. */
char *PyStructSequence_UnnamedField = "unnamed field"; This is the definition of os.stat_result in posixmodule.c: static PyStructSequence_Field stat_result_fields[] = { By visible i mean the values present in the repr of the object and by-extension accessible by position. I talked about my problems with os.stat_result in points 3 and 4 of msg202333 i.e repr of os.stat_result takes the first three keys from the attribute-access only fields (the invisible part) and uses them for the last three values in the displayed structseq. With my current patch, _fields for os.stat_result only has 7 values: >>> x = os.stat('.')
>>> x._fields
('st_mode', 'st_ino', 'st_dev', 'st_nlink', 'st_uid', 'st_gid', 'st_size')
>>> Is this what you expect? |
Hi, Stephan. Sorry, for some reason Yahoo was sending updates from the tracker to spam again, so I missed this. I'd be glad to sign a contributor agreement if it's still relevant, but it looks like there's a later patch that does what mine did and more? |
Sorry, Stefan, not Stephan. Anyway, I've signed the agreement. |
Andrew, thanks for signing the agreement! [Sunny]
I find the initialization in os.stat_result somewhat strange. Also, Apply structseq_repr_issue.diff, then: >>> from _testcapi import mk_structseq
>>> s = mk_structseq("unnamed_end")
>>> s[3]
3
>>> tuple(s)
(0, 1, 2, 3, 4)
>>> s
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
SystemError: In structseq_repr(), member 3 name is NULL for type _testcapi.struct_unnamed_end Perhaps we should just add the missing field names, like namedtuple (a=1, b=2, c=3, _4=4, _5=5) In any case, in 2.5 the entire tuple was printed as the repr, |
I just wondered if this project still had life in it. I came across an issue with structseqs and pandas. pandas will automatically apply column names based on the names of fields in a sequence of tuples, however, it uses the |
#108751 will make adding
|
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
_fields
,_field_defaults
, and_asdict
toPyStructSequence
#108648The text was updated successfully, but these errors were encountered: