Fix for issues #841800 and #900506
diff --git a/Lib/plat-mac/bundlebuilder.py b/Lib/plat-mac/bundlebuilder.py
index f0c7fc2..8f59969 100755
--- a/Lib/plat-mac/bundlebuilder.py
+++ b/Lib/plat-mac/bundlebuilder.py
@@ -245,14 +245,14 @@
     del sys.path[1:]  # sys.path[0] is Contents/Resources/
 """
 
-if USE_ZIPIMPORT:
-    ZIP_ARCHIVE = "Modules.zip"
-    SITE_PY += "sys.path.append(sys.path[0] + '/%s')\n" % ZIP_ARCHIVE
-    def getPycData(fullname, code, ispkg):
-        if ispkg:
-            fullname += ".__init__"
-        path = fullname.replace(".", os.sep) + PYC_EXT
-        return path, MAGIC + '\0\0\0\0' + marshal.dumps(code)
+ZIP_ARCHIVE = "Modules.zip"
+SITE_PY_ZIP = SITE_PY + ("sys.path.append(sys.path[0] + '/%s')\n" % ZIP_ARCHIVE)
+
+def getPycData(fullname, code, ispkg):
+    if ispkg:
+        fullname += ".__init__"
+    path = fullname.replace(".", os.sep) + PYC_EXT
+    return path, MAGIC + '\0\0\0\0' + marshal.dumps(code)
 
 #
 # Extension modules can't be in the modules zip archive, so a placeholder
@@ -301,6 +301,9 @@
 libdir = os.path.join(os.path.dirname(execdir), "Frameworks")
 mainprogram = os.path.join(resdir, "%(mainprogram)s")
 
+if %(optimize)s:
+    sys.argv.insert(1, '-O')
+
 sys.argv.insert(1, mainprogram)
 if %(standalone)s or %(semi_standalone)s:
     os.environ["PYTHONPATH"] = resdir
@@ -311,6 +314,7 @@
     if pypath:
         pypath = ":" + pypath
     os.environ["PYTHONPATH"] = resdir + pypath
+
 os.environ["PYTHONEXECUTABLE"] = executable
 os.environ["DYLD_LIBRARY_PATH"] = libdir
 os.environ["DYLD_FRAMEWORK_PATH"] = libdir
@@ -350,6 +354,8 @@
 
 class AppBuilder(BundleBuilder):
 
+    use_zipimport = USE_ZIPIMPORT
+
     # Override type of the bundle.
     type = "APPL"
 
@@ -506,6 +512,7 @@
                 hashbang = os.path.realpath(sys.executable)
             standalone = self.standalone
             semi_standalone = self.semi_standalone
+            optimize = sys.flags.optimize
             open(bootstrappath, "w").write(BOOTSTRAP_SCRIPT % locals())
             os.chmod(bootstrappath, 0775)
 
@@ -552,13 +559,14 @@
             self.files.append((src, dst))
 
     def _getSiteCode(self):
-        return compile(SITE_PY % {"semi_standalone": self.semi_standalone},
+        if self.use_zipimport:
+            return compile(SITE_PY % {"semi_standalone": self.semi_standalone},
                      "<-bundlebuilder.py->", "exec")
 
     def addPythonModules(self):
         self.message("Adding Python modules", 1)
 
-        if USE_ZIPIMPORT:
+        if self.use_zipimport:
             # Create a zip file containing all modules as pyc.
             import zipfile
             relpath = pathjoin("Contents", "Resources", ZIP_ARCHIVE)
@@ -624,7 +632,7 @@
         self.message("Finding module dependencies", 1)
         import modulefinder
         mf = modulefinder.ModuleFinder(excludes=self.excludeModules)
-        if USE_ZIPIMPORT:
+        if self.use_zipimport:
             # zipimport imports zlib, must add it manually
             mf.import_hook("zlib")
         # manually add our own site.py
@@ -658,7 +666,7 @@
                 filename = os.path.basename(path)
                 pathitems = name.split(".")[:-1] + [filename]
                 dstpath = pathjoin(*pathitems)
-                if USE_ZIPIMPORT:
+                if self.use_zipimport:
                     if name != "zlib":
                         # neatly pack all extension modules in a subdirectory,
                         # except zlib, since it's neccesary for bootstrapping.
@@ -672,9 +680,9 @@
                 self.files.append((path, pathjoin("Contents", "Resources", dstpath)))
             if mod.__code__ is not None:
                 ispkg = mod.__path__ is not None
-                if not USE_ZIPIMPORT or name != "site":
+                if not self.use_zipimport or name != "site":
                     # Our site.py is doing the bootstrapping, so we must
-                    # include a real .pyc file if USE_ZIPIMPORT is True.
+                    # include a real .pyc file if self.use_zipimport is True.
                     self.pymodules.append((name, mod.__code__, ispkg))
 
         if hasattr(mf, "any_missing_maybe"):
@@ -820,6 +828,7 @@
       --semi-standalone  build a standalone application, which depends on
                          an installed Python, yet includes all third-party
                          modules.
+      --no-zipimport     Do not copy code into a zip file
       --python=FILE      Python to use in #! line in stead of current Python
       --lib=FILE         shared library or framework to be copied into
                          the bundle
@@ -847,7 +856,9 @@
         "mainprogram=", "creator=", "nib=", "plist=", "link",
         "link-exec", "help", "verbose", "quiet", "argv", "standalone",
         "exclude=", "include=", "package=", "strip", "iconfile=",
-        "lib=", "python=", "semi-standalone", "bundle-id=", "destroot=")
+        "lib=", "python=", "semi-standalone", "bundle-id=", "destroot="
+        "no-zipimport"
+        )
 
     try:
         options, args = getopt.getopt(sys.argv[1:], shortopts, longopts)
@@ -911,6 +922,8 @@
             builder.strip = 1
         elif opt == '--destroot':
             builder.destroot = arg
+        elif opt == '--no-zipimport':
+            builder.use_zipimport = False
 
     if len(args) != 1:
         usage("Must specify one command ('build', 'report' or 'help')")
diff --git a/Misc/NEWS b/Misc/NEWS
index 040ed5c..af502b6 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -238,6 +238,10 @@
 - Issue #1149804: macostools.mkdirs now even works when another process
   creates one of the needed subdirectories.
 
+- Issue #900506: added --no-zipimport flag to the bundlebuilder script
+
+- Issue #841800: bundlebuilder now works with 'python -O'
+
 Tools/Demos
 -----------