classification
Title: Enhance Object/structseq.c to match namedtuple and tuple api
Type: enhancement Stage:
Components: Interpreter Core Versions: Python 3.3
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: adlaiff6, amaury.forgeotdarc, belopolsky, benjamin.peterson, christian.heimes, eric.araujo, eric.smith, eric.snow, georg.brandl, giampaolo.rodola, gjb1002, jackdied, lemburg, rhettinger, salty-horse, santa4nt, terry.reedy
Priority: normal Keywords: easy

Created on 2008-01-14 04:20 by christian.heimes, last changed 2012-06-25 01:05 by eric.snow.

Files
File name Uploaded Description Edit
structseq_subclasses_tuple.diff adlaiff6, 2008-01-14 07:03 patch which makes structseq a subclass of tuple review
structseq.diff rhettinger, 2008-01-15 01:21 Tuple subclass patch with tests.
Messages (24)
msg59888 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-01-14 04:20
Raymond Hettinger wrote:

Here's a couple more if you want to proceed down that path:

1. Have structseq subclass from PyTupleObject so that isinstance(s,
tuple) returns True.  This makes the object usable whenever 
tuples are needed.

2. Add _fields, _asdict, and _replace to match the API in
collections.namedtuple().  The _fields tuple should only include the 
visible positional fields while _asdict() and _replace() should include
all of the fields whether visible or accessible only by 
attribute access.

3. Change the constructor to accept keyword args so that eval(repr(s))
== s works.

NOTE:
I've marked the task as easy but it's not a task for a total newbie.
It's a feasible yet challenging task for somebody who likes to get into
CPython core programming. Basic C knowledge is required!
msg59891 - (view) Author: Leif Walsh (adlaiff6) Date: 2008-01-14 07:03
Here is a patch for #1.  I ran make test, and nothing was broken that
seemed to be my fault, so I assume it's okay.

Yes, it's small, it's my first one here.  I'll get to the other two
tomorrow.
msg59949 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-01-15 01:21
Thanks for the patch.  I removed the whitespace changes and added some
tests to make sure structseq now works with the % formatting operator
and isinstance(t,tuple).

Am getting a sporadic segfault in test_zipimport when running "make
test", so holding-off on applying:

test_zipimport
~/py26/Lib/test/test_zipimport.py:91: ImportWarning: Not importing
directory '/home/rhettinger/py26/Modules/zlib': missing __init__.py
  ["__dummy__"])
make: *** [test] Segmentation fault
msg59955 - (view) Author: Leif Walsh (adlaiff6) Date: 2008-01-15 02:01
I just svn upped (it updated zipimport) and applied your patch, and
'./python Lib/test/regrtest.py test_zipimport.py' says it's okay, so I
would go ahead and commit it.
msg59956 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-01-15 02:05
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
&PyTupleObject directly.  Will look it more later.
msg59957 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-01-15 03:03
It worked fine on a fresh check-out.  Committed in revision 59967.
msg62830 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-02-23 22:49
Is there something else to be done for this to be closed?
msg62831 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-02-23 22:54
All three items are still open.  The second one is the easiest.
msg81627 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-02-11 04:46
See also issue 2308
msg85097 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-04-01 21:16
Jack, do you have any interest in putting this one over the goal line?
msg90461 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-07-12 22:48
In Py3.x, this fails:
    "%s.%s.%s-%s-%s" % sys.version_info

The reason is that PyUnicode_Format() expects a real tuple, not a tuple
lookalike.  The fix is to either have structseq inherit from tuple or to
modify PyUnicode_Format() to handle structseq:

   if (PyCheck_StructSeq(args)) {
      newargs = PyTuple_FromSequence(args);
      if (newargs == NULL)
          return NULL;
      result = PyUncode_Format(format, newargs);
      Py_DECREF(newargs);
      return result;
   }
msg90472 - (view) Author: Marc-Andre Lemburg (lemburg) * (Python committer) Date: 2009-07-13 08:56
Raymond Hettinger wrote:
> Raymond Hettinger <rhettinger@users.sourceforge.net> added the comment:
> 
> In Py3.x, this fails:
>     "%s.%s.%s-%s-%s" % sys.version_info
> 
> The reason is that PyUnicode_Format() expects a real tuple, not a tuple
> lookalike.  The fix is to either have structseq inherit from tuple or to
> modify PyUnicode_Format() to handle structseq:
> 
>    if (PyCheck_StructSeq(args)) {
>       newargs = PyTuple_FromSequence(args);
>       if (newargs == NULL)
>           return NULL;
>       result = PyUncode_Format(format, newargs);
>       Py_DECREF(newargs);
>       return result;
>    }

-1

The special-casing of tuples vs. non-tuples for % is already
bad enough. Adding structseq as another special case doesn't
make that any better.

What's so hard about writing

"%s.%s.%s-%s-%s" % tuple(sys.version_info)

anyway ?
msg90506 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-07-13 22:01
ISTM that structseq should have been a tuple subclass anyway.  Isn't it
advertised as some sort of "tuple with attribute access"?
msg90953 - (view) Author: Ori Avtalion (salty-horse) Date: 2009-07-26 18:03
For those who missed it, the patch that was committed in r59967 was
quickly reverted in r59970 with the comment:

"Temporarily revert 59967 until GC can be added."

Raymond, can you please explain what was missing from the patch?
msg104559 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-04-29 18:21
See also issue 8413, which would be addressed by this change.
msg104562 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-04-29 18:34
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).
msg109503 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2010-07-07 21:31
structseq now does subclass tuple, so if there's any interest in adding namedtuple APIs, now it should be easier.
msg113451 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-08-09 18:50
Would whatever remains of this be deferred by the PEP3003 moratorium?
msg113453 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2010-08-09 18:55
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.
msg113456 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-08-09 19:02
This is definitely not covered by the language moratorium.  Guido has requested this change and it needs to go forward.
msg133351 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-04-08 23:02
Issue5907 would benefit of this change.
Unfortunately, structseq constructors already have keyword arguments; they are equivalent to "def __new__(cls, sequence, dict=NULL)".
OTOH these keywords arguments are not documented anywhere.

I suggest to change the constructor to something equivalent to:
"def __new__(cls, sequence=NULL, dict=NULL, *, field1=NULL,  field2=NULL)"
msg133355 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-04-08 23:23
Also see issue 11698
msg133364 - (view) Author: Alexander Belopolsky (belopolsky) * (Python committer) Date: 2011-04-09 00:53
Hasn't this been fixed in the following changeset?

changeset:   43509:384f73a104e9
user:        Benjamin Peterson <benjamin@python.org>
date:        Wed Jul 07 20:54:01 2010 +0000
summary:     make struct sequences subclass tuple; kill lots of code
msg133367 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2011-04-09 01:02
> Hasn't this been fixed in the following changeset?

It was a major step forward.  Now there needs to be work on other namedtuple methods and whatnot.
History
Date User Action Args
2012-06-25 01:05:44eric.snowsetnosy: + eric.snow
2011-07-13 15:42:21eric.araujosetnosy: + eric.araujo
2011-04-20 22:17:48rhettingerunlinkissue7796 dependencies
2011-04-16 20:00:37santa4ntsetversions: + Python 3.3, - Python 3.2
2011-04-09 01:02:30rhettingersetmessages: + msg133367
2011-04-09 00:53:03belopolskysetnosy: + belopolsky
messages: + msg133364
2011-04-08 23:23:55rhettingersetassignee: jackdied -> rhettinger
messages: + msg133355
2011-04-08 23:02:42amaury.forgeotdarclinkissue5907 dependencies
2011-04-08 23:02:13amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg133351
2011-04-08 19:51:33santa4ntsetnosy: + santa4nt
2010-11-28 05:11:16eric.araujolinkissue7796 dependencies
2010-08-24 19:07:54gjb1002setnosy: + gjb1002
2010-08-09 19:02:00rhettingersetmessages: + msg113456
2010-08-09 18:55:12eric.smithsetmessages: + msg113453
2010-08-09 18:50:48terry.reedysetnosy: + terry.reedy

messages: + msg113451
versions: - Python 3.1, Python 2.7
2010-07-07 21:31:36benjamin.petersonsetnosy: + benjamin.peterson
messages: + msg109503
2010-06-02 22:50:09giampaolo.rodolasetnosy: + giampaolo.rodola
2010-04-29 18:34:17rhettingersetpriority: low -> normal

messages: + msg104562
2010-04-29 18:21:47eric.smithsetmessages: + msg104559
2010-04-29 17:27:09eric.smithsetnosy: + eric.smith
2009-07-26 18:03:04salty-horsesetmessages: + msg90953
2009-07-26 10:32:20salty-horsesetnosy: + salty-horse
2009-07-13 22:01:06georg.brandlsetmessages: + msg90506
2009-07-13 08:56:27lemburgsetnosy: + lemburg
title: Enhance Object/structseq.c to match namedtuple and tuple api -> Enhance Object/structseq.c to match namedtuple and tuple api
messages: + msg90472
2009-07-12 22:48:16rhettingersetmessages: + msg90461
versions: + Python 3.2
2009-04-01 21:16:27rhettingersetassignee: rhettinger -> jackdied

messages: + msg85097
nosy: + jackdied
2009-02-11 04:46:19rhettingersetmessages: + msg81627
2009-02-11 04:39:11ajaksu2linkissue2308 superseder
2009-01-02 21:49:34rhettingersetversions: + Python 3.1, Python 2.7, - Python 2.6, Python 3.0
2008-02-23 22:54:01rhettingersetmessages: + msg62831
2008-02-23 22:49:45georg.brandlsetnosy: + georg.brandl
messages: + msg62830
2008-01-15 03:03:57rhettingersetmessages: + msg59957
2008-01-15 02:05:38rhettingersetmessages: + msg59956
2008-01-15 02:01:05adlaiff6setmessages: + msg59955
2008-01-15 01:21:34rhettingersetfiles: + structseq.diff
messages: + msg59949
2008-01-15 00:33:03rhettingersetassignee: rhettinger
2008-01-14 07:03:20adlaiff6setfiles: + structseq_subclasses_tuple.diff
nosy: + adlaiff6
messages: + msg59891
2008-01-14 04:20:27christian.heimescreate