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
Gwenaël Samain
cython
Commits
88523684
Commit
88523684
authored
Aug 06, 2012
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
provide compiler directive to set __file__ and __path__ from the source path at module init time
parent
e7c0e635
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
69 additions
and
2 deletions
+69
-2
Cython/Compiler/ModuleNode.py
Cython/Compiler/ModuleNode.py
+23
-1
Cython/Compiler/Options.py
Cython/Compiler/Options.py
+4
-0
Cython/Compiler/Symtab.py
Cython/Compiler/Symtab.py
+1
-1
tests/run/initial_file_path.srctree
tests/run/initial_file_path.srctree
+41
-0
No files found.
Cython/Compiler/ModuleNode.py
View file @
88523684
...
@@ -22,7 +22,7 @@ import PyrexTypes
...
@@ -22,7 +22,7 @@ import PyrexTypes
from
Errors
import
error
,
warning
from
Errors
import
error
,
warning
from
PyrexTypes
import
py_object_type
from
PyrexTypes
import
py_object_type
from
Cython.Utils
import
open_new_file
,
replace_suffix
from
Cython.Utils
import
open_new_file
,
replace_suffix
,
decode_filename
from
Code
import
UtilityCode
from
Code
import
UtilityCode
from
StringEncoding
import
EncodedString
from
StringEncoding
import
EncodedString
...
@@ -1829,6 +1829,28 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
...
@@ -1829,6 +1829,28 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
code
.
error_goto
(
self
.
pos
)))
code
.
error_goto
(
self
.
pos
)))
code
.
putln
(
"}"
)
code
.
putln
(
"}"
)
if
env
.
directives
[
'set_initial_path_from_source'
]
and
self
.
pos
[
0
]:
source_path
=
self
.
pos
[
0
].
filename
if
source_path
:
code
.
putln
(
'if (__Pyx_SetAttrString(%s, "__file__", %s) < 0) %s;'
%
(
env
.
module_cname
,
code
.
globalstate
.
get_py_string_const
(
EncodedString
(
decode_filename
(
source_path
))).
cname
,
code
.
error_goto
(
self
.
pos
)))
if
os
.
path
.
splitext
(
os
.
path
.
basename
(
source_path
))[
0
]
==
'__init__'
:
# compiling a package => set __path__ as well
temp
=
code
.
funcstate
.
allocate_temp
(
py_object_type
,
True
)
code
.
putln
(
'%s = Py_BuildValue("[O]", %s); %s'
%
(
temp
,
code
.
globalstate
.
get_py_string_const
(
EncodedString
(
decode_filename
(
os
.
path
.
dirname
(
source_path
)))).
cname
,
code
.
error_goto_if_null
(
temp
,
self
.
pos
)))
code
.
putln
(
'if (__Pyx_SetAttrString(%s, "__path__", %s) < 0) %s;'
%
(
env
.
module_cname
,
temp
,
code
.
error_goto
(
self
.
pos
)))
code
.
funcstate
.
release_temp
(
temp
)
if
Options
.
cache_builtins
:
if
Options
.
cache_builtins
:
code
.
putln
(
"/*--- Builtin init code ---*/"
)
code
.
putln
(
"/*--- Builtin init code ---*/"
)
code
.
putln
(
code
.
error_goto_if_neg
(
"__Pyx_InitCachedBuiltins()"
,
self
.
pos
))
code
.
putln
(
code
.
error_goto_if_neg
(
"__Pyx_InitCachedBuiltins()"
,
self
.
pos
))
...
...
Cython/Compiler/Options.py
View file @
88523684
...
@@ -98,6 +98,9 @@ directive_defaults = {
...
@@ -98,6 +98,9 @@ directive_defaults = {
'fast_getattr'
:
False
,
# Undocumented until we come up with a better way to handle this everywhere.
'fast_getattr'
:
False
,
# Undocumented until we come up with a better way to handle this everywhere.
'py2_import'
:
False
,
# For backward compatibility of Cython's source code in Py3 source mode
'py2_import'
:
False
,
# For backward compatibility of Cython's source code in Py3 source mode
# set __file__ and/or __path__ to source file path at import time (instead of not having them available)
'set_initial_path_from_source'
:
False
,
'warn'
:
None
,
'warn'
:
None
,
'warn.undeclared'
:
False
,
'warn.undeclared'
:
False
,
'warn.unreachable'
:
True
,
'warn.unreachable'
:
True
,
...
@@ -153,6 +156,7 @@ directive_scopes = { # defaults to available everywhere
...
@@ -153,6 +156,7 @@ directive_scopes = { # defaults to available everywhere
'autotestdict'
:
(
'module'
,),
'autotestdict'
:
(
'module'
,),
'autotestdict.all'
:
(
'module'
,),
'autotestdict.all'
:
(
'module'
,),
'autotestdict.cdef'
:
(
'module'
,),
'autotestdict.cdef'
:
(
'module'
,),
'set_initial_path_from_source'
:
(
'module'
,),
'test_assert_path_exists'
:
(
'function'
,
'class'
,
'cclass'
),
'test_assert_path_exists'
:
(
'function'
,
'class'
,
'cclass'
),
'test_fail_if_path_exists'
:
(
'function'
,
'class'
,
'cclass'
),
'test_fail_if_path_exists'
:
(
'function'
,
'class'
,
'cclass'
),
}
}
...
...
Cython/Compiler/Symtab.py
View file @
88523684
...
@@ -991,7 +991,7 @@ class ModuleScope(Scope):
...
@@ -991,7 +991,7 @@ class ModuleScope(Scope):
self.cached_builtins = []
self.cached_builtins = []
self.undeclared_cached_builtins = []
self.undeclared_cached_builtins = []
self.namespace_cname = self.module_cname
self.namespace_cname = self.module_cname
for var_name in ['__builtins__', '__name__', '__file__', '__doc__']:
for var_name in ['__builtins__', '__name__', '__file__', '__doc__'
, '__path__'
]:
self.declare_var(EncodedString(var_name), py_object_type, None)
self.declare_var(EncodedString(var_name), py_object_type, None)
def qualifying_scope(self):
def qualifying_scope(self):
...
...
tests/run/initial_file_path.srctree
0 → 100644
View file @
88523684
PYTHON setup.py build_ext --inplace
PYTHON -c "import my_test_package, my_test_package.a; my_test_package.test(); my_test_package.a.test()"
######## setup.py ########
from Cython.Build.Dependencies import cythonize
from distutils.core import setup
setup(
ext_modules = cythonize("my_test_package/*.py"),
)
######## my_test_package/__init__.py ########
# cython: set_initial_path_from_source=True
initial_path = __path__
initial_file = __file__
import a
def test():
assert initial_path[0].endswith('my_test_package'), initial_path
assert initial_file.endswith('__init__.py'), initial_file
######## my_test_package/a.py ########
# cython: set_initial_path_from_source=True
initial_file = __file__
try:
initial_path = __path__
except NameError:
got_name_error = True
else:
got_name_error = False
def test():
assert initial_file.endswith('a.py'), initial_file
assert got_name_error, "looks like __path__ was set at module init time: " + initial_path
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