Skip to content

Commit 23cb5eb

Browse files
committed
Reduce compilation warnings in tests.
1 parent ae0e28a commit 23cb5eb

File tree

8 files changed

+144
-115
lines changed

8 files changed

+144
-115
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/__init__.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,26 @@
3636
# SOFTWARE.
3737

3838
import sys
39-
#from _ast import arguments
4039
os = sys.modules.get("posix", sys.modules.get("nt", None))
4140
if os is None:
4241
raise ImportError("posix or nt module is required in builtin modules")
4342
__dir__ = __file__.rpartition("/")[0]
4443

45-
4644
GRAALPYTHON = sys.implementation.name == "graalpython"
4745

46+
4847
def unhandled_error_compare(x, y):
49-
if (isinstance(x,BaseException) and isinstance(y,BaseException)):
48+
if (isinstance(x, BaseException) and isinstance(y, BaseException)):
5049
return type(x) == type(y)
5150
else:
5251
return x == y
5352

53+
5454
class CPyExtTestCase():
55+
5556
def setUp(self):
5657
for typ in type(self).mro():
57-
for k,v in typ.__dict__.items():
58+
for k, v in typ.__dict__.items():
5859
if k.startswith("test_"):
5960
modname = k.replace("test_", "")
6061
if k.startswith("test_graalpython_"):
@@ -67,15 +68,15 @@ def setUp(self):
6768

6869
def ccompile(self, name):
6970
from distutils.core import setup, Extension
70-
module = Extension(name, sources = ['%s/%s.c' % (__dir__, name)])
71+
module = Extension(name, sources=['%s/%s.c' % (__dir__, name)])
7172
args = ['--quiet', 'build', 'install_lib', '-f', '--install-dir=%s' % __dir__]
7273
setup(
73-
script_name = 'setup',
74-
script_args = args,
75-
name = name,
76-
version = '1.0',
77-
description = '',
78-
ext_modules = [module]
74+
script_name='setup',
75+
script_args=args,
76+
name=name,
77+
version='1.0',
78+
description='',
79+
ext_modules=[module]
7980
)
8081

8182

@@ -185,7 +186,7 @@ def ccompile(self, name):
185186
PyObject* ___arg;
186187
{argumentdeclarations};
187188
{resultvardeclarations}
188-
void* res;
189+
{resulttype} res;
189190
190191
if (!PyArg_ParseTuple(args, "O", &___arg)) {{
191192
return NULL;
@@ -202,7 +203,7 @@ def ccompile(self, name):
202203
}}
203204
#endif
204205
205-
res = (void *){callfunction}({argumentnames}{resultvarlocations});
206+
res = {callfunction}({argumentnames}{resultvarlocations});
206207
207208
return Py_BuildValue("{resultspec}", res {resultvarnames});
208209
}}
@@ -228,15 +229,17 @@ def ccompile(self, name):
228229
}}
229230
"""
230231

232+
231233
class CPyExtFunction():
234+
232235
def __init__(self, pfunc, parameters, template=c_template, cmpfunc=None, **kwargs):
233236
self.template = template
234237
self.pfunc = pfunc
235238
self.parameters = parameters
236239
kwargs["name"] = kwargs["name"] if "name" in kwargs else None
237240
self.name = kwargs["name"]
238241
if "code" in kwargs:
239-
kwargs["customcode"] = kwargs["code"]
242+
kwargs["customcode"] = kwargs["code"]
240243
del kwargs["code"]
241244
else:
242245
kwargs["customcode"] = ""
@@ -286,7 +289,7 @@ def _insert(self, d, name, default_value):
286289

287290
def __repr__(self):
288291
return "<CPyExtFunction %s>" % self.name
289-
292+
290293
def test(self):
291294
sys.path.insert(0, __dir__)
292295
try:
@@ -327,10 +330,11 @@ class CPyExtFunctionOutVars(CPyExtFunction):
327330
This class supports this.
328331
Set 'resultvars' to declare the output vars.
329332
'''
333+
330334
def __init__(self, pfunc, parameters, template=c_template_multi_res, **kwargs):
331335
super(CPyExtFunctionOutVars, self).__init__(pfunc, parameters, **kwargs)
332336
self.template = template
333-
337+
334338
def create_module(self, name=None):
335339
fargs = self.formatargs
336340
if "resultvars" not in fargs:
@@ -348,26 +352,28 @@ def create_module(self, name=None):
348352
fargs["resultvarnames"] = ""
349353
if len(fargs["resultvarnames"]) and not fargs["resultvarnames"].startswith(","):
350354
fargs["resultvarnames"] = ", " + fargs["resultvarnames"]
351-
355+
352356
if "resultvarlocations" not in fargs:
353357
fargs["resultvarlocations"] = ", ".join("&" + arg.rpartition(" ")[2] for arg in fargs["resultvars"])
358+
if "resulttype" not in fargs:
359+
fargs["resulttype"] = "void*"
354360
if len(fargs["resultvarlocations"]):
355361
fargs["resultvarlocations"] = ", " + fargs["resultvarlocations"]
356362
self._insert(fargs, "customcode", "")
357363
super(CPyExtFunctionOutVars, self).create_module(name)
358364

359365

360366
class CPyExtFunctionVoid(CPyExtFunction):
367+
361368
def __init__(self, pfunc, parameters, template=c_template_void, **kwargs):
362369
super(CPyExtFunctionVoid, self).__init__(pfunc, parameters, **kwargs)
363370
self.template = template
364-
371+
365372
def create_module(self, name=None):
366373
fargs = self.formatargs
367374
if "resultval" not in fargs:
368375
fargs["resultval"] = "Py_None"
369376
super(CPyExtFunctionVoid, self).create_module(name)
370377

371378

372-
373379
CPyExtTestCase.compile_module = ccompile

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_bytes.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ def _reference_format(args):
5656
fmt_args = tuple(args[1:])
5757
return (fmt % fmt_args).encode()
5858

59+
5960
class TestPyBytes(CPyExtTestCase):
61+
6062
def compile_module(self, name):
6163
type(self).mro()[1].__dict__["test_%s" % name].create_module(name)
6264
super(TestPyBytes, self).compile_module(name)
@@ -84,7 +86,7 @@ def compile_module(self, name):
8486
# PyBytes_AsString
8587
test_PyBytes_AsString = CPyExtFunction(
8688
lambda b: b[0].decode(),
87-
lambda: ((b"hello", ), (b"world",)),
89+
lambda: ((b"hello",), (b"world",)),
8890
resultspec="s",
8991
argspec="O",
9092
arguments=["PyObject* arg"],
@@ -98,12 +100,13 @@ def compile_module(self, name):
98100
argspec="O",
99101
arguments=["PyObject* arg"],
100102
resultvars=("char* s", "Py_ssize_t sz"),
103+
resulttype="int"
101104
)
102105

103106
# PyBytes_Size
104107
test_PyBytes_Size = CPyExtFunction(
105108
lambda b: len(b[0]),
106-
lambda: ((b"hello", ), (b"hello world",), (b"",)),
109+
lambda: ((b"hello",), (b"hello world",), (b"",)),
107110
resultspec="n",
108111
argspec="O",
109112
arguments=["PyObject* arg"],
@@ -112,7 +115,7 @@ def compile_module(self, name):
112115
# PyBytes_GET_SIZE
113116
test_PyBytes_GET_SIZE = CPyExtFunction(
114117
lambda b: len(b[0]),
115-
lambda: ((b"hello", ), (b"hello world",), (b"",)),
118+
lambda: ((b"hello",), (b"hello world",), (b"",)),
116119
resultspec="n",
117120
argspec="O",
118121
arguments=["PyObject* arg"],
@@ -122,7 +125,7 @@ def compile_module(self, name):
122125
# PyBytes_FromFormat
123126
test_PyBytes_FromFormat = CPyExtFunction(
124127
_reference_format,
125-
lambda: (("hello %s %s, %d times", "beautiful", "world", 3), ),
128+
lambda: (("hello %s %s, %d times", "beautiful", "world", 3),),
126129
resultspec="O",
127130
argspec="sssi",
128131
arguments=["char* fmt", "char* arg0", "char* arg1", "int arg2"],
@@ -145,6 +148,7 @@ def compile_module(self, name):
145148
resultspec="iO",
146149
argspec="OO",
147150
arguments=["PyObject* arg0", "PyObject* arg1"],
151+
resulttype="int",
148152
argumentnames="&arg0, arg1",
149153
resultvarnames="arg0",
150154
callfunction="wrap_PyBytes_Concat"
@@ -167,6 +171,7 @@ def compile_module(self, name):
167171
resultspec="iO",
168172
argspec="OO",
169173
arguments=["PyObject* arg0", "PyObject* arg1"],
174+
resulttype="int",
170175
argumentnames="&arg0, arg1",
171176
resultvarnames="arg0",
172177
callfunction="wrap_PyBytes_ConcatAndDel"
@@ -206,9 +211,9 @@ def compile_module(self, name):
206211
test_PyBytes_AS_STRING = CPyExtFunction(
207212
lambda b: b[0].decode("utf-8"),
208213
lambda: (
209-
(b"hello", ),
210-
("hellö".encode("utf-8"), ),
211-
(b"hello world",),
214+
(b"hello",),
215+
("hellö".encode("utf-8"),),
216+
(b"hello world",),
212217
(b"",)
213218
),
214219
resultspec="s",

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_capsule.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,20 @@
3939
from . import CPyExtTestCase, CPyExtFunction, CPyExtFunctionOutVars, unhandled_error_compare, GRAALPYTHON
4040
__dir__ = __file__.rpartition("/")[0]
4141

42+
4243
class TestPyCapsule(CPyExtTestCase):
44+
4345
def compile_module(self, name):
4446
type(self).mro()[1].__dict__["test_%s" % name].create_module(name)
4547
super(TestPyCapsule, self).compile_module(name)
4648

4749
test_PyCapsule_CheckExact = CPyExtFunction(
4850
lambda args: True,
4951
lambda: (
50-
("hello",0xDEADBEEF),
52+
("hello", 0xDEADBEEF),
5153
),
5254
code='''int wrap_PyCapsule_Check(char * name, Py_ssize_t ptr) {
53-
PyObject* capsule = PyCapsule_New(ptr, name, NULL);
55+
PyObject* capsule = PyCapsule_New((void *)ptr, name, NULL);
5456
return PyCapsule_CheckExact(capsule);
5557
}
5658
''',

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_dict.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
__dir__ = __file__.rpartition("/")[0]
4141

4242

43-
4443
def _reference_get_item(args):
4544
try:
4645
d = args[0]
@@ -92,7 +91,9 @@ def _reference_copy(args):
9291
class SubDict(dict):
9392
pass
9493

94+
9595
class TestPyDict(CPyExtTestCase):
96+
9697
def compile_module(self, name):
9798
type(self).mro()[1].__dict__["test_%s" % name].create_module(name)
9899
super(TestPyDict, self).compile_module(name)
@@ -155,6 +156,7 @@ def compile_module(self, name):
155156
resultspec="O",
156157
argspec='OsO',
157158
arguments=("PyObject* dict", "char* key", "PyObject* defaultValue"),
159+
resulttype="PyObject*",
158160
callfunction="wrap_PyDict_GetItemString",
159161
cmpfunc=unhandled_error_compare
160162
)
@@ -172,8 +174,8 @@ def compile_module(self, name):
172174
# PyDict_Next
173175
test_PyDict_Next = CPyExtFunctionOutVars(
174176
_reference_next,
175-
#lambda: (({'a': "hello"}, 0), ({'a': "hello", 'b': 'world'}, 1), ({'a': "hello"}, 1)),
176-
lambda: (({'a': "hello"}, 1), ),
177+
# lambda: (({'a': "hello"}, 0), ({'a': "hello", 'b': 'world'}, 1), ({'a': "hello"}, 1)),
178+
lambda: (({'a': "hello"}, 1),),
177179
code='''int wrap_PyDict_Next(PyObject* dict, Py_ssize_t* ppos, PyObject** key, PyObject** value) {
178180
int res = 0;
179181
Py_ssize_t iterations = *ppos;
@@ -196,6 +198,7 @@ def compile_module(self, name):
196198
resultspec="iOO",
197199
argspec='On',
198200
arguments=("PyObject* dict", "Py_ssize_t ppos"),
201+
resulttype="int",
199202
argumentnames=("dict, &ppos"),
200203
resultvars=("PyObject* key", "PyObject* value"),
201204
callfunction="wrap_PyDict_Next",
@@ -229,7 +232,7 @@ def compile_module(self, name):
229232
# PyDict_Contains
230233
test_PyDict_Contains = CPyExtFunction(
231234
lambda args: int(args[1] in args[0]),
232-
lambda: (({},"a"), ({'a': "hello"},"a"), ({'a': "hello"},"b")),
235+
lambda: (({}, "a"), ({'a': "hello"}, "a"), ({'a': "hello"}, "b")),
233236
resultspec="i",
234237
argspec='OO',
235238
arguments=["PyObject* dict", "PyObject* key"],
@@ -238,8 +241,8 @@ def compile_module(self, name):
238241
test_PyDict_Check = CPyExtFunction(
239242
lambda args: isinstance(args[0], dict),
240243
lambda: (
241-
({},),
242-
({'a': "hello"},),
244+
({},),
245+
({'a': "hello"},),
243246
(dict(),),
244247
("not a dict",),
245248
(3,),
@@ -255,8 +258,8 @@ def compile_module(self, name):
255258
test_PyDict_CheckExact = CPyExtFunction(
256259
lambda args: type(args[0]) is dict,
257260
lambda: (
258-
({},),
259-
({'a': "hello"},),
261+
({},),
262+
({'a': "hello"},),
260263
(dict(),),
261264
("not a dict",),
262265
(3,),

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_float.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@
4141

4242

4343
def _float_compare(x, y):
44+
4445
def isNan(x):
4546
return isinstance(x, float) and x != x
4647

47-
if (isinstance(x,BaseException) and isinstance(y,BaseException)):
48+
if (isinstance(x, BaseException) and isinstance(y, BaseException)):
4849
return type(x) == type(y)
4950
else:
5051
# either equal or both are NaN
@@ -69,31 +70,33 @@ class DummyNonFloat():
6970

7071

7172
class DummyFloatable():
73+
7274
def __float__(self):
7375
return 3.14159
7476

7577

7678
class DummyFloatSubclass(float):
79+
7780
def __float__(self):
7881
return 2.71828
7982

8083

8184
class TestPyFloat(CPyExtTestCase):
85+
8286
def compile_module(self, name):
8387
type(self).mro()[1].__dict__["test_%s" % name].create_module(name)
8488
super(TestPyFloat, self).compile_module(name)
8589

86-
8790
test_PyFloat_AsDouble = CPyExtFunctionOutVars(
8891
lambda args: True,
8992
lambda: (
9093
(float(0.0), 0.0),
91-
(float(-1.0),-1.0),
92-
(float(0xffffffffffffffffffffffff),0xffffffffffffffffffffffff),
93-
(float('nan'),float('nan')),
94-
(DummyFloatable(),3.14159),
95-
(DummyFloatSubclass(),0.0),
96-
(DummyNonFloat(),-1.0),
94+
(float(-1.0), -1.0),
95+
(float(0xffffffffffffffffffffffff), 0xffffffffffffffffffffffff),
96+
(float('nan'), float('nan')),
97+
(DummyFloatable(), 3.14159),
98+
(DummyFloatSubclass(), 0.0),
99+
(DummyNonFloat(), -1.0),
97100
),
98101
code='''int wrap_PyFloat_AsDouble(PyObject* obj, double expected) {
99102
double res = PyFloat_AsDouble(obj);
@@ -113,6 +116,7 @@ def compile_module(self, name):
113116
resultspec="i",
114117
argspec='Od',
115118
arguments=["PyObject* obj", "double expected"],
119+
resulttype="int",
116120
callfunction="wrap_PyFloat_AsDouble",
117121
)
118122

0 commit comments

Comments
 (0)