Skip to content

Commit 71fe343

Browse files
author
markus
committed
Fix utf-8 error on py 2.7
1 parent c4c6533 commit 71fe343

File tree

7 files changed

+41
-22
lines changed

7 files changed

+41
-22
lines changed

graphql_relay/node/tests/test_global.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
from collections import namedtuple
2-
from pytest import raises
32
from graphql import graphql
43
from graphql.type import (
54
GraphQLSchema,
65
GraphQLObjectType,
76
GraphQLField,
8-
GraphQLArgument,
97
GraphQLList,
10-
GraphQLNonNull,
118
GraphQLInt,
129
GraphQLString,
13-
GraphQLBoolean,
14-
GraphQLID,
1510
)
1611

1712
from graphql_relay.node.node import (

graphql_relay/node/tests/test_node.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
from collections import namedtuple
2-
from pytest import raises
32
from graphql import graphql
43
from graphql.type import (
54
GraphQLSchema,
65
GraphQLObjectType,
76
GraphQLField,
8-
GraphQLArgument,
9-
GraphQLList,
107
GraphQLNonNull,
118
GraphQLInt,
129
GraphQLString,
13-
GraphQLBoolean,
1410
GraphQLID,
1511
)
1612

graphql_relay/tests/__init__.py

Whitespace-only changes.

graphql_relay/tests/test_utils.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import base64
5+
6+
from .. import utils
7+
8+
9+
def test_base64_encode_unicode_strings_correctly():
10+
my_unicode = u'ûñö'
11+
my_base64 = utils.base64(my_unicode)
12+
assert my_base64 == base64.b64encode(my_unicode.encode('utf-8')).decode('utf-8')
13+
14+
15+
def test_base64_encode_strings_correctly():
16+
my_string = 'abc'
17+
my_base64 = utils.base64(my_string)
18+
assert my_base64 == base64.b64encode(my_string.encode('utf-8')).decode('utf-8')
19+
20+
21+
def test_unbase64_decodes_unicode_strings_correctly():
22+
my_unicode = u'ûñö'
23+
my_converted_unicode = utils.unbase64(utils.base64(my_unicode))
24+
assert my_unicode == my_converted_unicode
25+
26+
27+
def test_unbase64_decodes_strings_correctly():
28+
my_string = 'abc'
29+
my_converted_string = utils.unbase64(utils.base64(my_string))
30+
assert my_string == my_converted_string

graphql_relay/utils.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
from base64 import b64encode as _base64, b64decode as _unbase64
22

3-
try:
4-
str_type = basestring
5-
base64 = _base64
6-
unbase64 = _unbase64
3+
from six import string_types
74

8-
def is_str(s):
9-
return isinstance(s, basestring)
105

11-
except NameError:
12-
def base64(s):
13-
return _base64(bytes(s, 'utf-8')).decode('utf-8')
6+
def base64(s):
7+
return _base64(s.encode('utf-8')).decode('utf-8')
148

15-
def unbase64(s):
16-
return _unbase64(s).decode('utf-8')
179

18-
def is_str(s):
19-
return isinstance(s, str)
10+
def unbase64(s):
11+
return _unbase64(s).decode('utf-8')
12+
13+
14+
def is_str(s):
15+
return isinstance(s, string_types)
2016

2117

2218
def resolve_maybe_thunk(f):

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def run_tests(self):
5454
packages=find_packages(exclude=['tests']),
5555

5656
install_requires=[
57+
'six>=1.10.0',
5758
'graphql-core>=0.5.0',
5859
'promise>=0.4.0'
5960
],

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ envlist = py27,py33,py34,py35,pypy
55
deps=
66
pytest>=2.7.2
77
django>=1.8.0,<1.9
8+
six
89
flake8
910
singledispatch
1011
commands=

0 commit comments

Comments
 (0)