Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Allow embed option to take function name.

  • Loading branch information...
commit 4350d7f2807b45021eb480b4b3e3fb52eb58218f 1 parent 4e6da5a
Robert Bradshaw authored
10  Cython/Compiler/CmdLine.py
@@ -34,7 +34,7 @@
34 34
   -a, --annotate                 Produce a colorized HTML version of the source.
35 35
   --line-directives              Produce #line directives pointing to the .pyx source
36 36
   --cplus                        Output a C++ rather than C file.
37  
-  --embed                        Generate a main() function that embeds the Python interpreter.
  37
+  --embed[=<method_name>]        Generate a main() function that embeds the Python interpreter.
38 38
   -2                             Compile based on Python-2 syntax and code semantics.
39 39
   -3                             Compile based on Python-3 syntax and code semantics.
40 40
   --fast-fail                    Abort the compilation on the first error
@@ -84,8 +84,12 @@ def get_param(option):
84 84
                 options.use_listing_file = 1
85 85
             elif option in ("-+", "--cplus"):
86 86
                 options.cplus = 1
87  
-            elif option == "--embed":
88  
-                Options.embed = True
  87
+            elif option.startswith("--embed"):
  88
+                ix = option.find('=')
  89
+                if ix == -1:
  90
+                    Options.embed = "main"
  91
+                else:
  92
+                    Options.embed = option[ix+1:]
89 93
             elif option.startswith("-I"):
90 94
                 options.include_path.append(get_param(option))
91 95
             elif option == "--include-dir":
80  Cython/Compiler/ModuleNode.py
@@ -1897,7 +1897,16 @@ def generate_module_cleanup_func(self, env, code):
1897 1897
 
1898 1898
     def generate_main_method(self, env, code):
1899 1899
         module_is_main = "%s%s" % (Naming.module_is_main, self.full_module_name.replace('.', '__'))
1900  
-        code.globalstate.use_utility_code(main_method.specialize(module_name=env.module_name, module_is_main=module_is_main))
  1900
+        if Options.embed == "main":
  1901
+            wmain = "wmain"
  1902
+        else:
  1903
+            wmain = Options.embed
  1904
+        code.globalstate.use_utility_code(
  1905
+            main_method.specialize(
  1906
+                module_name = env.module_name,
  1907
+                module_is_main = module_is_main,
  1908
+                main_method = Options.embed,
  1909
+                wmain_method = wmain))
1901 1910
 
1902 1911
     def generate_pymoduledef_struct(self, env, code):
1903 1912
         if env.doc:
@@ -2646,9 +2655,9 @@ def generate_typeptr_assignment_code(self, entry, code):
2646 2655
 #endif
2647 2656
 
2648 2657
 #if PY_MAJOR_VERSION < 3
2649  
-int main(int argc, char** argv) {
  2658
+int %(main_method)s(int argc, char** argv) {
2650 2659
 #elif defined(WIN32) || defined(MS_WINDOWS)
2651  
-int wmain(int argc, wchar_t **argv) {
  2660
+int %(wmain_method)s(int argc, wchar_t **argv) {
2652 2661
 #else
2653 2662
 static int __Pyx_main(int argc, wchar_t **argv) {
2654 2663
 #endif
@@ -2665,9 +2674,13 @@ def generate_typeptr_assignment_code(self, entry, code):
2665 2674
     m = fpgetmask();
2666 2675
     fpsetmask(m & ~FP_X_OFL);
2667 2676
 #endif
2668  
-    Py_SetProgramName(argv[0]);
  2677
+    if (argc) {
  2678
+        Py_SetProgramName(argv[0]);
  2679
+    }
2669 2680
     Py_Initialize();
2670  
-    PySys_SetArgv(argc, argv);
  2681
+    if (argc) {
  2682
+        PySys_SetArgv(argc, argv);
  2683
+    }
2671 2684
     %(module_is_main)s = 1;
2672 2685
 #if PY_MAJOR_VERSION < 3
2673 2686
         init%(module_name)s();
@@ -2794,33 +2807,38 @@ def generate_typeptr_assignment_code(self, entry, code):
2794 2807
 }
2795 2808
 
2796 2809
 int
2797  
-main(int argc, char **argv)
  2810
+%(main_method)s(int argc, char **argv)
2798 2811
 {
2799  
-	wchar_t **argv_copy = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
2800  
-	/* We need a second copies, as Python might modify the first one. */
2801  
-	wchar_t **argv_copy2 = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
2802  
-	int i, res;
2803  
-	char *oldloc;
2804  
-	if (!argv_copy || !argv_copy2) {
2805  
-		fprintf(stderr, "out of memory\\n");
2806  
-		return 1;
2807  
-	}
2808  
-	oldloc = strdup(setlocale(LC_ALL, NULL));
2809  
-	setlocale(LC_ALL, "");
2810  
-	for (i = 0; i < argc; i++) {
2811  
-		argv_copy2[i] = argv_copy[i] = __Pyx_char2wchar(argv[i]);
2812  
-		if (!argv_copy[i])
2813  
-			return 1;
2814  
-	}
2815  
-	setlocale(LC_ALL, oldloc);
2816  
-	free(oldloc);
2817  
-	res = __Pyx_main(argc, argv_copy);
2818  
-	for (i = 0; i < argc; i++) {
2819  
-		free(argv_copy2[i]);
2820  
-	}
2821  
-	free(argv_copy);
2822  
-	free(argv_copy2);
2823  
-	return res;
  2812
+    if (!argc) {
  2813
+        return __Pyx_main(0, NULL);
  2814
+    }
  2815
+    else {
  2816
+        wchar_t **argv_copy = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
  2817
+        /* We need a second copies, as Python might modify the first one. */
  2818
+        wchar_t **argv_copy2 = (wchar_t **)malloc(sizeof(wchar_t*)*argc);
  2819
+        int i, res;
  2820
+        char *oldloc;
  2821
+        if (!argv_copy || !argv_copy2) {
  2822
+            fprintf(stderr, "out of memory\\n");
  2823
+            return 1;
  2824
+        }
  2825
+        oldloc = strdup(setlocale(LC_ALL, NULL));
  2826
+        setlocale(LC_ALL, "");
  2827
+        for (i = 0; i < argc; i++) {
  2828
+            argv_copy2[i] = argv_copy[i] = __Pyx_char2wchar(argv[i]);
  2829
+            if (!argv_copy[i])
  2830
+                return 1;
  2831
+        }
  2832
+        setlocale(LC_ALL, oldloc);
  2833
+        free(oldloc);
  2834
+        res = __Pyx_main(argc, argv_copy);
  2835
+        for (i = 0; i < argc; i++) {
  2836
+            free(argv_copy2[i]);
  2837
+        }
  2838
+        free(argv_copy);
  2839
+        free(argv_copy2);
  2840
+        return res;
  2841
+    }
2824 2842
 }
2825 2843
 #endif
2826 2844
 """)
6  Cython/Compiler/Options.py
@@ -44,9 +44,9 @@
44 44
 init_local_none = 1
45 45
 
46 46
 # Whether or not to embed the Python interpreter, for use in making a
47  
-# standalone executable. This will provide a main() method which simply
48  
-# executes the body of this module.
49  
-embed = False
  47
+# standalone executable or calling from external libraries.
  48
+# This will provide a method which simply executes the body of this module.
  49
+embed = None
50 50
 
51 51
 # Disables function redefinition, allowing all functions to be declared at
52 52
 # module creation time. For legacy code only. 

0 notes on commit 4350d7f

Please sign in to comment.
Something went wrong with that request. Please try again.