Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
errorMsg = errorMsg.decode(encoding=appEncoding, errors="replace")
try:
with codecs.open(errorFile, mode="a", encoding=appEncoding) as fp:
fp.write("\n[%s] %s\n" % (
time.strftime("%Y-%m-%d %H:%M:%S"), errorMsg))
except:
print("[cefhello] WARNING: failed writing to error file: %s" % (
errorFile))
# Convert error message to ascii before printing, otherwise
# you may get error like this:
# | UnicodeEncodeError: 'charmap' codec can't encode characters
errorMsg = errorMsg.encode("ascii", errors="replace")
errorMsg = errorMsg.decode("ascii", errors="replace")
print("\n"+errorMsg+"\n")
cefpython.QuitMessageLoop()
cefpython.Shutdown()
os._exit(1)
def ExceptHook(excType, excValue, traceObject):
import traceback, os, time, codecs
# This hook does the following: in case of exception write it to
# the "error.log" file, display it to the console, shutdown CEF
# and exit application immediately by ignoring "finally" (os._exit()).
errorMsg = "\n".join(traceback.format_exception(excType, excValue,
traceObject))
errorFile = GetApplicationPath("error.log")
try:
appEncoding = cefpython.g_applicationSettings["string_encoding"]
except:
appEncoding = "utf-8"
if type(errorMsg) == bytes:
errorMsg = errorMsg.decode(encoding=appEncoding, errors="replace")
try:
with codecs.open(errorFile, mode="a", encoding=appEncoding) as fp:
fp.write("\n[%s] %s\n" % (
time.strftime("%Y-%m-%d %H:%M:%S"), errorMsg))
except:
print("[cefhello] WARNING: failed writing to error file: %s" % (
errorFile))
# Convert error message to ascii before printing, otherwise
# you may get error like this:
# | UnicodeEncodeError: 'charmap' codec can't encode characters
errorMsg = errorMsg.encode("ascii", errors="replace")
errorMsg = errorMsg.decode("ascii", errors="replace")
g_browserSettings = {
# "plugins_disabled": True,
# "file_access_from_file_urls_allowed": True,
# "universal_access_from_file_urls_allowed": True,
}
# Command line switches set programmatically
g_commandLineSwitches = {
# "proxy-server": "socks5://127.0.0.1:8888",
# "no-proxy-server": "",
# "enable-media-stream": "",
# "disable-gpu": "",
}
cefpython.Initialize(g_applicationSettings, g_commandLineSwitches)
app = MyApp(False)
app.MainLoop()
# Let wx.App destructor do the cleanup before calling
# cefpython.Shutdown(). This is to ensure reliable CEF shutdown.
del app
cefpython.Shutdown()
self.test_case.assertFalse(browser.IsPopup())
else:
# Second call for implicit popup browser opened via js.
# Should execute only for main test.
# Should not execute for DevTools window.
assert "main_test" in self.test_case.id()
assert not self.OnAfterCreatedPopup_True
self.OnAfterCreatedPopup_True = True
self.test_case.assertEqual(browser.GetIdentifier(),
POPUP_BROWSER_ID)
# browser.GetUrl() returns empty at this moment.
self.test_case.assertTrue(browser.IsPopup())
if WINDOWS:
cef.WindowUtils.SetTitle(browser, "Popup test")
# Close the popup browser after 250 ms
cef.PostDelayedTask(cef.TID_UI, 250, close_popup, self, browser)
def run_message_loop():
# Run message loop for some time.
# noinspection PyTypeChecker
for i in range(MESSAGE_LOOP_RANGE):
cef.MessageLoopWork()
time.sleep(0.01)
subtest_message("cef.MessageLoopWork() ok")
def do_message_loop_work(work_loops):
# noinspection PyTypeChecker
for i in range(work_loops):
cef.MessageLoopWork()
time.sleep(0.01)
def OnSize(self, event):
cefpython.WindowUtils.OnSize(self.GetHandleForBrowser(), 0, 0, 0)
self.clientHandler._OnCertificateError)
cefpython.SetGlobalClientCallback("OnBeforePluginLoad",
self.clientHandler._OnBeforePluginLoad)
cefpython.SetGlobalClientCallback("OnAfterCreated",
self.clientHandler._OnAfterCreated)
windowInfo = cefpython.WindowInfo()
windowInfo.SetAsChild(self.GetHandleForBrowser())
self.browser = cefpython.CreateBrowserSync(windowInfo,
browserSettings=g_browserSettings,
navigateUrl=url)
self.clientHandler.mainBrowser = self.browser
self.browser.SetClientHandler(self.clientHandler)
jsBindings = cefpython.JavascriptBindings(
bindToFrames=False, bindToPopups=True)
jsBindings.SetFunction("PyPrint", PyPrint)
jsBindings.SetProperty("pyProperty", "This was set in Python")
jsBindings.SetProperty("pyConfig", ["This was set in Python",
{"name": "Nested dictionary", "isNested": True},
[1,"2", None]])
self.javascriptExternal = JavascriptExternal(self.browser)
jsBindings.SetObject("external", self.javascriptExternal)
# jsBindings.SetProperty("sources", GetSources())
self.browser.SetJavascriptBindings(jsBindings)
if self.mainPanel:
self.mainPanel.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
self.mainPanel.Bind(wx.EVT_SIZE, self.OnSize)
else:
self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
if TEST_EMBEDDING_IN_PANEL:
print("Embedding in a wx.Panel!")
# You also have to set the wx.WANTS_CHARS style for
# all parent panels/controls, if it's deeply embedded.
self.mainPanel = wx.Panel(self, style=wx.WANTS_CHARS)
# Global client callbacks must be set before browser is created.
self.clientHandler = ClientHandler()
cefpython.SetGlobalClientCallback("OnCertificateError",
self.clientHandler._OnCertificateError)
cefpython.SetGlobalClientCallback("OnBeforePluginLoad",
self.clientHandler._OnBeforePluginLoad)
cefpython.SetGlobalClientCallback("OnAfterCreated",
self.clientHandler._OnAfterCreated)
windowInfo = cefpython.WindowInfo()
windowInfo.SetAsChild(self.GetHandleForBrowser())
self.browser = cefpython.CreateBrowserSync(windowInfo,
browserSettings=g_browserSettings,
navigateUrl=url)
self.clientHandler.mainBrowser = self.browser
self.browser.SetClientHandler(self.clientHandler)
jsBindings = cefpython.JavascriptBindings(
bindToFrames=False, bindToPopups=True)
jsBindings.SetFunction("PyPrint", PyPrint)
jsBindings.SetProperty("pyProperty", "This was set in Python")
jsBindings.SetProperty("pyConfig", ["This was set in Python",
g_applicationSettings = {
# Disk cache
# "cache_path": "webcache/",
# CEF Python debug messages in console and in log_file
"debug": True,
# Set it to LOGSEVERITY_VERBOSE for more details
"log_severity": cefpython.LOGSEVERITY_INFO,
# Set to "" to disable logging to a file
"log_file": GetApplicationPath("debug.log"),
# This should be enabled only when debugging
"release_dcheck_enabled": True,
# These directories must be set on Linux
"locales_dir_path": cefpython.GetModuleDirectory()+"/locales",
"resources_dir_path": cefpython.GetModuleDirectory(),
# The "subprocess" executable that launches the Renderer
# and GPU processes among others. You may rename that
# executable if you like.
"browser_subprocess_path": "%s/%s" % (
cefpython.GetModuleDirectory(), "subprocess"),
}
# You can comment out the code below if you do not want High
# DPI support. If you disable it text will look fuzzy on
# high DPI displays.
#
# Enabling High DPI support in app can be done by
# embedding a DPI awareness xml manifest in executable
# (see Issue 112 comment #2), or by calling SetProcessDpiAware
# function. Embedding xml manifest is the most reliable method.