--- /opt/local/var/macports/sources/git.macports.org/dports/python/py-pyqt5/Portfile	2016-12-14 22:34:57.000000000 +0100
+++ python/py-pyqt5/Portfile	2016-12-15 18:41:47.000000000 +0100
@@ -1,80 +1,127 @@
 # -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
 # $Id$
 
-PortSystem          1.0
-PortGroup           qt5 1.0
-PortGroup           python 1.0
-
-name                py-pyqt5
-version             5.6
-revision            2
-categories-append   devel
-platforms           darwin
-maintainers         mmoll openmaintainer
-description         PyQt5 is a set of Python bindings for the Qt5 toolkit
-long_description \
-    PyQt5 is a set of Python bindings for the Qt5 toolkit. The bindings \
-    are implemented as a set of Python modules and contain over 620 classes.
-homepage            http://www.riverbankcomputing.co.uk/software/pyqt/intro
-license             GPL-3
-master_sites        sourceforge:pyqt
-distname            PyQt5_gpl-${version}
-checksums           rmd160  498905b678fc2641c8ff6521b5c2126ac6ddf347 \
-                    sha256  2e481a6c4c41b96ed3b33449e5f9599987c63a5c8db93313bd57a6acbf20f0e1
+PortSystem              1.0
+PortGroup               qt5 1.0
+set depends_lib_backup  ${depends_lib}
+PortGroup               python 1.0
+
+name                    py-pyqt5
+version                 5.6
+revision                3
+categories-append       devel
+platforms               darwin
+maintainers             mmoll openmaintainer
+description             PyQt5 is a set of Python bindings for the Qt5 toolkit
+long_description        ${description}. The bindings \
+                        are implemented as a set of Python modules and contain over 620 classes.
+homepage                http://www.riverbankcomputing.co.uk/software/pyqt/intro
+license                 GPL-3
+master_sites            sourceforge:pyqt
+distname                PyQt5_gpl-${version}
+checksums               rmd160  498905b678fc2641c8ff6521b5c2126ac6ddf347 \
+                        sha256  2e481a6c4c41b96ed3b33449e5f9599987c63a5c8db93313bd57a6acbf20f0e1
 
 python.versions 27 34 35
 
+# at this point, the python portgroup will have reset depends_lib (python.versions does that),
+depends_lib-append      ${depends_lib_backup}
+
+if {[info procs qt5.depends_component] eq ""} {
+    proc qt5.depends_component {first args} {
+        global qt5_component_lib qt5.using_kde os.major
+        # join ${first} and (the optional) ${args}
+        set args [linsert $args[set list {}] 0 ${first}]
+        if {[info exists qt5.using_kde] && ${qt5.using_kde}} {
+            set qt5main "qt5-kde"
+        } if {${os.major} == 11} {
+            set qt5main "qt55"
+        } else {
+            set qt5main "qt5"
+        }
+        foreach comp ${args} {
+            if {${comp} eq "qt5"} {
+                if {[info exists qt5.using_kde] && ${qt5.using_kde}} {
+                    global qt5_dependency
+                    depends_lib-append ${qt5_dependency}
+                } else {
+                    depends_lib-append port:${qt5main}
+                }
+            } else {
+                set portname "${qt5main}-${comp}"
+                depends_lib-append port:${portname}
+            }
+        }
+    }
+}
+
 if {${name} ne ${subport}} {
-    depends_lib-append port:py${python.version}-sip \
-        port:qt5                                    \
-        port:dbus-python${python.version}
-    use_configure      yes
-    configure.pre_args
-    configure.cmd      "${python.bin} configure.py"
-    configure.args-append  -q ${qt_qmake_cmd} --verbose --confirm-license \
-        --sip=${prefix}/bin/sip-${python.branch}
+    depends_lib-append  port:py${python.version}-sip \
+                        port:dbus-python${python.version}
 
-    # modules that are not installed with qt5 port
+    use_configure       yes
+    configure.pre_args
+    configure.cmd       "${python.bin} configure.py"
     configure.args-append \
-        --disable=QtWebKit \
-        --disable=QtWebKitWidgets \
-        --disable=QtWebEngineCore \
-        --disable=QtWebEngineWidgets
-
-    build.cmd          make
-    build.target       all
-    destroot.cmd       ${build.cmd}
-    destroot.destdir   DESTDIR=${destroot}
+                        -q ${qt_qmake_cmd} \
+                        --verbose \
+                        --confirm-license \
+                        --sip=${prefix}/bin/sip-${python.branch} \
+                        --dbus=${python.include}/dbus-1.0 \
+                        --designer-plugindir=${qt_plugins_dir}/designer/Py${python.version}Qt5 \
+                        --qml-plugindir=${qt_plugins_dir}/Py${python.version}Qt5 \
+                        --disable=QtWebKit \
+                        --disable=QtWebKitWidgets \
+                        --disable=QtWebEngineCore \
+                        --disable=QtWebEngineWidgets
+
+    post-configure {
+        # using --dbus means the compiler will find dbus-python.h but not
+        # the DBus headers themselves. Correct dbus.pro :
+        set dbus1incpath [strsed [exec pkg-config --cflags-only-I dbus-1] "g/-I//"]
+        reinplace "s|INCLUDEPATH += .$|INCLUDEPATH += . ${dbus1incpath}|g" \
+                        ${worksrcpath}/dbus/dbus.pro
+        system -W ${worksrcpath}/dbus "${qt_qmake_cmd} dbus.pro"
+    }
+
+    build.cmd           make
+    build.target        all
+    destroot.cmd        ${build.cmd}
+    destroot.destdir    DESTDIR=${destroot}
 
     # override QMAKE_MACOSX_DEPLOYMENT_TARGET set in ${prefix}/libexec/qt5/mkspecs/macx-clang/qmake.conf
-    configure.args-append QMAKE_MACOSX_DEPLOYMENT_TARGET=${macosx_deployment_target}
+    configure.args-append \
+                        QMAKE_MACOSX_DEPLOYMENT_TARGET=${macosx_deployment_target}
 
     # override C++ flags set in ${prefix}/libexec/qt5/mkspecs/common/clang-mac.conf
     #    so value of ${configure.cxx_stdlib} can always be used
     configure.args-append \
-        QMAKE_CXXFLAGS-=-stdlib=libc++ \
-        QMAKE_LFLAGS-=-stdlib=libc++ \
-        QMAKE_CXXFLAGS+=-stdlib=${configure.cxx_stdlib} \
-        QMAKE_LFLAGS+=-stdlib=${configure.cxx_stdlib}
+                        QMAKE_CXXFLAGS-=-stdlib=libc++ \
+                        QMAKE_LFLAGS-=-stdlib=libc++ \
+                        QMAKE_CXXFLAGS+=-stdlib=${configure.cxx_stdlib} \
+                        QMAKE_LFLAGS+=-stdlib=${configure.cxx_stdlib}
 
     variant debug description "Build debug libraries" {
         configure.cmd-append --debug
     }
 
     variant webengine conflicts universal description {Build QtWebEngine module} {
-        depends_lib-append port:qt5-qtwebengine
-
-        configure.args-delete --disable=QtWebEngineCore
-        configure.args-delete --disable=QtWebEngineWidgets
+        qt5.depends_component qtwebengine
+        configure.args-delete --disable=QtWebEngineCore \
+                              --disable=QtWebEngineWidgets
     }
 
-    default_variants-append +webengine
+# probably not justified yet, given QtWebEngine's size
+#     default_variants-append +webengine
 
     variant webkit description {Build QtWebKit module} {
-        depends_lib-append port:qt5-qtwebkit
-
+        qt5.depends_component qtwebkit
         configure.args-delete --disable=QtWebKit
-        configure.args-delete --disable=QtWebKitWidgets
+                              --disable=QtWebKitWidgets
+    }
+
+    variant graceful description {Don't abort (crash) on Python errors} {
+        patchfiles-append  patch-no-abort-on-python-errors.diff
     }
 
     if { ![variant_isset universal] } {
@@ -85,9 +132,8 @@
         set merger_configure_args(x86_64) "--spec=${qt_qmake_spec_64}"
     }
 
-    livecheck.type     none
-} else {
-    livecheck.type     regex
-    livecheck.url      https://www.riverbankcomputing.co.uk/software/pyqt/download5
-    livecheck.regex    >PyQt5_gpl-(\[0-9.\]*).tar.gz<
 }
+
+livecheck.type     regex
+livecheck.url      http://www.riverbankcomputing.co.uk/software/pyqt/download5
+livecheck.regex    >PyQt5_gpl-(\[0-9.\]*).tar.gz<
