Skip to content

Commit 431a8dc

Browse files
committed
Support Python 3.4 (fix #17)
1 parent 9440f21 commit 431a8dc

File tree

16 files changed

+58
-34
lines changed

16 files changed

+58
-34
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ language: python
22
sudo: false
33
python:
44
- 2.7
5+
- 3.4
56
install:
67
- pip install pytest pytest-cov coveralls flake8
78
- pip install -e .

graphql/core/compat.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
try:
2+
str_type = basestring
3+
str_is_unicode = False
4+
except NameError:
5+
str_type = str
6+
str_is_unicode = True
7+
8+
try:
9+
unichr = unichr
10+
except NameError:
11+
unichr = chr
12+
13+
14+
if str_is_unicode:
15+
def native_str(s, errors=None):
16+
return s
17+
else:
18+
def native_str(s, errors=None):
19+
return s.encode(errors=errors)

graphql/core/error.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Error(Exception):
88
class GraphQLError(Error):
99
def __init__(self, message, nodes=None, stack=None, source=None, positions=None):
1010
super(GraphQLError, self).__init__(message)
11+
self.message = message
1112
self.nodes = nodes
1213
self.stack = stack or message
1314
self._source = source

graphql/core/execution/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def __init__(self, schema, root, document_ast, operation_name, args):
7171
raise GraphQLError(
7272
'Must provide operation name '
7373
'if query contains multiple operations')
74-
op_name = operation_name or operations.keys()[0]
74+
op_name = operation_name or next(iter(operations.keys()))
7575
operation = operations.get(op_name)
7676
if not operation:
7777
raise GraphQLError('Unknown operation name: {}'.format(op_name))
@@ -106,7 +106,8 @@ def execute(schema, root, ast, operation_name='', args=None):
106106
data = None
107107
if not ctx.errors:
108108
return ExecutionResult(data)
109-
return ExecutionResult(data, map(format_error, ctx.errors))
109+
formatted_errors = list(map(format_error, ctx.errors))
110+
return ExecutionResult(data, formatted_errors)
110111

111112

112113
def execute_operation(ctx, root, operation):

graphql/core/execution/values.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import collections
2+
from ..compat import str_type
23
from ..error import GraphQLError
34
from ..language import ast
45
from ..language.printer import print_ast
@@ -85,7 +86,7 @@ def is_valid_value(type, value):
8586

8687
if isinstance(type, GraphQLList):
8788
item_type = type.of_type
88-
if not isinstance(value, basestring) and \
89+
if not isinstance(value, str_type) and \
8990
isinstance(value, collections.Iterable):
9091
return all(is_valid_value(item_type, item) for item in value)
9192
else:
@@ -125,7 +126,7 @@ def coerce_value(type, value):
125126

126127
if isinstance(type, GraphQLList):
127128
item_type = type.of_type
128-
if not isinstance(value, basestring) and isinstance(value, collections.Iterable):
129+
if not isinstance(value, str_type) and isinstance(value, collections.Iterable):
129130
return [coerce_value(item_type, item) for item in value]
130131
else:
131132
return [coerce_value(item_type, value)]

graphql/core/language/error.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from ..compat import native_str
12
from ..error import Error
23
from .location import get_location
34

@@ -11,18 +12,14 @@ def __init__(self, source, position, description):
1112
self.description = description
1213

1314
def __str__(self):
14-
# FIXME
15-
return unicode(self).encode(errors='replace')
16-
17-
def __unicode__(self):
1815
location = get_location(self.source, self.position)
19-
return u'Syntax Error {} ({}:{}) {}\n\n{}'.format(
16+
return native_str(u'Syntax Error {} ({}:{}) {}\n\n{}'.format(
2017
self.source.name,
2118
location.line,
2219
location.column,
2320
self.description,
2421
highlight_source_at_location(self.source, location),
25-
)
22+
), errors='replace')
2623

2724

2825
def highlight_source_at_location(source, location):

graphql/core/language/lexer.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
from ..compat import unichr
13
from .error import LanguageError
24

35
__all__ = ['Token', 'Lexer', 'TokenKind',
@@ -101,7 +103,7 @@ def get_token_kind_desc(kind):
101103
def char_code_at(s, pos):
102104
if 0 <= pos < len(s):
103105
return ord(s[pos])
104-
return None
106+
return 0
105107

106108

107109
PUNCT_CODE_TO_KIND = {
@@ -153,7 +155,7 @@ def read_token(source, from_position):
153155

154156
raise LanguageError(
155157
source, position,
156-
u'Unexpected character "{}"'.format(body[position]))
158+
u'Unexpected character {}'.format(json.dumps(body[position])))
157159

158160

159161
def position_after_whitespace(body, start_position):
@@ -176,7 +178,7 @@ def position_after_whitespace(body, start_position):
176178
position += 1
177179
while position < body_length:
178180
code = char_code_at(body, position)
179-
if code is None or code in (10, 13, 0x2028, 0x2029):
181+
if not code or code in (10, 13, 0x2028, 0x2029):
180182
break
181183
position += 1
182184
else:
@@ -273,7 +275,7 @@ def read_string(source, start):
273275

274276
while position < len(body):
275277
code = char_code_at(body, position)
276-
if code is None or code in (34, 10, 13, 0x2028, 0x2029):
278+
if not code or code in (34, 10, 13, 0x2028, 0x2029):
277279
break
278280
position += 1
279281
if code == 92: # \
@@ -349,7 +351,7 @@ def read_name(source, position):
349351
code = None
350352
while end != body_length:
351353
code = char_code_at(body, end)
352-
if code is None or not (
354+
if not code or not (
353355
code == 95 or # _
354356
48 <= code <= 57 or # 0-9
355357
65 <= code <= 90 or # A-Z

graphql/core/language/parser.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from ..compat import str_type
12
from .source import Source
23
from .error import LanguageError
34
from .lexer import Lexer, TokenKind, get_token_kind_desc, get_token_desc
@@ -11,7 +12,7 @@ def parse(source, **kwargs):
1112
options = {'no_location': False, 'no_source': False}
1213
options.update(kwargs)
1314
source_obj = source
14-
if isinstance(source, basestring):
15+
if isinstance(source, str_type):
1516
source_obj = Source(source)
1617
parser = Parser(source_obj, options)
1718
return parse_document(parser)

graphql/core/type/schema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from functools import reduce
12
from .definition import (
23
GraphQLObjectType,
34
GraphQLInterfaceType,

graphql/core/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def enter(self, node):
107107
arg_type = None
108108
field_or_directive = self.get_directive() or self.get_field_def()
109109
if field_or_directive:
110-
arg_def = filter(lambda arg: arg.name == node.name.value, field_or_directive.args)
110+
arg_def = [arg for arg in field_or_directive.args if arg.name == node.name.value]
111111
if arg_def:
112112
arg_def = arg_def[0]
113113
arg_type = arg_def.type

0 commit comments

Comments
 (0)