@@ -64,52 +64,36 @@ def init():
6464 "For updates, see https://github.com/pytorch/pytorch/issues/78829."
6565 )
6666
67- def flag (name ):
68- """Get a boolean flag.
69- Flags can be set as command line arguments '-X juliacall-{name}=yes|no' or as
70- environment variables 'PYTHON_JULIACALL_{NAME}=yes|no'.
71- """
72- val , source = _get_config_var (name )
73- if val is None :
74- return None , ''
75-
76- if val == "yes" : return True , source
77- if val == "no" : return False , source
78-
79- raise ValueError (f"{ source } : Invalid flag '{ val } '. Use 'yes' or 'no'." )
80-
81- def option (name , default = None , xkey = None , envkey = None ):
67+ def option (name , default = None , xkey = None , envkey = None , allowflag = False ):
8268 """Get an option.
8369
8470 Options can be set as command line arguments '-X juliacall-{name}={value}' or as
8571 environment variables 'PYTHON_JULIACALL_{NAME}={value}'.
8672 """
87- val , source = _get_config_var (name , xkey = xkey , envkey = envkey )
88- if val is not None :
89- return val , f'{ source } ={ val } '
90-
91- return default , f'<default>={ default } '
92-
93- def _get_config_var (name , xkey = None , envkey = None ):
94- """
95- Retrieves a config value, checking -X options first, then Environment variables.
96- Returns (value, source_description).
97- """
98- if xkey is None :
99- xkey = 'juliacall-' + name .lower ().replace ('_' , '-' )
100-
101- v = sys ._xoptions .get (xkey )
73+ k = xkey or 'juliacall-' + name .lower ().replace ('_' , '-' )
74+ v = sys ._xoptions .get (k )
10275 if v is not None :
103- return v , f'-X{ xkey } '
104-
105- if envkey is None :
106- envkey = 'PYTHON_JULIACALL_' + name .upper ()
107-
108- v = os .getenv (envkey )
76+ if v is True :
77+ if not allowflag :
78+ raise ValueError (f'-X{ k } : expecting an argument' )
79+ return True , f'-X{ k } '
80+ return v , f'-X{ k } ={ v } '
81+ k = envkey or 'PYTHON_JULIACALL_' + name .upper ()
82+ v = os .getenv (k )
10983 if v is not None :
110- return v , envkey
84+ return v , f'{ k } ={ v } '
85+ return default , f'<default>={ default } '
11186
112- return None , ''
87+ def flag (name , default = None , ** kw ):
88+ v , s = option (name , allowflag = True , ** kw )
89+ if v is None :
90+ return default , s
91+ elif v is True or v == 'yes' :
92+ return True , s
93+ elif v == 'no' :
94+ return False , s
95+ else :
96+ raise ValueError (f'{ s } : expecting yes or no' )
11397
11498 def choice (name , choices , default = None , ** kw ):
11599 v , s = option (name , ** kw )
@@ -120,24 +104,13 @@ def choice(name, choices, default=None, **kw):
120104 raise ValueError (
121105 f'{ s } : expecting one of { ", " .join (choices )} ' )
122106
123- def path_or_keyword_option (name , keywords , check_exists = False , default = None , ** kw ):
124- v , s = option (name , ** kw )
125- if v is None :
126- return default , s
127- if v in keywords :
128- return v , s
129- return _path (v , check_exists = check_exists ), s
130-
131107 def path_option (name , default = None , check_exists = False , ** kw ):
132108 path , s = option (name , ** kw )
133109 if path is not None :
134- return _path (path , check_exists = check_exists ), s
110+ if check_exists and not os .path .exists (path ):
111+ raise ValueError (f'{ s } : path does not exist' )
112+ return os .path .abspath (path ), s
135113 return default , s
136-
137- def _path (path , check_exists = False ):
138- if check_exists and not os .path .exists (path ):
139- raise ValueError (f'{ path } : path does not exist' )
140- return os .path .abspath (path )
141114
142115 def executable_option (name , default = None , ** kw ):
143116 import shutil
@@ -173,8 +146,6 @@ def args_from_config(config):
173146 arg = '--' + opt [4 :].replace ('_' , '-' )
174147 if val is True :
175148 argv .append (arg )
176- elif val is False :
177- continue
178149 elif val is not None :
179150 argv .append (f"{ arg } ={ val } " )
180151 argv = [s .encode ("utf-8" ) for s in argv ]
@@ -193,7 +164,7 @@ def args_from_config(config):
193164 CONFIG ['opt_home' ] = bindir = path_option ('home' , check_exists = True , envkey = 'PYTHON_JULIACALL_BINDIR' )[0 ]
194165 CONFIG ['opt_check_bounds' ] = choice ('check_bounds' , ['yes' , 'no' , 'auto' ])[0 ]
195166 CONFIG ['opt_compile' ] = choice ('compile' , ['yes' , 'no' , 'all' , 'min' ])[0 ]
196- CONFIG ["opt_trace_compile" ] = path_or_keyword_option ('trace_compile' , [ 'stderr' ], check_exists = False )[0 ]
167+ CONFIG ["opt_trace_compile" ] = option ('trace_compile' )[0 ]
197168 CONFIG ["opt_trace_compile_timing" ] = flag ('trace_compile_timing' )[0 ]
198169 CONFIG ['opt_compiled_modules' ] = choice ('compiled_modules' , ['yes' , 'no' ])[0 ]
199170 CONFIG ['opt_depwarn' ] = choice ('depwarn' , ['yes' , 'no' , 'error' ])[0 ]
0 commit comments