[Cython] 'Class' redeclared error if C++ class & file name match those of the wrapper class

Robert Bradshaw robertwb at gmail.com
Thu Oct 22 04:36:47 EDT 2015


On Wed, Oct 21, 2015 at 8:52 AM, Nathan Goldbaum <nathan12343 at gmail.com>
wrote:

> Hi all,
>
> I'm forwarding this at the request of David Nemesky ("savior" on
> Freenode), who I've been chatting with on the cython IRC channel. He was
> having trouble signing up to the cython-devel mailing list, so I've gone
> ahead and sent his bug report in for him. His report follows below.
>
> Apparently this error happens under very specific circumstances, but I
> managed to run into them. When wrapping a C++ class, Cython reports a
> "'ClassName' redeclared" error, if:
> - the name of the .pyx is the same as the name of the .pxd
> - the name of the python class is the same as the name of the C++ class
>
> Mismatch in either the file- or the class name results in a successful
> compilation.
>

Correct, this would result in the two objects having the same name in the
same namespace--you can't name the Python class the same as the C++ class
any more than you can have two C++ classes named the same thing in the same
namespace, or two typedefs named the same thing, etc.

The solution is to put it in a separate .pxd file and refer to it either
qualified with the module name or using an alias like "from ... cimport
CppClass as CppClass_c". You can also do
http://docs.cython.org/src/userguide/external_C_code.html#resolving-naming-conflicts-c-name-specifications


> I have modified my fork of the cython-cpp-test repo to make the issue
> easily reproducible. In https://github.com/DavidNemeskey/cython-cpp-test/,
> try `python setup.py build_ext --inplace` (do not forget to delete all .cpp
> and .so files between calls) in the following branches:
> - cython_bug (diff file, diff class) -> OK
> - diff_file_same_class -> OK
> - same_file_diff_class -> OK
> - same_file_same_class -> ERROR (https://bpaste.net/show/2c1af5fa5ee2)
>
> Since Cython compiles xxx.pyx to xxx.cpp in this case, in order to avoid
> problems with testclass.cpp being overwritten, I implemented the class in
> testclass.hpp (in the original repo, there was a .h and a .cpp file).
> However, implementing everything in the header is not a pre-requisite of
> the bug, as is evident from branch same_file_same_class_diff_c.
>
> _______________________________________________
> cython-devel mailing list
> cython-devel at python.org
> https://mail.python.org/mailman/listinfo/cython-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cython-devel/attachments/20151022/71054f5c/attachment.html>


More information about the cython-devel mailing list