Skip to content

Commit

Permalink
Python: Python2 removal (#3076)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-detiste committed Jul 12, 2023
1 parent 6af1e4c commit e225508
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 133 deletions.
12 changes: 2 additions & 10 deletions gui/wxpython/core/gconsole.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,13 @@
@author Wolf Bergenheim <wolf bergenheim.net> (#962)
"""

from __future__ import print_function

import os
import sys
import re
import time
import threading

if sys.version_info.major == 2:
import Queue
else:
import queue as Queue
import queue as Queue

import codecs
import locale
Expand Down Expand Up @@ -570,10 +565,7 @@ def RunCmd(
message=_("Module <%s> not found.") % command[0],
)
pymodule = imp.load_source(command[0].replace(".", "_"), pyPath)
try: # PY3
pymain = inspect.getfullargspec(pymodule.main)
except AttributeError:
pymain = inspect.getargspec(pymodule.main)
pymain = inspect.getfullargspec(pymodule.main)
if pymain and "giface" in pymain.args:
pymodule.main(self._giface)
return
Expand Down
2 changes: 0 additions & 2 deletions gui/wxpython/core/globalvar.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
@author Martin Landa <landa.martin gmail.com>
"""

from __future__ import print_function

import os
import sys
import locale
Expand Down
5 changes: 2 additions & 3 deletions gui/wxpython/core/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import sys
import copy
import time
import six

import wx

Expand Down Expand Up @@ -230,7 +229,7 @@ def _getRegionDict(self, env):
def _createRegionStr(self, region):
"""Create string for GRASS_REGION env variable from dict created by _getRegionDict."""
regionStr = ""
for k, v in six.iteritems(region):
for k, v in region.items():
item = k + ": " + str(v)
if regionStr:
regionStr += "; "
Expand Down Expand Up @@ -364,7 +363,7 @@ def AddRasterBands(self, sourceFile, sTBands):
if sXsize < 1 or sYsize < 1:
return

for sBandNnum, tBandNum in six.iteritems(sTBands):
for sBandNnum, tBandNum in sTBands.items():
bandData = sDataset.GetRasterBand(sBandNnum).ReadRaster(
sXoff, sYoff, sXsize, sYsize, tXsize, tYsize, gdal.GDT_Byte
)
Expand Down
33 changes: 16 additions & 17 deletions gui/wxpython/nviz/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"""

import copy
import six

from core.settings import UserSettings

Expand All @@ -32,7 +31,7 @@ def __init__(self):
def SetConstantDefaultProp(self):
"""Set default constant data properties"""
data = dict()
for key, value in six.iteritems(UserSettings.Get(group="nviz", key="constant")):
for key, value in UserSettings.Get(group="nviz", key="constant").items():
data[key] = value
color = (
str(data["color"][0])
Expand All @@ -57,19 +56,19 @@ def SetSurfaceDefaultProp(self, data=None):
#
for attrb in ("shine",):
data["attribute"][attrb] = {}
for key, value in six.iteritems(
UserSettings.Get(group="nviz", key="surface", subkey=attrb)
):
for key, value in UserSettings.Get(
group="nviz", key="surface", subkey=attrb
).items():
data["attribute"][attrb][key] = value
data["attribute"][attrb]["update"] = None

#
# draw
#
data["draw"]["all"] = False # apply only for current surface
for control, value in six.iteritems(
UserSettings.Get(group="nviz", key="surface", subkey="draw")
):
for control, value in UserSettings.Get(
group="nviz", key="surface", subkey="draw"
).items():
if control[:3] == "res":
if "resolution" not in data["draw"]:
data["draw"]["resolution"] = {}
Expand Down Expand Up @@ -115,9 +114,9 @@ def SetVolumeDefaultProp(self):
#
# draw
#
for control, value in six.iteritems(
UserSettings.Get(group="nviz", key="volume", subkey="draw")
):
for control, value in UserSettings.Get(
group="nviz", key="volume", subkey="draw"
).items():
if control == "shading":
sel = UserSettings.Get(
group="nviz", key="volume", subkey=["draw", "shading"]
Expand Down Expand Up @@ -164,9 +163,9 @@ def SetVolumeDefaultProp(self):
#
for attrb in ("shine",):
data["attribute"][attrb] = {}
for key, value in six.iteritems(
UserSettings.Get(group="nviz", key="volume", subkey=attrb)
):
for key, value in UserSettings.Get(
group="nviz", key="volume", subkey=attrb
).items():
data["attribute"][attrb][key] = value

return data
Expand All @@ -180,9 +179,9 @@ def SetIsosurfaceDefaultProp(self):
if attr == "inout":
data[attr]["value"] = 0
continue
for key, value in six.iteritems(
UserSettings.Get(group="nviz", key="volume", subkey=attr)
):
for key, value in UserSettings.Get(
group="nviz", key="volume", subkey=attr
).items():
data[attr][key] = value
return data

Expand Down
23 changes: 4 additions & 19 deletions lib/init/grass.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
# (this makes it more stable since we have to set up paths first)
# pylint: disable=too-many-lines

from __future__ import print_function
import sys
import os
import errno
Expand All @@ -49,7 +48,6 @@
import string
import subprocess
import re
import six
import platform
import tempfile
import locale
Expand Down Expand Up @@ -118,9 +116,7 @@ def decode(bytes_, encoding=ENCODING):
:param encoding: encoding to be used, default value is the system's default
encoding or, if that cannot be determined, 'UTF-8'.
"""
if sys.version_info.major >= 3:
unicode = str
if isinstance(bytes_, unicode):
if isinstance(bytes_, str):
return bytes_
elif isinstance(bytes_, bytes):
return bytes_.decode(encoding)
Expand All @@ -140,12 +136,9 @@ def encode(string, encoding=ENCODING):
:param encoding: encoding to be used, default value is the system's default
encoding or, if that cannot be determined, 'UTF-8'.
"""
if sys.version_info.major >= 3:
unicode = str
if isinstance(string, bytes):
return string
# this also tests str in Py3:
elif isinstance(string, unicode):
elif isinstance(string, str):
return string.encode(encoding)
else:
# if something else than text
Expand All @@ -155,12 +148,7 @@ def encode(string, encoding=ENCODING):
# see https://trac.osgeo.org/grass/ticket/3508
def to_text_string(obj, encoding=ENCODING):
"""Convert `obj` to (unicode) text string"""
if six.PY2:
# Python 2
return encode(obj, encoding=encoding)
else:
# Python 3
return decode(obj, encoding=encoding)
return decode(obj, encoding=encoding)


def try_remove(path):
Expand Down Expand Up @@ -279,10 +267,7 @@ def count_wide_chars(s):
:param str s: string
"""
return sum(
unicodedata.east_asian_width(c) in "WF"
for c in (s if sys.version_info.major >= 3 else unicode(s))
)
return sum(unicodedata.east_asian_width(c) in "WF" for c in s)


def f(fmt, *args):
Expand Down
19 changes: 2 additions & 17 deletions python/grass/gunittest/multirunner.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,12 @@
:authors: Vaclav Petras
"""

from __future__ import print_function

import sys
import os
import argparse
import subprocess
import locale

try:
from itertools import izip as zip
except ImportError: # will be 3.x series
pass

if sys.version_info.major >= 3:
unicode = str


def _get_encoding():
try:
Expand All @@ -45,7 +35,7 @@ def decode(bytes_, encoding=None):


def encode(string, encoding=None):
if isinstance(string, unicode):
if isinstance(string, str):
return string.encode(_get_encoding())
else:
return string
Expand All @@ -55,12 +45,7 @@ def text_to_string(text):
"""Convert text to str. Useful when passing text into environments,
in Python 2 it needs to be bytes on Windows, in Python 3 in needs unicode.
"""
if sys.version[0] == "2":
# Python 2
return encode(text)
else:
# Python 3
return decode(text)
return decode(text)


def main():
Expand Down
5 changes: 0 additions & 5 deletions python/grass/imaging/images2swf.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
"""

import os
import sys
import zlib

try:
Expand All @@ -81,10 +80,6 @@
PIL = None


# True if we are running on Python 3.
# Code taken from six.py by Benjamin Peterson (MIT licensed)
PY3 = sys.version_info[0] == 3

string_types = (str,)
integer_types = (int,)
class_types = (type,)
Expand Down
11 changes: 3 additions & 8 deletions python/grass/pygrass/modules/interface/testsuite/test_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@
@author: pietro
"""
import sys
from fnmatch import fnmatch
from io import BytesIO

from grass.gunittest.case import TestCase
from grass.gunittest.main import test

from grass.script.core import get_commands
from grass.exceptions import ParameterError
from grass.pygrass.modules.interface import Module

PY2 = sys.version_info[0] == 2
if PY2:
from StringIO import StringIO
else:
from io import BytesIO as StringIO


SKIP = [
"g.parser",
Expand Down Expand Up @@ -66,7 +61,7 @@ def test_rsun(self):
"""Test if a Module instance is pickable"""
import pickle

out = StringIO()
out = BytesIO()
pickle.dump(Module("r.sun"), out)
out.close()

Expand Down
13 changes: 6 additions & 7 deletions python/grass/pygrass/raster/testsuite/test_raster_region.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import six
from grass.gunittest.case import TestCase
from grass.gunittest.main import test

Expand Down Expand Up @@ -42,11 +41,11 @@ def test_resampling_1(self):
rast.set_region(region)
rast.open(mode="r")

six.assertCountEqual(
self, rast[0].tolist(), [22, 22, 22, 22, 22, 32, 32, 32, 32, 32]
self.assertCountEqual(
rast[0].tolist(), [22, 22, 22, 22, 22, 32, 32, 32, 32, 32]
)
six.assertCountEqual(
self, rast[5].tolist(), [23, 23, 23, 23, 23, 33, 33, 33, 33, 33]
self.assertCountEqual(
rast[5].tolist(), [23, 23, 23, 23, 23, 33, 33, 33, 33, 33]
)

rast.close()
Expand Down Expand Up @@ -77,8 +76,8 @@ def test_resampling_2(self):
[nan, nan, nan, nan, nan, nan, nan, nan]
"""

six.assertCountEqual(self, rast[2].tolist()[2:6], [11.0, 21.0, 31.0, 41.0])
six.assertCountEqual(self, rast[5].tolist()[2:6], [14.0, 24.0, 34.0, 44.0])
self.assertCountEqual(rast[2].tolist()[2:6], [11.0, 21.0, 31.0, 41.0])
self.assertCountEqual(rast[5].tolist()[2:6], [14.0, 24.0, 34.0, 44.0])

rast.close()

Expand Down
5 changes: 1 addition & 4 deletions python/grass/script/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@
from .utils import decode, split
from .core import Popen, PIPE, get_real_command

try:
import xml.etree.ElementTree as etree
except ImportError:
import elementtree.ElementTree as etree # Python <= 2.4
import xml.etree.ElementTree as etree
from xml.parsers import expat # TODO: works for any Python?

# Get the XML parsing exceptions to catch. The behavior chnaged with Python 2.7
Expand Down

0 comments on commit e225508

Please sign in to comment.