Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

# -*- coding: utf-8 -*- 

r""" 

Test that the Sage library uses Python 3 syntax 

 

AUTHORS: 

 

- Volker Braun (2017-02-11): initial version 

 

- Frédéric Chapoton (2017-02-28): fixes 

 

- Julian Rüth (2017-03-14): documentation changes 

 

EXAMPLES:: 

 

sage: from sage.tests.py3_syntax import Python3SyntaxTest 

sage: py3_syntax = Python3SyntaxTest('sage', 'sage_setup') 

sage: py3_syntax.run_tests('.py') # long time 

""" 

#***************************************************************************** 

# Copyright (C) 2017 Volker Braun <vbraun.name@gmail.com> 

# 2017 Frédéric Chapoton <chapoton@math.univ-lyon1.fr> 

# 2017 Julian Rüth <julian.rueth@fsfe.org> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

 

from __future__ import print_function 

 

import os 

import itertools 

import subprocess 

 

from sage.env import SAGE_SRC 

 

class SortedDirectoryWalkerABC(object): 

r""" 

Walks the directory tree in a reproducible manner. 

 

INPUT: 

 

- ``*directories`` -- roots of the directory trees to walk 

 

EXAMPLES:: 

 

sage: from sage.tests.py3_syntax import SortedDirectoryWalkerABC 

sage: walker = SortedDirectoryWalkerABC('sage/tests') 

 

""" 

def __init__(self, *directories): 

r""" 

TESTS:: 

 

sage: from sage.tests.py3_syntax import Python3SyntaxTest, SortedDirectoryWalkerABC 

sage: isinstance(Python3SyntaxTest('sage/tests'), SortedDirectoryWalkerABC) 

True 

""" 

self._directories = tuple( 

os.path.join(SAGE_SRC, d) for d in directories 

) 

 

def __iter__(self): 

r""" 

Return an iterator over the files in the directories. 

 

OUTPUT: 

 

Iterator over triples consisting of path, filename, and file 

extension. The iteration order only depends on the filenames 

and not on filesystem layout. 

 

EXAMPLES:: 

 

sage: from sage.tests.py3_syntax import Python3SyntaxTest 

sage: test = Python3SyntaxTest('sage/tests/french_book') 

sage: next(iter(test)) 

('...src/sage/tests/french_book', 'README', '') 

""" 

tree_walk = itertools.chain(*map(os.walk, self._directories)) 

for path, _, files in tree_walk: 

path = os.path.relpath(path) 

files.sort() 

for filename in files: 

if filename.startswith('.'): 

continue 

_, ext = os.path.splitext(filename) 

yield (path, filename, ext) 

 

def run_tests(self, *extensions): 

r""" 

Run :meth:`test` on each file with a matching extension. 

 

INPUT: 

 

- ``*extensions`` -- the file extensions (including the leading dot) to 

check 

 

EXAMPLES:: 

 

sage: from sage.tests.py3_syntax import SortedDirectoryWalkerABC 

sage: walker = SortedDirectoryWalkerABC('sage/tests/french_book') 

sage: walker.run_tests('.py') 

Traceback (most recent call last): 

... 

NotImplementedError: to be implemented in derived class 

""" 

buf = [] 

for (path, filename, ext) in self: 

if ext in extensions: 

fqn = os.path.join(path, filename) 

buf.append(fqn) 

if len(buf) > 20: 

self.test(*buf) 

buf = [] 

self.test(*buf) 

 

def test(self, *filenames): 

r""" 

Test the given filenames. 

 

To be implemented in derived classes. 

 

INPUT: 

 

- ``*filenames`` -- the fully qualified filenames to check 

 

EXAMPLES:: 

 

sage: from sage.tests.py3_syntax import SortedDirectoryWalkerABC 

sage: walker = SortedDirectoryWalkerABC() 

sage: from sage.env import SAGE_SRC 

sage: filename = os.path.join(SAGE_SRC, 'sage', 'tests', 'py3_syntax.py') 

sage: walker.test(filename) 

Traceback (most recent call last): 

... 

NotImplementedError: to be implemented in derived class 

""" 

raise NotImplementedError('to be implemented in derived class') 

 

 

PYTHON3_ENV = dict(os.environ) 

PYTHON3_ENV.pop('PYTHONPATH', None) 

 

class Python3SyntaxTest(SortedDirectoryWalkerABC): 

r""" 

Walks the directory tree and tests that all Python files are valid Python 3 

syntax. 

 

INPUT: 

 

- ``*directories`` -- roots of the directory trees to walk 

 

EXAMPLES:: 

 

sage: from sage.tests.py3_syntax import Python3SyntaxTest 

sage: walker = Python3SyntaxTest('sage/tests') 

sage: walker.run_tests('.py') # long time 

 

""" 

def test(self, *filenames): 

r""" 

Print an error message if the given files are not using valid Python 3 

syntax. 

 

INPUT: 

 

- ``*filenames`` -- the fully qualified filenames to check 

 

EXAMPLES:: 

 

sage: import os, tempfile 

sage: src = tempfile.NamedTemporaryFile(suffix='.py', delete=False) 

sage: _ = src.write('print "invalid print statement"') 

sage: src.close() 

sage: from sage.tests.py3_syntax import Python3SyntaxTest 

sage: py3_syntax = Python3SyntaxTest() 

sage: py3_syntax.test(src.name) 

Invalid Python 3 syntax found: 

File "...py", line 1 

print "invalid print statement" 

^ 

SyntaxError: Missing parentheses in call to 'print' 

sage: os.unlink(src.name) 

""" 

cmd = [ 

'python3', 

'-m', 'py_compile' 

] + list(filenames) 

process = subprocess.Popen( 

cmd, 

env=PYTHON3_ENV, 

stdout=subprocess.PIPE, 

stderr=subprocess.PIPE 

) 

stdout, stderr = process.communicate() 

if process.returncode == 0: 

return 

print('Invalid Python 3 syntax found:') 

if stdout: 

print(stdout) 

if stderr: 

print(stderr)