remzi-arpacidusseau / ostep-homework

1.99k stars 959 forks source link

Fix the paging-multilevel-translate.py to work with Python 3 #20

Open modulitos opened 3 years ago

modulitos commented 3 years ago

When running this script with python 3, we get the following error:

❯ python3 paging-multilevel-translate.py ARG seed 0 ARG allocated 64 ARG num 10

Traceback (most recent call last): File "paging-multilevel-translate.py", line 241, in os = OS() File "paging-multilevel-translate.py", line 57, in init for i in range(0, self.maxPageCount): TypeError: 'float' object cannot be interpreted as an integer

This is due to a breaking change in Python 3, where the division operator represents true division, producing a floating point result. Whereas in Python 2, it performs classic division that rounds the result down toward negative infinity (also known as taking the floor).

This PR changes the operator to use //, which according to the docs:

https://www.python.org/dev/peps/pep-0238/

The // operator will be available to request floor division unambiguously.

This should make the script Python 3 compatible, without changing any behavior in Python 2.

I tested this change by running the script in Python 2, and confirming that the output is the same before and after this change.

modulitos commented 3 years ago

@remzi-arpacidusseau Can we merge this in? I think it will save future students the trouble of having to fix this script when they're running on Python 3.

I can confirm that it introduces no behavioral change.

remzi-arpacidusseau commented 3 years ago

Will do, thanks!

On Sun, Oct 25, 2020 at 4:35 AM Lucas Swart notifications@github.com wrote:

When running this script with python 3, we get the following error:

❯ python3 paging-multilevel-translate.py ARG seed 0 ARG allocated 64 ARG num 10

Traceback (most recent call last): File "paging-multilevel-translate.py", line 241, in os = OS() File "paging-multilevel-translate.py", line 57, in init for i in range(0, self.maxPageCount): TypeError: 'float' object cannot be interpreted as an integer

This is due to a breaking change in Python 3, where the division operator represents true division, producing a floating point result. Whereas in Python 2, it performs classic division that rounds the result down toward negative infinity (also known as taking the floor).

This PR changes the operator to use //, which according to the docs:

https://www.python.org/dev/peps/pep-0238/

The // operator will be available to request floor division unambiguously.

This should make the script Python 2 and 3 compatible.

You can view, comment on, or merge this pull request online at:

https://github.com/remzi-arpacidusseau/ostep-homework/pull/20 Commit Summary

  • python3 fixes

File Changes

Patch Links:

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/remzi-arpacidusseau/ostep-homework/pull/20, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADDKP56DNGLBOJ32ZMFBK7LSMPWPZANCNFSM4S6GT7KA .