@@ -229,15 +229,15 @@ import os,sys,inspect,collections
229229cwd = os.getcwd()
230230currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
231231os.chdir(currentdir)
232- import _%[1 ]s
232+ %[6 ]s
233233os.chdir(cwd)
234234
235235# to use this code in your end-user python file, import it as follows:
236236# from %[1]s import %[3]s
237237# and then refer to everything using %[3]s. prefix
238238# packages imported by this package listed below:
239239
240- %[6 ]s
240+ %[7 ]s
241241
242242`
243243
@@ -249,14 +249,15 @@ os.chdir(cwd)
249249# File is generated by gopy. Do not edit.
250250# %[2]s
251251
252- import _%[1]s, collections
252+ import collections
253+ %[6]s
253254
254255# to use this code in your end-user python file, import it as follows:
255256# from %[1]s import %[3]s
256257# and then refer to everything using %[3]s. prefix
257258# packages imported by this package listed below:
258259
259- %[6 ]s
260+ %[7 ]s
260261
261262`
262263
@@ -463,15 +464,11 @@ var NoMake = false
463464// and wrapper .py file(s) that are loaded as the interface to the package with shadow
464465// python-side classes
465466// 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 {
467468 gen := & pyGen {
468469 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 ,
475472 libext : libext ,
476473 extraGccArgs : extragccargs ,
477474 lang : lang ,
@@ -497,22 +494,17 @@ type pyGen struct {
497494 err ErrorList
498495 pkgmap map [string ]struct {} // map of package paths
499496
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
508500 libext string
509501 extraGccArgs string
510502 lang int // c-python api version (2,3)
511503}
512504
513505func (g * pyGen ) gen () error {
514506 g .pkg = nil
515- err := os .MkdirAll (g .odir , 0755 )
507+ err := os .MkdirAll (g .cfg . OutputDir , 0755 )
516508 if err != nil {
517509 return fmt .Errorf ("gopy: could not create output directory: %v" , err )
518510 }
@@ -549,14 +541,14 @@ func (g *pyGen) genPre() {
549541 if ! NoMake {
550542 g .genMakefile ()
551543 }
552- oinit , err := os .Create (filepath .Join (g .odir , "__init__.py" ))
544+ oinit , err := os .Create (filepath .Join (g .cfg . OutputDir , "__init__.py" ))
553545 g .err .Add (err )
554546 err = oinit .Close ()
555547 g .err .Add (err )
556548}
557549
558550func (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 ))
560552 g .err .Add (err )
561553 _ , err = io .Copy (of , pr )
562554 g .err .Add (err )
@@ -565,10 +557,10 @@ func (g *pyGen) genPrintOut(outfn string, pr *printer) {
565557}
566558
567559func (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 )
569561 g .gofile .Printf ("\n \n " )
570562 g .genLeaksPostamble ()
571- g .genPrintOut (g .outname + ".go" , g .gofile )
563+ g .genPrintOut (g .cfg . Name + ".go" , g .gofile )
572564 g .genPrintOut ("patch-leaks.go" , g .leakfile )
573565 g .genPrintOut ("build.py" , g .pybuild )
574566 if ! NoMake {
@@ -585,7 +577,7 @@ func (g *pyGen) genPkgWrapOut() {
585577 if g .mode == ModeGen || g .mode == ModeBuild {
586578 impstr += fmt .Sprintf ("import %s\n " , im )
587579 } 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 )
589581 }
590582 }
591583 b := g .pywrap .buf .Bytes ()
@@ -615,7 +607,7 @@ func (g *pyGen) genGoPreamble() {
615607 pkgimport += fmt .Sprintf ("\n \t %q" , pi )
616608 }
617609 libcfg := func () string {
618- pycfg , err := GetPythonConfig (g .vm )
610+ pycfg , err := GetPythonConfig (g .cfg . VM )
619611 if err != nil {
620612 panic (err )
621613 }
@@ -629,29 +621,30 @@ func (g *pyGen) genGoPreamble() {
629621 return pkgcfg
630622 }()
631623
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
634626 }
635627 exeprec := ""
636628 exeprego := ""
637629 if g .mode == ModeExe {
638- exeprec = fmt .Sprintf (goExePreambleC , g .outname )
630+ exeprec = fmt .Sprintf (goExePreambleC , g .cfg . Name )
639631 exeprego = goExePreambleGo
640632 }
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 )
643636}
644637
645638func (g * pyGen ) genLeaksPreamble () {
646- g .leakfile .Printf (patchLeaksPreamble , g .outname , g .cmdstr )
639+ g .leakfile .Printf (patchLeaksPreamble , g .cfg . Name , g .cfg . Cmd )
647640}
648641
649642func (g * pyGen ) genLeaksPostamble () {
650643 g .leakfile .Printf (patchLeaksPostamble )
651644}
652645
653646func (g * pyGen ) genPyBuildPreamble () {
654- g .pybuild .Printf (PyBuildPreamble , g .outname , g .cmdstr )
647+ g .pybuild .Printf (PyBuildPreamble , g .cfg . Name , g .cfg . Cmd )
655648}
656649
657650func (g * pyGen ) genPyWrapPreamble () {
@@ -666,14 +659,34 @@ func (g *pyGen) genPyWrapPreamble() {
666659 }
667660
668661 // import other packages for other types that we might use
669- impstr := ""
662+ var impstr , impgenstr string
663+ impgenNames := []string {"_" + g .cfg .Name , "go" }
670664 switch {
671665 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 )
673672 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+ }
675682 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+ }
677690 }
678691 imps := g .pkg .pkg .Imports ()
679692 for _ , im := range imps {
@@ -685,9 +698,9 @@ func (g *pyGen) genPyWrapPreamble() {
685698 impstr += importHereKeyString
686699
687700 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 )
689702 } 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 )
691704 }
692705}
693706
@@ -708,18 +721,18 @@ func CmdStrToMakefile(cmdstr string) string {
708721}
709722
710723func (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 )
712725 gencmd = CmdStrToMakefile (gencmd )
713726
714- pycfg , err := GetPythonConfig (g .vm )
727+ pycfg , err := GetPythonConfig (g .cfg . VM )
715728 if err != nil {
716729 panic (err )
717730 }
718731
719732 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 )
721734 } 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 )
723736 }
724737}
725738
0 commit comments