@@ -450,6 +450,16 @@ def run_main(comline_list):
450
450
f2pydir = os .path .dirname (os .path .abspath (cfuncs .__file__ ))
451
451
fobjhsrc = os .path .join (f2pydir , 'src' , 'fortranobject.h' )
452
452
fobjcsrc = os .path .join (f2pydir , 'src' , 'fortranobject.c' )
453
+ # gh-22819 -- begin
454
+ parser = make_f2py_parser ()
455
+ args , comline_list = parser .parse_known_args (comline_list )
456
+ pyf_files , _ = filter_files ("" , "[.]pyf([.]src|)" , comline_list )
457
+ # Checks that no existing modulename is defined in a pyf file
458
+ # TODO: Remove all this when scaninputline is replaced
459
+ if "-h" not in comline_list and args .module_name : # Can't check what doesn't exist yet, -h creates the pyf
460
+ modname = validate_modulename (pyf_files , args .module_name )
461
+ comline_list += ['-m' , modname ] # needed for the rest of scaninputline
462
+ # gh-22819 -- end
453
463
files , options = scaninputline (comline_list )
454
464
auxfuncs .options = options
455
465
capi_maps .load_f2cmap_file (options ['f2cmap_file' ])
@@ -516,24 +526,30 @@ def get_prefix(module):
516
526
p = os .path .dirname (os .path .dir
8000
name (module .__file__ ))
517
527
return p
518
528
519
- def preparse_sysargv ():
520
- # To keep backwards bug compatibility, newer flags are handled by argparse,
521
- # and `sys.argv` is passed to the rest of `f2py` as is.
529
+ def make_f2py_parser ():
522
530
parser = argparse .ArgumentParser (add_help = False )
523
531
parser .add_argument ("--dep" , action = "append" , dest = "dependencies" )
524
532
parser .add_argument ("--backend" , choices = ['meson' , 'distutils' ], default = 'distutils' )
533
+ parser .add_argument ("-m" , dest = "module_name" )
534
+ return parser
535
+
536
+ def preparse_sysargv ():
537
+ # To keep backwards bug compatibility, newer flags are handled by argparse,
538
+ # and `sys.argv` is passed to the rest of `f2py` as is.
539
+ parser = make_f2py_parser ()
525
540
526
541
args , remaining_argv = parser .parse_known_args ()
527
542
sys .argv = [sys .argv [0 ]] + remaining_argv
528
543
529
544
backend_key = args .backend
530
545
if sys .version_info >= (3 , 12 ) and backend_key == 'distutils' :
531
- outmess (' Cannot use distutils backend with Python 3.12, using meson backend instead.' )
546
+ outmess (" Cannot use distutils backend with Python 3.12, using meson backend instead.\n " )
532
547
backend_key = 'meson'
533
548
534
549
return {
535
550
"dependencies" : args .dependencies or [],
536
- "backend" : backend_key
551
+ "backend" : backend_key ,
552
+ "modulename" : args .module_name ,
537
553
}
538
554
539
555
def run_compile ():
@@ -544,11 +560,11 @@ def run_compile():
544
560
545
561
# Collect dependency flags, preprocess sys.argv
546
562
argy = preparse_sysargv ()
563
+ modulename = argy ["modulename" ]
547
564
dependencies = argy ["dependencies" ]
548
565
backend_key = argy ["backend" ]
549
566
build_backend = f2py_build_generator (backend_key )
550
567
551
-
552
568
i = sys .argv .index ('-c' )
553
569
del sys .argv [i ]
554
570
@@ -628,30 +644,17 @@ def run_compile():
628
644
if '--quiet' in f2py_flags :
629
645
setup_flags .append ('--quiet' )
630
646
631
- modulename = 'untitled'
632
647
sources = sys .argv [1 :]
633
-
634
648
for optname in ['--include_paths' , '--include-paths' , '--f2cmap' ]:
635
649
if optname in sys .argv :
636
650
i = sys .argv .index (optname )
637
651
f2py_flags .extend (sys .argv [i :i + 2 ])
638
652
del sys .argv [i + 1 ], sys .argv [i ]
639
653
sources = sys .argv [1 :]
640
654
641
- pyf_files = []
642
- if '-m' in sys .argv :
643
- i = sys .argv .index ('-m' )
644
- modulename = sys .argv [i + 1 ]
645
- del sys .argv [i + 1 ], sys .argv [i ]
646
- sources = sys .argv [1 :]
647
- else :
648
- pyf_files , _sources = filter_files ('' , '[.]pyf([.]src|)' , sources )
649
- sources = pyf_files + _sources
650
- for f in pyf_files :
651
- modulename = auxfuncs .get_f2py_modulename (f )
652
- if modulename :
653
- break
654
-
655
+ pyf_files , _sources = filter_files ("" , "[.]pyf([.]src|)" , sources )
656
+ sources = pyf_files + _sources
657
+ modulename = validate_modulename (pyf_files , modulename )
655
658
extra_objects , sources = filter_files ('' , '[.](o|a|so|dylib)' , sources )
656
659
include_dirs , sources = filter_files ('-I' , '' , sources , remove_prefix = 1 )
657
660
library_dirs , sources = filter_files ('-L' , '' , sources , remove_prefix = 1 )
@@ -698,6 +701,21 @@ def run_compile():
698
701
699
702
builder .compile ()
700
703
704
+
705
+ def validate_modulename (pyf_files , modulename = 'untitled' ):
706
+ if len (pyf_files ) > 1 :
707
+ raise ValueError ("Only one .pyf file per call" )
708
+ if pyf_files :
709
+ pyff = pyf_files [0 ]
710
+ pyf_modname = auxfuncs .get_f2py_modulename (pyff )
711
+ if modulename != pyf_modname :
712
+ outmess (
713
+ f"Ignoring -m { modulename } .\n "
714
+ f"{ pyff } defines { pyf_modname } to be the modulename.\n "
715
+ )
716
+ modulename = pyf_modname
717
+ return modulename
718
+
701
719
def main ():
702
720
if '--help-link' in sys .argv [1 :]:
703
721
sys .argv .remove ('--help-link' )
0 commit comments