@@ -229,15 +229,15 @@ import os,sys,inspect,collections
229
229
cwd = os.getcwd()
230
230
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
231
231
os.chdir(currentdir)
232
- import _%[1 ]s
232
+ %[6 ]s
233
233
os.chdir(cwd)
234
234
235
235
# to use this code in your end-user python file, import it as follows:
236
236
# from %[1]s import %[3]s
237
237
# and then refer to everything using %[3]s. prefix
238
238
# packages imported by this package listed below:
239
239
240
- %[6 ]s
240
+ %[7 ]s
241
241
242
242
`
243
243
@@ -249,14 +249,15 @@ os.chdir(cwd)
249
249
# File is generated by gopy. Do not edit.
250
250
# %[2]s
251
251
252
- import _%[1]s, collections
252
+ import collections
253
+ %[6]s
253
254
254
255
# to use this code in your end-user python file, import it as follows:
255
256
# from %[1]s import %[3]s
256
257
# and then refer to everything using %[3]s. prefix
257
258
# packages imported by this package listed below:
258
259
259
- %[6 ]s
260
+ %[7 ]s
260
261
261
262
`
262
263
@@ -463,15 +464,11 @@ var NoMake = false
463
464
// and wrapper .py file(s) that are loaded as the interface to the package with shadow
464
465
// python-side classes
465
466
// mode = gen, build, pkg, exe
466
- func GenPyBind (mode BuildMode , odir , outname , cmdstr , vm , mainstr , libext , extragccargs string , lang int ) error {
467
+ func GenPyBind (mode BuildMode , libext , extragccargs string , lang int , cfg * BindCfg ) error {
467
468
gen := & pyGen {
468
469
mode : mode ,
469
- odir : odir ,
470
- pypkgname : outname ,
471
- outname : outname ,
472
- cmdstr : cmdstr ,
473
- vm : vm ,
474
- mainstr : mainstr ,
470
+ pypkgname : cfg .Name ,
471
+ cfg : cfg ,
475
472
libext : libext ,
476
473
extraGccArgs : extragccargs ,
477
474
lang : lang ,
@@ -497,22 +494,17 @@ type pyGen struct {
497
494
err ErrorList
498
495
pkgmap map [string ]struct {} // map of package paths
499
496
500
- mode BuildMode // mode: gen, build, pkg, exe
501
- odir string // output directory
502
- pypkgname string // python package name, for pkg and exe build modes (--name arg there), else = outname
503
- // all global functions in goPreamble are accessed by: pypkgname.FuncName, e.g., IncRef, DecRef
504
- outname string // output (package) name -- for specific current go package
505
- cmdstr string // overall command (embedded in generated files)
506
- vm string // python interpreter
507
- mainstr string // main function code string
497
+ mode BuildMode // mode: gen, build, pkg, exe
498
+ pypkgname string
499
+ cfg * BindCfg
508
500
libext string
509
501
extraGccArgs string
510
502
lang int // c-python api version (2,3)
511
503
}
512
504
513
505
func (g * pyGen ) gen () error {
514
506
g .pkg = nil
515
- err := os .MkdirAll (g .odir , 0755 )
507
+ err := os .MkdirAll (g .cfg . OutputDir , 0755 )
516
508
if err != nil {
517
509
return fmt .Errorf ("gopy: could not create output directory: %v" , err )
518
510
}
@@ -549,14 +541,14 @@ func (g *pyGen) genPre() {
549
541
if ! NoMake {
550
542
g .genMakefile ()
551
543
}
552
- oinit , err := os .Create (filepath .Join (g .odir , "__init__.py" ))
544
+ oinit , err := os .Create (filepath .Join (g .cfg . OutputDir , "__init__.py" ))
553
545
g .err .Add (err )
554
546
err = oinit .Close ()
555
547
g .err .Add (err )
556
548
}
557
549
558
550
func (g * pyGen ) genPrintOut (outfn string , pr * printer ) {
559
- of , err := os .Create (filepath .Join (g .odir , outfn ))
551
+ of , err := os .Create (filepath .Join (g .cfg . OutputDir , outfn ))
560
552
g .err .Add (err )
561
553
_ , err = io .Copy (of , pr )
562
554
g .err .Add (err )
@@ -565,10 +557,10 @@ func (g *pyGen) genPrintOut(outfn string, pr *printer) {
565
557
}
566
558
567
559
func (g * pyGen ) genOut () {
568
- g .pybuild .Printf ("\n mod.generate(open('%v.c', 'w'))\n \n " , g .outname )
560
+ g .pybuild .Printf ("\n mod.generate(open('%v.c', 'w'))\n \n " , g .cfg . Name )
569
561
g .gofile .Printf ("\n \n " )
570
562
g .genLeaksPostamble ()
571
- g .genPrintOut (g .outname + ".go" , g .gofile )
563
+ g .genPrintOut (g .cfg . Name + ".go" , g .gofile )
572
564
g .genPrintOut ("patch-leaks.go" , g .leakfile )
573
565
g .genPrintOut ("build.py" , g .pybuild )
574
566
if ! NoMake {
@@ -585,7 +577,7 @@ func (g *pyGen) genPkgWrapOut() {
585
577
if g .mode == ModeGen || g .mode == ModeBuild {
586
578
impstr += fmt .Sprintf ("import %s\n " , im )
587
579
} else {
588
- impstr += fmt .Sprintf ("from %s import %s\n " , g .outname , im )
580
+ impstr += fmt .Sprintf ("from %s import %s\n " , g .cfg . Name , im )
589
581
}
590
582
}
591
583
b := g .pywrap .buf .Bytes ()
@@ -615,7 +607,7 @@ func (g *pyGen) genGoPreamble() {
615
607
pkgimport += fmt .Sprintf ("\n \t %q" , pi )
616
608
}
617
609
libcfg := func () string {
618
- pycfg , err := GetPythonConfig (g .vm )
610
+ pycfg , err := GetPythonConfig (g .cfg . VM )
619
611
if err != nil {
620
612
panic (err )
621
613
}
@@ -629,29 +621,30 @@ func (g *pyGen) genGoPreamble() {
629
621
return pkgcfg
630
622
}()
631
623
632
- if g .mode == ModeExe && g .mainstr == "" {
633
- g .mainstr = "GoPyMainRun()" // default is just to run main
624
+ if g .mode == ModeExe && g .cfg . Main == "" {
625
+ g .cfg . Main = "GoPyMainRun()" // default is just to run main
634
626
}
635
627
exeprec := ""
636
628
exeprego := ""
637
629
if g .mode == ModeExe {
638
- exeprec = fmt .Sprintf (goExePreambleC , g .outname )
630
+ exeprec = fmt .Sprintf (goExePreambleC , g .cfg . Name )
639
631
exeprego = goExePreambleGo
640
632
}
641
- g .gofile .Printf (goPreamble , g .outname , g .cmdstr , libcfg , GoHandle , CGoHandle , pkgimport , g .mainstr , exeprec , exeprego )
642
- g .gofile .Printf ("\n // --- generated code for package: %[1]s below: ---\n \n " , g .outname )
633
+ g .gofile .Printf (goPreamble , g .cfg .Name , g .cfg .Cmd , libcfg , GoHandle , CGoHandle ,
634
+ pkgimport , g .cfg .Main , exeprec , exeprego )
635
+ g .gofile .Printf ("\n // --- generated code for package: %[1]s below: ---\n \n " , g .cfg .Name )
643
636
}
644
637
645
638
func (g * pyGen ) genLeaksPreamble () {
646
- g .leakfile .Printf (patchLeaksPreamble , g .outname , g .cmdstr )
639
+ g .leakfile .Printf (patchLeaksPreamble , g .cfg . Name , g .cfg . Cmd )
647
640
}
648
641
649
642
func (g * pyGen ) genLeaksPostamble () {
650
643
g .leakfile .Printf (patchLeaksPostamble )
651
644
}
652
645
653
646
func (g * pyGen ) genPyBuildPreamble () {
654
- g .pybuild .Printf (PyBuildPreamble , g .outname , g .cmdstr )
647
+ g .pybuild .Printf (PyBuildPreamble , g .cfg . Name , g .cfg . Cmd )
655
648
}
656
649
657
650
func (g * pyGen ) genPyWrapPreamble () {
@@ -666,14 +659,34 @@ func (g *pyGen) genPyWrapPreamble() {
666
659
}
667
660
668
661
// import other packages for other types that we might use
669
- impstr := ""
662
+ var impstr , impgenstr string
663
+ impgenNames := []string {"_" + g .cfg .Name , "go" }
670
664
switch {
671
665
case g .pkg .Name () == "go" :
672
- impstr += fmt .Sprintf (GoPkgDefs , g .outname )
666
+ if g .cfg .PkgPrefix != "" {
667
+ impgenstr += fmt .Sprintf ("from %s import %s\n " , g .cfg .PkgPrefix , "_" + g .cfg .Name )
668
+ } else {
669
+ impgenstr += fmt .Sprintf ("import %s\n " , "_" + g .cfg .Name )
670
+ }
671
+ impstr += fmt .Sprintf (GoPkgDefs , g .cfg .Name )
673
672
case g .mode == ModeGen || g .mode == ModeBuild :
674
- impstr += fmt .Sprintf ("import go\n " )
673
+ if g .cfg .PkgPrefix != "" {
674
+ for _ , name := range impgenNames {
675
+ impgenstr += fmt .Sprintf ("from %s import %s\n " , g .cfg .PkgPrefix , name )
676
+ }
677
+ } else {
678
+ for _ , name := range impgenNames {
679
+ impgenstr += fmt .Sprintf ("import %s\n " , name )
680
+ }
681
+ }
675
682
default :
676
- impstr += fmt .Sprintf ("from %s import go\n " , g .outname )
683
+ pkg := "." + g .cfg .Name
684
+ if g .cfg .PkgPrefix != "" {
685
+ pkg = g .cfg .PkgPrefix + pkg
686
+ }
687
+ for _ , name := range impgenNames {
688
+ impgenstr += fmt .Sprintf ("from %s import %s\n " , pkg , name )
689
+ }
677
690
}
678
691
imps := g .pkg .pkg .Imports ()
679
692
for _ , im := range imps {
@@ -685,9 +698,9 @@ func (g *pyGen) genPyWrapPreamble() {
685
698
impstr += importHereKeyString
686
699
687
700
if g .mode == ModeExe {
688
- g .pywrap .Printf (PyWrapExePreamble , g .outname , g .cmdstr , n , pkgimport , pkgDoc , impstr )
701
+ g .pywrap .Printf (PyWrapExePreamble , g .cfg . Name , g .cfg . Cmd , n , pkgimport , pkgDoc , impgenstr , impstr )
689
702
} else {
690
- g .pywrap .Printf (PyWrapPreamble , g .outname , g .cmdstr , n , pkgimport , pkgDoc , impstr )
703
+ g .pywrap .Printf (PyWrapPreamble , g .cfg . Name , g .cfg . Cmd , n , pkgimport , pkgDoc , impgenstr , impstr )
691
704
}
692
705
}
693
706
@@ -708,18 +721,18 @@ func CmdStrToMakefile(cmdstr string) string {
708
721
}
709
722
710
723
func (g * pyGen ) genMakefile () {
711
- gencmd := strings .Replace (g .cmdstr , "gopy build" , "gopy gen" , 1 )
724
+ gencmd := strings .Replace (g .cfg . Cmd , "gopy build" , "gopy gen" , 1 )
712
725
gencmd = CmdStrToMakefile (gencmd )
713
726
714
- pycfg , err := GetPythonConfig (g .vm )
727
+ pycfg , err := GetPythonConfig (g .cfg . VM )
715
728
if err != nil {
716
729
panic (err )
717
730
}
718
731
719
732
if g .mode == ModeExe {
720
- g .makefile .Printf (MakefileExeTemplate , g .outname , g .cmdstr , gencmd , g .vm , g .libext , pycfg .CFlags , pycfg .LdFlags )
733
+ g .makefile .Printf (MakefileExeTemplate , g .cfg . Name , g .cfg . Cmd , gencmd , g .cfg . VM , g .libext , pycfg .CFlags , pycfg .LdFlags )
721
734
} else {
722
- g .makefile .Printf (MakefileTemplate , g .outname , g .cmdstr , gencmd , g .vm , g .libext , g .extraGccArgs , pycfg .CFlags , pycfg .LdFlags )
735
+ g .makefile .Printf (MakefileTemplate , g .cfg . Name , g .cfg . Cmd , gencmd , g .cfg . VM , g .libext , g .extraGccArgs , pycfg .CFlags , pycfg .LdFlags )
723
736
}
724
737
}
725
738
0 commit comments