demisjohn / CAMFR

Python-based electromagnetic simulator and mode solver for nanophotonics applications, using the Eigenmode Expansion (EME) method.
GNU General Public License v2.0
65 stars 30 forks source link

Compile for Python 3.x #8

Open demisjohn opened 6 years ago

demisjohn commented 6 years ago

From @demisjohn on February 28, 2018 8:23

Will try Python 3.5 first since I have that installed.

Copied from original issue: demisjohn/CAMFR#4

demisjohn commented 6 years ago

Trying this on this branch: https://github.com/demisjohn/CAMFR/tree/py35_compat

demisjohn commented 6 years ago

Stuff To fix:

camfr/camfr_wrap.cpp:580:3: error: void function 'init_module__camfr' should not return a
      value [-Wreturn-type]
  import_array();
  ^~~~~~~~~~~~~~
/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/numpy/core/include/numpy/__multiarray_api.h:1547:144: note: 
      expanded from macro 'import_array'
  ..."numpy.core.multiarray failed to import"); return NUMPY_IMPORT_ARRAY_RETVAL; } }
                                                ^      ~~~~~~~~~~~~~~~~~~~~~~~~~

See these links for fixes: https://github.com/pandas-dev/pandas/issues/3872 https://github.com/tensorflow/tensorflow/issues/733 Googled : https://www.google.com/search?client=safari&rls=en&q=%22should+not+return+a+value%22+return+NUMPY_IMPORT_ARRAY_RETVAL&ie=UTF-8&oe=UTF-8

demisjohn commented 6 years ago

After fixing "TabErrors", compiling (and failing due to above numpy issue), ahve a bunch of indentation errors! Numerous def and if statements are now incorrectly indented. Some of those might only pop errors when the functions are executed during usage, not during compilation...

byte-compiling visualisation/camfr_PIL.py to camfr_PIL.cpython-35.pyc
Sorry: IndentationError: expected an indented block (camfr_PIL.py, line 672)
byte-compiling visualisation/camfr_matlab.py to camfr_matlab.cpython-35.pyc
Sorry: IndentationError: expected an indented block (camfr_matlab.py, line 203)
byte-compiling visualisation/camfr_tk.py to camfr_tk.cpython-35.pyc
Sorry: IndentationError: expected an indented block (camfr_tk.py, line 312)
byte-compiling visualisation/TkPlotCanvas.py to TkPlotCanvas.cpython-35.pyc
Sorry: IndentationError: expected an indented block (TkPlotCanvas.py, line 187)

Looks like the original files will randomly switch between using Tabs and four-spaces, even within a single function. Sounds like Py27 graciously glossed this over, but this is now an error in py3.x

demisjohn commented 6 years ago

@demisjohn: Run 'diff' on py35 and py27 source files, look for indentation mistakes (I may have introduced some)

Do the diff in GitHub: https://stackoverflow.com/questions/43552274/how-can-i-diff-two-branches-in-github

Here's the diff link: https://github.com/demisjohn/CAMFR/compare/master...demisjohn:py35_compat

demisjohn commented 6 years ago

Corrected indentations to match old master version, in 7e1224c16297561ac468f142593d6b534a11e0d5

Some of the loops and function defs had been incorrectly indented, hopefully they are fixed. Have NOT yet run tests to confirm they're all ok.

demisjohn commented 6 years ago

Regarding numpy error, it seems that, new in Python3, numpy's C library returns an int whereas it used to return void. This is supposedly ocurring in the camfrwrap.cpp file, where it creates some sort of wrapper/pointer to the numpy C code (?). Some suggestions were to add a conditional into the wrap.cpp file that defines our func as void/int depending on whether it's being compiled for py2x/py3x.
I suppose it would be good for the library to be cross-compatible with python2/3, but I am heavily leaning towards just dropping support for 2.x entirely.

Let me know if there's some pressing reason we should still support python 2.x.

kitchenknif commented 6 years ago

Don't see any real reason on maintaining Python 2.x support.

Maybe do a final 2.x-compatible release, and then drop 2.x?

demisjohn commented 6 years ago

Online articles indicate that one should really just move to Python3.x, so i will try to do that.

demisjohn commented 6 years ago

Good idea @kitchenknif , I'll make a "release" for py2.x and call it done. May keep py2.7 alive for a short while longer as I try to get actual work done with CAMFR before the py3.x version is actually working.

jsenellart commented 2 years ago

just to keep track on this issue about the last changes for CAMFR users:

one single test unit is not passing:

======================================================================
FAIL: testbackward (__main__.backward)
backward
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/senellart/DEV/3rdParty/CAMFR/testsuite/backward.py", line 48, in testbackward
    self.assertTrue(R_pass and T_pass)
AssertionError: False is not true

----------------------------------------------------------------------

I will complete with additional cleanup on setup.py - on from if the last test unit passes, from my perspective, this branch will be fully functional.