Skip to content

Commit 15c0da5

Browse files
committed
Python3 compatibility
1 parent 4664ebd commit 15c0da5

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

gitlab.py

+17-5
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,21 @@
1515
# You should have received a copy of the GNU Lesser General Public License
1616
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1717

18+
from __future__ import print_function, division, absolute_import
19+
20+
from itertools import chain
21+
1822
import json
1923
import requests
2024
import sys
2125

26+
if sys.version_info[0] < 3:
27+
PY2=True
28+
str_types = (str, unicode,)
29+
else:
30+
PY2=False
31+
str_types = (str,)
32+
2233
__title__ = 'python-gitlab'
2334
__version__ = '0.7'
2435
__author__ = 'Gauvain Pocentek'
@@ -312,7 +323,7 @@ def update(self, obj):
312323
d[k] = str(v)
313324
elif type(v) == bool:
314325
d[k] = 1 if v else 0
315-
elif type(v) == unicode:
326+
elif PY2 and type(v) == unicode:
316327
d[k] = str(v.encode(self.gitlab_encoding, "replace"))
317328

318329
try:
@@ -462,7 +473,7 @@ def _get_display_encoding():
462473

463474

464475
def _sanitize(value):
465-
if type(value) in (str, unicode):
476+
if type(value) in str_types:
466477
return value.replace('/', '%2F')
467478
return value
468479

@@ -562,7 +573,7 @@ def delete(self):
562573
def __init__(self, gl, data=None, **kwargs):
563574
self.gitlab = gl
564575

565-
if data is None or type(data) in [int, str, unicode]:
576+
if data is None or type(data) in chain((int,), str_types):
566577
data = self.gitlab.get(self.__class__, data, **kwargs)
567578

568579
self._setFromDict(data)
@@ -598,7 +609,7 @@ def _obj_to_str(obj):
598609
elif isinstance(obj, list):
599610
s = ", ".join([GitlabObject._obj_to_str(x) for x in obj])
600611
return "[ %s ]" % s
601-
elif isinstance(obj, unicode):
612+
elif PY2 and isinstance(obj, unicode):
602613
return obj.encode(_get_display_encoding(), "replace")
603614
else:
604615
return str(obj)
@@ -611,7 +622,8 @@ def pretty_print(self, depth=0):
611622
continue
612623
v = self.__dict__[k]
613624
pretty_k = k.replace('_', '-')
614-
pretty_k = pretty_k.encode(_get_display_encoding(), "replace")
625+
if PY2:
626+
pretty_k = pretty_k.encode(_get_display_encoding(), "replace")
615627
if isinstance(v, GitlabObject):
616628
if depth == 0:
617629
print("%s:" % pretty_k)

0 commit comments

Comments
 (0)