Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cython
Commits
2a304b0f
Commit
2a304b0f
authored
Feb 06, 2022
by
Matti Picus
Committed by
GitHub
Feb 06, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use context manager when opening files instead of leaving a file dangling open. (GH-4621)
parent
2c7c53b4
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
45 additions
and
30 deletions
+45
-30
Cython/Parser/ConcreteSyntaxTree.pyx
Cython/Parser/ConcreteSyntaxTree.pyx
+12
-9
pyximport/pyximport.py
pyximport/pyximport.py
+4
-2
pyximport/test/test_pyximport.py
pyximport/test/test_pyximport.py
+14
-11
pyximport/test/test_reload.py
pyximport/test/test_reload.py
+4
-2
runtests.py
runtests.py
+5
-2
tests/build/common_include_dir.srctree
tests/build/common_include_dir.srctree
+4
-3
tests/run/cpdef_pickle.srctree
tests/run/cpdef_pickle.srctree
+2
-1
No files found.
Cython/Parser/ConcreteSyntaxTree.pyx
View file @
2a304b0f
...
...
@@ -32,7 +32,8 @@ import re
def
extract_names
(
path
):
# All parse tree types are #defined in these files as ints.
type_names
=
{}
for
line
in
open
(
path
):
with
open
(
path
)
as
fid
:
for
line
in
fid
:
if
line
.
startswith
(
'#define'
):
try
:
_
,
name
,
value
=
line
.
strip
().
split
()
...
...
@@ -61,7 +62,8 @@ def handle_includes(source, path):
included
=
os
.
path
.
join
(
os
.
path
.
dirname
(
path
),
include_line
.
group
(
1
)[
1
:
-
1
])
if
not
os
.
path
.
exists
(
included
):
return
include_line
.
group
(
0
)
+
' # no such path: '
+
included
return
handle_includes
(
open
(
included
).
read
(),
path
)
with
open
(
included
)
as
fid
:
return
handle_includes
(
fid
.
read
(),
path
)
# TODO: Proper string tokenizing.
return
re
.
sub
(
r'^include\
s+([^
\n]+[\'"])\
s*(#.*)?$
', include_here, source, flags=re.M)
...
...
@@ -69,7 +71,8 @@ def p_module(path):
cdef perrdetail err
cdef int flags
cdef node* n
source = open(path).read()
with open(path) as fid:
source = fid.read()
if '
\
ninclude
' in source:
# TODO: Tokanizer needs to understand includes.
source = handle_includes(source, path)
...
...
pyximport/pyximport.py
View file @
2a304b0f
...
...
@@ -117,7 +117,8 @@ def handle_special_build(modname, pyxfilename):
# locs = {}
# execfile(special_build, globls, locs)
# ext = locs["make_ext"](modname, pyxfilename)
mod
=
imp
.
load_source
(
"XXXX"
,
special_build
,
open
(
special_build
))
with
open
(
special_build
)
as
fid
:
mod
=
imp
.
load_source
(
"XXXX"
,
special_build
,
fid
)
make_ext
=
getattr
(
mod
,
'make_ext'
,
None
)
if
make_ext
:
ext
=
make_ext
(
modname
,
pyxfilename
)
...
...
@@ -144,7 +145,8 @@ def handle_dependencies(pyxfilename):
# but we know more about dependencies so force a rebuild if
# some of the dependencies are newer than the pyxfile.
if
os
.
path
.
exists
(
dependfile
):
depends
=
open
(
dependfile
).
readlines
()
with
open
(
dependfile
)
as
fid
:
depends
=
fid
.
readlines
()
depends
=
[
depend
.
strip
()
for
depend
in
depends
]
# gather dependencies in the "files" variable
...
...
pyximport/test/test_pyximport.py
View file @
2a304b0f
...
...
@@ -41,33 +41,36 @@ def test_with_reload():
tempdir
=
make_tempdir
()
sys
.
path
.
append
(
tempdir
)
filename
=
os
.
path
.
join
(
tempdir
,
"dummy.pyx"
)
open
(
filename
,
"w"
).
write
(
"print 'Hello world from the Pyrex install hook'"
)
with
open
(
filename
,
"w"
)
as
fid
:
fid
.
write
(
"print 'Hello world from the Pyrex install hook'"
)
import
dummy
reload
(
dummy
)
depend_filename
=
os
.
path
.
join
(
tempdir
,
"dummy.pyxdep"
)
depend_file
=
open
(
depend_filename
,
"w"
)
with
open
(
depend_filename
,
"w"
)
as
depend_file
:
depend_file
.
write
(
"*.txt
\
n
foo.bar"
)
depend_file
.
close
()
build_filename
=
os
.
path
.
join
(
tempdir
,
"dummy.pyxbld"
)
build_file
=
open
(
build_filename
,
"w"
)
with
open
(
build_filename
,
"w"
)
as
build_file
:
build_file
.
write
(
"""
from distutils.extension import Extension
def make_ext(name, filename):
return Extension(name=name, sources=[filename])
"""
)
build_file
.
close
()
open
(
os
.
path
.
join
(
tempdir
,
"foo.bar"
),
"w"
).
write
(
" "
)
open
(
os
.
path
.
join
(
tempdir
,
"1.txt"
),
"w"
).
write
(
" "
)
open
(
os
.
path
.
join
(
tempdir
,
"abc.txt"
),
"w"
).
write
(
" "
)
with
open
(
os
.
path
.
join
(
tempdir
,
"foo.bar"
),
"w"
)
as
fid
:
fid
.
write
(
" "
)
with
open
(
os
.
path
.
join
(
tempdir
,
"1.txt"
),
"w"
)
as
fid
:
fid
.
write
(
" "
)
with
open
(
os
.
path
.
join
(
tempdir
,
"abc.txt"
),
"w"
)
as
fid
:
fid
.
write
(
" "
)
reload
(
dummy
)
assert
len
(
pyximport
.
_test_files
)
==
1
,
pyximport
.
_test_files
reload
(
dummy
)
time
.
sleep
(
1
)
# sleep a second to get safer mtimes
open
(
os
.
path
.
join
(
tempdir
,
"abc.txt"
),
"w"
).
write
(
" "
)
with
open
(
os
.
path
.
join
(
tempdir
,
"abc.txt"
),
"w"
)
as
fid
:
fid
.
write
(
" "
)
print
(
"Here goes the reload"
)
reload
(
dummy
)
assert
len
(
pyximport
.
_test_files
)
==
1
,
pyximport
.
_test_files
...
...
pyximport/test/test_reload.py
View file @
2a304b0f
...
...
@@ -18,14 +18,16 @@ def test():
tempdir
=
test_pyximport
.
make_tempdir
()
sys
.
path
.
append
(
tempdir
)
hello_file
=
os
.
path
.
join
(
tempdir
,
"hello.pyx"
)
open
(
hello_file
,
"w"
).
write
(
"x = 1; print x; before = 'before'
\
n
"
)
with
open
(
hello_file
,
"w"
)
as
fid
:
fid
.
write
(
"x = 1; print x; before = 'before'
\
n
"
)
import
hello
assert
hello
.
x
==
1
time
.
sleep
(
1
)
# sleep to make sure that new "hello.pyx" has later
# timestamp than object file.
open
(
hello_file
,
"w"
).
write
(
"x = 2; print x; after = 'after'
\
n
"
)
with
open
(
hello_file
,
"w"
)
as
fid
:
fid
.
write
(
"x = 2; print x; after = 'after'
\
n
"
)
reload
(
hello
)
assert
hello
.
x
==
2
,
"Reload should work on Python 2.3 but not 2.2"
test_pyximport
.
remove_tempdir
(
tempdir
)
...
...
runtests.py
View file @
2a304b0f
...
...
@@ -1352,8 +1352,11 @@ class CythonCompileTestCase(unittest.TestCase):
self.run_cython(test_directory, module, module_path, workdir2, incdir, annotate)
diffs = []
for file in os.listdir(workdir2):
if (open(os.path.join(workdir, file)).read()
!= open(os.path.join(workdir2, file)).read()):
with open(os.path.join(workdir, file)) as fid:
txt1 = fid.read()
with open(os.path.join(workdir2, file)) as fid:
txt2 = fid.read()
if txt1 != txt2:
diffs.append(file)
os.system('diff -u %s/%s %s/%s > %s/%s.diff' % (
workdir, file,
...
...
tests/build/common_include_dir.srctree
View file @
2a304b0f
...
...
@@ -78,7 +78,8 @@ if sys.platform == 'win32':
assert opt == '-c'
count = 0
regex = re.compile(pattern)
for line in open(file):
with open(file) as fid:
for line in fid:
if regex.search(line):
count += 1
print(count)
...
...
tests/run/cpdef_pickle.srctree
View file @
2a304b0f
...
...
@@ -56,7 +56,8 @@ from lib.cy import WithoutC, WithCPDef, WithCDefWrapper
def tryThis(obj):
print("Pickling %s ..." % obj.__class__.__name__)
try:
pkl.dump(obj, open("test.pkl", "wb"))
with open("test.pkl", "wb") as fid:
pkl.dump(obj, fid)
print("\t... OK")
except Exception as e:
print("\t... KO: %s" % str(e))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment