Boost C++ Libraries: Ticket #2744: boost::python does not support enums with duplicated values. https://svn.boost.org/trac10/ticket/2744 <p> boost::python does not support enums with duplicated values. </p> <p> The proposed patch do the following: </p> <p> If the enum does not have duplicated values, all remains the same (the "values" attribute is a dict of value=&gt;enum_type). If it has duplicated values, the duplicated values point to a tuple of enum_types in the values attr dict. </p> <p> Remarks: </p> <ul><li>Maybe I should use lists instead of tuples to avoid the list &lt;=&gt; tuple conversion </li><li>I did not use tuples for all entries in the values attribute to keep the source compatibility. </li></ul> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2744 Trac 1.4.3 hugo.lima@… Tue, 10 Feb 2009 16:59:46 GMT attachment set https://svn.boost.org/trac10/ticket/2744 https://svn.boost.org/trac10/ticket/2744 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">enum_with_duplicated_values.patch</span> </li> </ul> <p> Patch to fix the problem </p> Ticket Dave Abrahams Tue, 10 Feb 2009 17:14:19 GMT type changed https://svn.boost.org/trac10/ticket/2744#comment:1 https://svn.boost.org/trac10/ticket/2744#comment:1 <ul> <li><strong>type</strong> <span class="trac-field-old">Bugs</span> → <span class="trac-field-new">Patches</span> </li> </ul> <p> Are you saying that changing the code to use tuples even when there are no duplicates would break backward compatibility with existing user code? How so? </p> Ticket hugo.lima@… Tue, 10 Feb 2009 19:29:48 GMT <link>https://svn.boost.org/trac10/ticket/2744#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2744#comment:2</guid> <description> <p> The code: </p> <p> for x in <a class="missing wiki">SomeEnum</a>.values: </p> <blockquote> <p> # do something with x, because I know that it is a int print x+2 </p> </blockquote> <p> because the values attribute is public. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dave Abrahams</dc:creator> <pubDate>Tue, 10 Feb 2009 19:39:14 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2744#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2744#comment:3</guid> <description> <p> Then maybe a better solution is to add a public "names" dict that maps name -&gt; value? </p> </description> <category>Ticket</category> </item> <item> <author>hugo.lima@…</author> <pubDate>Tue, 10 Feb 2009 19:59:01 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2744#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2744#comment:4</guid> <description> <p> Yeah, but I did not want to change the API or the current behaviour of the current boost::python based bindings with this patch. IMHO, values attribute should be a dict that maps name-&gt;value. But this will change the current API of all enums generated by boost python. </p> <p> Adding a new "name" attribute without any real usecase may only increase the memory footprint with this dict. So, not changing the API and fixing a bug is good enough for now :-). What you think about? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dave Abrahams</dc:creator> <pubDate>Tue, 10 Feb 2009 21:47:33 GMT</pubDate> <title>status changed https://svn.boost.org/trac10/ticket/2744#comment:5 https://svn.boost.org/trac10/ticket/2744#comment:5 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> <p> ?? The usecase is supporting your enum with multiple names for the same value </p> <p> The memory footprint of this additional dict will be tiny. </p> <p> The problem with the change as proposed is that it <em>can</em> break user code, if users are counting on "values" containing single int values, because the person using an enum is not always the same as the person exporting it. </p> Ticket hugo.lima@… Wed, 11 Feb 2009 12:06:38 GMT <link>https://svn.boost.org/trac10/ticket/2744#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2744#comment:6</guid> <description> <p> I can leave the values attribute intact and create another temporary attribute, just to help the enum declaration, like the current values attribute is used, and remove then after the declaration of all enums. what you think? </p> <p> Results: </p> <ul><li>Do not change the current API, so do not break user code, values map to a single enum_type ever, not a tuple. </li><li>We have the bug fixed. </li><li>The same memory footprint. </li></ul> </description> <category>Ticket</category> </item> <item> <dc:creator>Dave Abrahams</dc:creator> <pubDate>Wed, 11 Feb 2009 14:57:13 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2744#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2744#comment:7</guid> <description> <p> No, what I want is to increase the memory footprint by one dict per enum declaration. The names attribute would be useful in general. </p> </description> <category>Ticket</category> </item> <item> <author>hugo.lima@…</author> <pubDate>Wed, 11 Feb 2009 17:23:35 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2744#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2744#comment:8</guid> <description> <p> Ok, so I must keep values attribute as is and add the names attribute? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dave Abrahams</dc:creator> <pubDate>Wed, 11 Feb 2009 18:19:31 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2744#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2744#comment:9</guid> <description> <p> That's the change that makes sense to me </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Wed, 11 Feb 2009 21:08:24 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2744#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2744#comment:10</guid> <description> <p> Ok, I'll create a new patch and post it here until next weekend, because I'm too busy now </p> </description> <category>Ticket</category> </item> <item> <author>hugo.lima@…</author> <pubDate>Wed, 11 Feb 2009 22:41:05 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/2744 https://svn.boost.org/trac10/ticket/2744 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">enum_with_duplicated_values_v2.patch</span> </li> </ul> <p> Create a new attribute called "names" that maps the name of enum to their value and fix the "enum with duplicated values" bug. </p> Ticket hugo.lima@… Wed, 11 Feb 2009 23:33:06 GMT <link>https://svn.boost.org/trac10/ticket/2744#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2744#comment:11</guid> <description> <p> patch done, any comments? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dave Abrahams</dc:creator> <pubDate>Thu, 12 Feb 2009 15:17:26 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2744#comment:12 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2744#comment:12</guid> <description> <p> According to <a class="ext-link" href="http://trac.edgewall.org/ticket/8047#comment:2"><span class="icon">​</span>http://trac.edgewall.org/ticket/8047#comment:2</a> your patch is ill-formed. Could you please replace it with a well-formed one? </p> </description> <category>Ticket</category> </item> <item> <author>hugo.lima@…</author> <pubDate>Thu, 12 Feb 2009 16:23:38 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/2744 https://svn.boost.org/trac10/ticket/2744 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">enum_with_duplicated_values_v2.2.patch</span> </li> </ul> Ticket hugo.lima@… Thu, 12 Feb 2009 16:25:36 GMT <link>https://svn.boost.org/trac10/ticket/2744#comment:13 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2744#comment:13</guid> <description> <p> the last version is the output of svn diff, with trailing spaces removals, blank spaces removals, etc... </p> </description> <category>Ticket</category> </item> <item> <author>hugo.lima@…</author> <pubDate>Tue, 17 Feb 2009 11:08:02 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2744#comment:14 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2744#comment:14</guid> <description> <p> any comments? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dave Abrahams</dc:creator> <pubDate>Sun, 15 Mar 2009 03:57:57 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2744#comment:15 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2744#comment:15</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/2744#comment:14" title="Comment 14">hugo.lima@openbossa.org</a>: </p> <blockquote class="citation"> <p> any comments? </p> </blockquote> <p> Looks great, but before I can check it in I need a testcase that fails unless this patch is applied. Do you think you could whip one up? A patch to the existing enum test case would be fine. </p> </description> <category>Ticket</category> </item> <item> <author>hugo.lima@…</author> <pubDate>Tue, 17 Mar 2009 17:29:42 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2744#comment:16 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2744#comment:16</guid> <description> <p> Ok, I'll do it. </p> </description> <category>Ticket</category> </item> <item> <author>hugo.lima@…</author> <pubDate>Tue, 17 Mar 2009 18:50:51 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/2744 https://svn.boost.org/trac10/ticket/2744 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">enum_with_duplicated_values_unittest.patch</span> </li> </ul> <p> Patch to the unit test </p> Ticket hugo.lima@… Tue, 17 Mar 2009 19:24:53 GMT <link>https://svn.boost.org/trac10/ticket/2744#comment:17 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2744#comment:17</guid> <description> <p> Patch to the unit test added, it will fail because color.blood will overwrite color.red. the test just do not test if the blood has been declared. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dave Abrahams</dc:creator> <pubDate>Tue, 17 Mar 2009 20:20:31 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2744#comment:18 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2744#comment:18</guid> <description> <p> The new test seems to fail even after application of the patch v2.2: </p> <div class="wiki-code"><div class="code"><pre><span class="n">running</span><span class="o">...</span> <span class="o">**********************************************************************</span> <span class="n">File</span> <span class="s2">&quot;enum.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">7</span><span class="p">,</span> <span class="ow">in</span> <span class="n">__main__</span> <span class="n">Failed</span> <span class="n">example</span><span class="p">:</span> <span class="n">identity</span><span class="p">(</span><span class="n">color</span><span class="o">.</span><span class="n">red</span><span class="p">)</span> <span class="n">Expected</span><span class="p">:</span> <span class="n">enum_ext</span><span class="o">.</span><span class="n">color</span><span class="o">.</span><span class="n">red</span> <span class="n">Got</span><span class="p">:</span> <span class="n">enum_ext</span><span class="o">.</span><span class="n">color</span><span class="o">.</span><span class="n">blood</span> <span class="o">**********************************************************************</span> <span class="n">File</span> <span class="s2">&quot;enum.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">16</span><span class="p">,</span> <span class="ow">in</span> <span class="n">__main__</span> <span class="n">Failed</span> <span class="n">example</span><span class="p">:</span> <span class="n">identity</span><span class="p">(</span><span class="n">color</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span> <span class="n">Expected</span><span class="p">:</span> <span class="n">enum_ext</span><span class="o">.</span><span class="n">color</span><span class="o">.</span><span class="n">red</span> <span class="n">Got</span><span class="p">:</span> <span class="n">enum_ext</span><span class="o">.</span><span class="n">color</span><span class="o">.</span><span class="n">blood</span> <span class="o">**********************************************************************</span> <span class="n">File</span> <span class="s2">&quot;enum.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">30</span><span class="p">,</span> <span class="ow">in</span> <span class="n">__main__</span> <span class="n">Failed</span> <span class="n">example</span><span class="p">:</span> <span class="n">identity</span><span class="p">(</span><span class="n">red</span><span class="p">)</span> <span class="ne">Exception</span> <span class="n">raised</span><span class="p">:</span> <span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span> <span class="n">File</span> <span class="s2">&quot;/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/doctest.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1212</span><span class="p">,</span> <span class="ow">in</span> <span class="n">__run</span> <span class="n">compileflags</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="ow">in</span> <span class="n">test</span><span class="o">.</span><span class="n">globs</span> <span class="n">File</span> <span class="s2">&quot;&lt;doctest __main__[8]&gt;&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1</span><span class="p">,</span> <span class="ow">in</span> <span class="o">&lt;</span><span class="n">module</span><span class="o">&gt;</span> <span class="n">identity</span><span class="p">(</span><span class="n">red</span><span class="p">)</span> <span class="ne">NameError</span><span class="p">:</span> <span class="n">name</span> <span class="s1">&#39;red&#39;</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">defined</span> <span class="o">**********************************************************************</span> <span class="n">File</span> <span class="s2">&quot;enum.py&quot;</span><span class="p">,</span> <span class="n">line</span> <span class="mi">44</span><span class="p">,</span> <span class="ow">in</span> <span class="n">__main__</span> <span class="n">Failed</span> <span class="n">example</span><span class="p">:</span> <span class="n">c</span><span class="o">.</span><span class="n">x</span> <span class="n">Expected</span><span class="p">:</span> <span class="n">enum_ext</span><span class="o">.</span><span class="n">color</span><span class="o">.</span><span class="n">red</span> <span class="n">Got</span><span class="p">:</span> <span class="n">enum_ext</span><span class="o">.</span><span class="n">color</span><span class="o">.</span><span class="n">blood</span> <span class="o">**********************************************************************</span> <span class="mi">1</span> <span class="n">items</span> <span class="n">had</span> <span class="n">failures</span><span class="p">:</span> <span class="mi">4</span> <span class="n">of</span> <span class="mi">20</span> <span class="ow">in</span> <span class="n">__main__</span> <span class="o">***</span><span class="n">Test</span> <span class="n">Failed</span><span class="o">***</span> <span class="mi">4</span> <span class="n">failures</span><span class="o">.</span> </pre></div></div><p> Note that you can replace existing patches so obsolete ones don't pile up in the ticket. </p> </description> <category>Ticket</category> </item> <item> <author>hugo.lima@…</author> <pubDate>Wed, 18 Mar 2009 21:05:03 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/2744 https://svn.boost.org/trac10/ticket/2744 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">enum_with_duplicated_values_v2.3.patch</span> </li> </ul> <p> Patch to enum.cpp and their unit tests </p> Ticket anonymous Wed, 18 Mar 2009 21:10:41 GMT <link>https://svn.boost.org/trac10/ticket/2744#comment:19 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2744#comment:19</guid> <description> <p> I dont have enough privileges to replace existing patches, maybe if I create a trac account. </p> <p> I fixed the original patch, so enums are not integers and the tests do not fail. Just one issue, when I cast something to a enum, if the enum value is duplicated, it will ever return the last declared value. so I replaced the color.red by color.blood in the python test script. But that's not a problem, because color.red == color.blood and hash(color.red) == hash(color.blood). </p> </description> <category>Ticket</category> </item> <item> <author>anderson.lizardo@…</author> <pubDate>Mon, 23 Mar 2009 19:20:10 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2744#comment:20 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2744#comment:20</guid> <description> <p> Any other comments on the latest patch (v2.3) ? I'm currently using it and it seems to work without problems. </p> </description> <category>Ticket</category> </item> <item> <author>hugo.lima@…</author> <pubDate>Fri, 08 May 2009 14:24:50 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/2744#comment:21 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2744#comment:21</guid> <description> <p> ping </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Dave Abrahams</dc:creator> <pubDate>Fri, 05 Jun 2009 21:18:16 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/2744#comment:22 https://svn.boost.org/trac10/ticket/2744#comment:22 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/53660" title="Allow duplicate enum values. Fixes #2744 Thanks to ...">[53660]</a>) Allow duplicate enum values. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2744" title="#2744: Patches: boost::python does not support enums with duplicated values. (closed: fixed)">#2744</a> Thanks to hugo.lima@… </p> Ticket