boskee / Minecraft

Simple Minecraft-inspired program using Python and Pyglet
MIT License
207 stars 33 forks source link

Update project to Python3 (2.7 -> 3.6) #91

Closed Nebual closed 5 years ago

Nebual commented 5 years ago

May want to review commit-by-commit.

The 2to3 tool handled most of the quick replacements, however there were 2 main areas it wasn't able to detect which required some care:

  1. / -> // when we specifically want integer math. In Python2, 5 / 2 == 2, whereas 5 / 2.0 == 2.5. As of Python3, / always means float math, and // always means integer math - but lots of our code was relying on resulting ints (and Pyglet would blow up upon being given floats...)
  2. Handling of str vs bytes, especially relevant as this project makes heavy use of raw bytes operations in both the saving/loading and networking.

Additionally, as Python3.5 added support for (compile-time) type hints, I added a number of them to help clarify the distinction between str and bytes. Cython uses those type hints instead of needing a separate .pyd definitions file, which seems nice.

Everything appears to work about as well as on Python2, with likely a few bugs/inaccuracies fixed due to Python3's generally stricter handling, and possibly a few new crashes caused by that strictness.

As py2exe doesn't seem to support the latest Python3.x, I've updated setup.py to use cx_Freeze, which can also produce all-dependencies-bundled zips, helping #71 along

Closes #87 Closes #78

r58Playz commented 5 years ago

Great! I'm tired of typing C:/Python27/python.exe as I have both python 2.7 and python 3.7 installed.

r58Playz commented 5 years ago

There still are some errors, though.

Nebual commented 5 years ago

There still are some errors, though.

Can you be more specific?

This project is rather stale and had several issues on master, so this branch is focused on specifically Python3 related errors, I ignored the "crash on block break due to audio" for example as that happened on python2 (though I then fixed it on master, so git checkout python3 && git merge origin/master should fix that).

r58Playz commented 5 years ago

Like a TypeError when trying to place a block, and lots of server errors while trying to handle another server error.

I can fix the block-break animations, though.

r58Playz commented 5 years ago

The crash on block break due to audio just needs a rename:

pyglet.audio.ManagedSoundPlayer to pyglet.audio.Player

r58Playz commented 5 years ago

I guess the errors are happening because I'm using Python 3.7.

Does this work for Python 3.7?

Nebual commented 5 years ago

I patched ManagedSoundPlayer on master, though I was still getting periodic segfaults, so I disabled audio.

Python 3.7 works for me, though I did accidentally try loading the python3 branch with python2, resulting in a corrupted save file that I then had to manually delete. https://pyglet.readthedocs.io/en/pyglet-1.2-maintenance/api/pyglet/resource/pyglet.resource.get_settings_path.html shows where the saves are.

r58Playz commented 5 years ago

I just deleted the get_settings_path part and set it to a folder called "pyCraft".

r58Playz commented 5 years ago

Oh, and the TypeError I was getting earlier was cannot assign a string of slice 24 to a string of slice 48.

r58Playz commented 5 years ago

Another error, right when I connected to a server. `TypeError: can't concat str to bytes

r58Playz commented 5 years ago

Everything's working.

Nebual commented 5 years ago

In the interest of getting master to a working state, I'm going to merge this now.