Skip to content

Commit b92b3c1

Browse files
committed
Updated the wxpython example on Windows, added the tests
for RequestHandler, DragHandler, DownloadHandler. Fixes to the cefadvanced example. Fixed error in ExceptHook in the examples.
1 parent e8482dd commit b92b3c1

19 files changed

Lines changed: 636 additions & 107 deletions

File tree

cefpython/cef1/linux/binaries_32bit/pygtk_.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,26 @@ def GetApplicationPath(file=None):
4141
return str(file)
4242

4343
def ExceptHook(excType, excValue, traceObject):
44-
import traceback, os, time
44+
import traceback, os, time, codecs
4545
# This hook does the following: in case of exception write it to
4646
# the "error.log" file, display it to the console, shutdown CEF
4747
# and exit application immediately by ignoring "finally" (_exit()).
4848
errorMsg = "\n".join(traceback.format_exception(excType, excValue,
4949
traceObject))
50-
logFile = GetApplicationPath("error.log")
50+
errorFile = GetApplicationPath("error.log")
5151
try:
5252
appEncoding = cefpython.g_applicationSettings["string_encoding"]
5353
except:
5454
appEncoding = "utf-8"
5555
if type(errorMsg) == bytes:
5656
errorMsg = errorMsg.decode(encoding=appEncoding, errors="replace")
5757
try:
58-
with open(logFile, "a", encoding=appEncoding) as fp:
58+
with codecs.open(errorFile, mode="a", encoding=appEncoding) as fp:
5959
fp.write("\n[%s] %s\n" % (
6060
time.strftime("%Y-%m-%d %H:%M:%S"), errorMsg))
6161
except:
6262
print("cefpython: WARNING: failed writing to error file: %s" % (
63-
error_file))
63+
errorFile))
6464
# Convert error message to ascii before printing, otherwise
6565
# you may get error like this:
6666
# | UnicodeEncodeError: 'charmap' codec can't encode characters

cefpython/cef1/linux/binaries_32bit/wxpython.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,26 @@ def GetApplicationPath(file=None):
5050
return str(file)
5151

5252
def ExceptHook(excType, excValue, traceObject):
53-
import traceback, os, time
53+
import traceback, os, time, codecs
5454
# This hook does the following: in case of exception write it to
5555
# the "error.log" file, display it to the console, shutdown CEF
5656
# and exit application immediately by ignoring "finally" (_exit()).
5757
errorMsg = "\n".join(traceback.format_exception(excType, excValue,
5858
traceObject))
59-
logFile = GetApplicationPath("error.log")
59+
errorFile = GetApplicationPath("error.log")
6060
try:
6161
appEncoding = cefpython.g_applicationSettings["string_encoding"]
6262
except:
6363
appEncoding = "utf-8"
6464
if type(errorMsg) == bytes:
6565
errorMsg = errorMsg.decode(encoding=appEncoding, errors="replace")
6666
try:
67-
with open(logFile, "a", encoding=appEncoding) as fp:
67+
with codecs.open(errorFile, mode="a", encoding=appEncoding) as fp:
6868
fp.write("\n[%s] %s\n" % (
6969
time.strftime("%Y-%m-%d %H:%M:%S"), errorMsg))
7070
except:
7171
print("cefpython: WARNING: failed writing to error file: %s" % (
72-
error_file))
72+
errorFile))
7373
# Convert error message to ascii before printing, otherwise
7474
# you may get error like this:
7575
# | UnicodeEncodeError: 'charmap' codec can't encode characters

cefpython/cef1/linux/binaries_64bit/pygtk_.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,26 @@ def GetApplicationPath(file=None):
4141
return str(file)
4242

4343
def ExceptHook(excType, excValue, traceObject):
44-
import traceback, os, time
44+
import traceback, os, time, codecs
4545
# This hook does the following: in case of exception write it to
4646
# the "error.log" file, display it to the console, shutdown CEF
4747
# and exit application immediately by ignoring "finally" (_exit()).
4848
errorMsg = "\n".join(traceback.format_exception(excType, excValue,
4949
traceObject))
50-
logFile = GetApplicationPath("error.log")
50+
errorFile = GetApplicationPath("error.log")
5151
try:
5252
appEncoding = cefpython.g_applicationSettings["string_encoding"]
5353
except:
5454
appEncoding = "utf-8"
5555
if type(errorMsg) == bytes:
5656
errorMsg = errorMsg.decode(encoding=appEncoding, errors="replace")
5757
try:
58-
with open(logFile, "a", encoding=appEncoding) as fp:
58+
with codecs.open(errorFile, mode="a", encoding=appEncoding) as fp:
5959
fp.write("\n[%s] %s\n" % (
6060
time.strftime("%Y-%m-%d %H:%M:%S"), errorMsg))
6161
except:
6262
print("cefpython: WARNING: failed writing to error file: %s" % (
63-
error_file))
63+
errorFile))
6464
# Convert error message to ascii before printing, otherwise
6565
# you may get error like this:
6666
# | UnicodeEncodeError: 'charmap' codec can't encode characters

cefpython/cef1/linux/binaries_64bit/wxpython.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,26 @@ def GetApplicationPath(file=None):
5050
return str(file)
5151

5252
def ExceptHook(excType, excValue, traceObject):
53-
import traceback, os, time
53+
import traceback, os, time, codecs
5454
# This hook does the following: in case of exception write it to
5555
# the "error.log" file, display it to the console, shutdown CEF
5656
# and exit application immediately by ignoring "finally" (_exit()).
5757
errorMsg = "\n".join(traceback.format_exception(excType, excValue,
5858
traceObject))
59-
logFile = GetApplicationPath("error.log")
59+
errorFile = GetApplicationPath("error.log")
6060
try:
6161
appEncoding = cefpython.g_applicationSettings["string_encoding"]
6262
except:
6363
appEncoding = "utf-8"
6464
if type(errorMsg) == bytes:
6565
errorMsg = errorMsg.decode(encoding=appEncoding, errors="replace")
6666
try:
67-
with open(logFile, "a", encoding=appEncoding) as fp:
67+
with codecs.open(errorFile, mode="a", encoding=appEncoding) as fp:
6868
fp.write("\n[%s] %s\n" % (
6969
time.strftime("%Y-%m-%d %H:%M:%S"), errorMsg))
7070
except:
7171
print("cefpython: WARNING: failed writing to error file: %s" % (
72-
error_file))
72+
errorFile))
7373
# Convert error message to ascii before printing, otherwise
7474
# you may get error like this:
7575
# | UnicodeEncodeError: 'charmap' codec can't encode characters
@@ -82,9 +82,7 @@ def ExceptHook(excType, excValue, traceObject):
8282

8383
class MainFrame(wx.Frame):
8484
browser = None
85-
initialized = False
8685
idleCount = 0
87-
box = None
8886

8987
def __init__(self):
9088
wx.Frame.__init__(self, parent=None, id=wx.ID_ANY,
@@ -246,11 +244,9 @@ def OnDrain(self, remainder):
246244
remainder.SetData("body h3 { color: orange; }")
247245

248246
class ClientHandler:
249-
250247
# --------------------------------------------------------------------------
251248
# RequestHandler
252249
# --------------------------------------------------------------------------
253-
254250
contentFilter = None
255251

256252
def OnBeforeBrowse(self, browser, frame, request, navType, isRedirect):

cefpython/cef1/windows/binaries/cefadvanced.html

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
Welcome to <b>CEF Python</b> - bindings for the Chromium Embedded Framework.
1515
<br><br>
1616

17-
Project's website: <a href="http://code.google.com/p/cefpython/">
17+
Project website: <a href="http://code.google.com/p/cefpython/">
1818
http://code.google.com/p/cefpython/</a><br>
1919

2020
Wiki documentation: <a href="http://code.google.com/p/cefpython/wiki/">
@@ -62,11 +62,11 @@ <h3>Browser object</h3>
6262
<a href="javascript:browser.Find(searchID=0, searchText='test', forward=true, matchCase=false, findNext=false)">Find(searchId=0, searchText='test', forward=true, matchCase=false, findNext=false)</a><br>
6363
<a href="javascript:alert(browser.GetUserData('some'))">GetUserData('some')</a><br>
6464
<a href="javascript:alert(browser.GetFocusedFrame())">GetFocusedFrame()</a>
65-
(returning a class object from python is not yet supported)<br>
66-
<a href="javascript:alert(browser.GetFrame('simple'))">CloseBrowser('simple')</a><br>
65+
- throws an error, returning a PyFrame object to to javascript is not yet supported<br>
66+
<a href="javascript:alert(browser.GetFrame('simple'))">GetFrame('simple')</a> - throws an error, returning a PyFrame object to javascript is not yet supported<br>
6767
<a href="javascript:alert(browser.GetFrameNames())">GetFrameNames()</a><br>
6868
<a href="javascript:alert(browser.GetMainFrame())">GetMainFrame()</a>
69-
(returning a class object from python is not yet supported)<br>
69+
- throws an error, returning a PyFrame object to javascript is not yet supported<br>
7070
<a href="javascript:alert(browser.GetOpenerWindowHandle())">
7171
GetOpenerWindowHandle()</a><br>
7272
<a href="javascript:alert(browser.GetOuterWindowHandle())">
@@ -85,13 +85,13 @@ <h3>Browser object</h3>
8585
<a href="javascript:browser.Reload()">Reload()</a><br>
8686
<a href="javascript:browser.ReloadIgnoreCache()">ReloadIgnoreCache()</a><br>
8787
<a href="javascript:browser.SetFocus(false)">SetFocus(false)</a>
88-
(inner window loses focus, check keyboard & mouse wheel scrolling)<br>
88+
- inner window loses focus, check keyboard and mouse wheel scrolling<br>
8989
<a href="javascript:browser.SetUserData('some', 'something')">
90-
SetUserData('some', 'something')</a><br>
90+
SetUserData('some', 'something')</a> - call GetUserData('some')<br>
9191
<a href="javascript:browser.SetZoomLevel(2)">SetZoomLevel(2)</a><br>
9292
<a href="javascript:browser.ShowDevTools()">ShowDevTools()</a><br>
9393
<a href="javascript:browser.StopLoad()">StopLoad()</a><br>
94-
<a href="javascript:browser.StopFinding(true)">StopFinding(true)</a><br>
94+
<a href="javascript:browser.StopFinding(true)">StopFinding(true)</a> - call Find(), then StopFinding()<br>
9595
<a href="javascript:browser.ToggleFullscreen()">ToggleFullscreen()</a><br>
9696

9797
<h3>Frame object</h3>
@@ -118,24 +118,21 @@ <h3>Frame object</h3>
118118
<a href="javascript:((frame.Redo()))">Redo()</a><br>
119119
<a href="javascript:((frame.SelectAll()))">SelectAll()</a><br>
120120
<a href="javascript:((frame.SetProperty('name', 'noname')))">
121-
SetProperty('name', 'noname')</a><br>
121+
SetProperty('name', 'noname')</a> - call GetProperty('name')<br>
122122
<a href="javascript:((frame.Undo()))">Undo()</a><br>
123123
<a href="javascript:((frame.ViewSource()))">ViewSource()</a><br>
124124

125125
<h3>Browser view as an image</h3>
126126

127-
Browser.GetImage() returns PaintBuffer, that object
128-
has method called GetString() that returns the buffer
129-
as a string that can be later passed to Python Imaging Library
130-
by calling PIL.Image.fromstring() and saved as a PNG (or
131-
other format) to a file. Test it:<br>
127+
Browser.GetImage() returns a PaintBuffer object that has the GetString()
128+
method that returns the buffer as a string, that string can be
129+
passed to PIL.Image.fromstring() and saved as a PNG or other
130+
format. This test requires the PIL or pygame library to be installed:<br>
132131
<a href="javascript:python.SaveImage('!cefadvanced.png', 'PNG')">
133132
python.SaveImage('!cefadvanced.png', 'PNG')</a><br>
134-
(you need to have installed the PIL extension).
135-
136-
<br><br>
137-
TODO: capture an image of all scrollable content on a webpage,
138-
detect the height of content in javascript and call
133+
134+
TODO: capture image of all the scrollable content on a webpage,
135+
detect the height of the content in javascript and call
139136
Browser.SetSize() using that height.
140137

141138
<h3>Test unicode string</h3>
@@ -151,7 +148,7 @@ <h3>Test creation of second browser</h3>
151148
<h3>Test int32, uint, long</h3>
152149

153150
<a href="javascript:alert(python.GetType(2147483647))">python.GetType(2147483647)</a><br>
154-
<a href="javascript:alert(python.GetType(2147483648))">python.GetType(2147483648)</a> - here we get UINT from CEF so it's still converted to long.<br>
151+
<a href="javascript:alert(python.GetType(2147483648))">python.GetType(2147483648)</a> - here we get UINT from CEF so it is converted to long in Py2, int in Py3.<br>
155152
<a href="javascript:alert(python.GetType(4294967295))">python.GetType(4294967295)</a> - uint limit.<br>
156153
<a href="javascript:alert(python.GetType(4294967296))">python.GetType(4294967296)</a> - outside of uint limit, converted to float.<br>
157154
<a href="javascript:alert(python.GetType(-2147483647))">python.GetType(-2147483647)</a><br>
@@ -164,7 +161,7 @@ <h3>Test int32, uint, long</h3>
164161

165162
<h3>Basic authentication</h3>
166163

167-
<input type=text value="http://127.0.0.1/cefpython/cefpython/cefpython1/var/http_authentication.php" size=60 id=httpauth>
164+
<input type=text value="http://127.0.0.1/cefpython/cefpython-src/cefpython/var/http_authentication.php" size=80 id=httpauth>
168165
<input type=button value="Go" onclick="location.href=httpauth.value">
169166

170167
<br>
@@ -181,12 +178,13 @@ <h3>Frames</h3>
181178

182179
<h3>Javascript bindings</h3>
183180

184-
See output in console for these tests. (Note: "window." is optional, you can just call "python.Test1()")<br>
181+
See output in the console for the javascript bindings tests.<br>
182+
Note: "window." is optional, you can just call "python.Test1()".<br>
185183

186184
<h4>Passing arguments</h4>
187185

188186
<a href="javascript:alert('Return value from python.Test1: '+python.Test1(100))">python.Test1(100)</a><br>
189-
<a href="javascript:alert('Return value from python.Test2: '+JSON.stringify(python.Test2(100, 'This string was passed from javascript [unicode: ąś]', '')))">python.Test2(100, 'This string was passed from javascript [unicode: ąś]', '')</a> - python.Test2() should return: [1,2, [2.1, {'3': 3, '4': [5,6]}]]
187+
<a href="javascript:alert('Return value from python.Test2: '+JSON.stringify(python.Test2(100, 'This string was passed from javascript [unicode: ąś]', '')))">python.Test2(100, 'This string was passed from javascript [unicode: ąś]', '')</a> - python.Test2() should return: [1,2, [2.1, {'3': 3, '4': [5,6]}], unicode...]
190188
<br><br>
191189

192190
<script>var array = [1]; array[100] = 100.01;</script>
@@ -236,13 +234,23 @@ <h3>Python callbacks</h3>
236234

237235
<h3>Errors</h3>
238236

239-
<p>Typically when you browse a webpage your javascript errors appear in javascript console in developer tools (in cefadvanced.py there is a binding to F12 key for developer tools).</p>
237+
<p>Typically when you browse a webpage javascript errors appear in
238+
the developer tools javascript console (in cefadvanced.py there
239+
is a binding to F12 key for the developer tools popup).</p>
240240

241241
<h4>Global exception handler</h4>
242242

243+
<script>
244+
function DoSomeError() {
245+
asd()
246+
}
247+
</script>
248+
243249
<p>You can catch javascript errors programmatically by
244-
using JavascriptContextHandler and the OnUncaughtException callback,
245-
test it:<br>
250+
using JavascriptContextHandler.OnUncaughtException() callback,
251+
to see the complete stack trace you need to set the
252+
"uncaught_exception_stack_size" ApplicationSettings option to
253+
a value greater than 0, test it:<br>
246254
<a href="javascript:DoSomeError()">
247255
DoSomeError()</a>
248256
</p>
@@ -263,7 +271,7 @@ <h4>Infinite recursion</h4>
263271

264272
<a href="javascript:python.Test1(window)">python.Test1(window)</a><br>
265273
You should see a python exception in the console (also in error.log),
266-
data structures can have maximum 8 levels of nesting.
274+
as data structures can have maximum 8 levels of nesting.
267275

268276
<h3>Moving and resizing window</h3>
269277

cefpython/cef1/windows/binaries/cefadvanced.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,26 @@ def GetApplicationPath(file=None):
6868
return str(file)
6969

7070
def ExceptHook(excType, excValue, traceObject):
71-
import traceback, os, time
71+
import traceback, os, time, codecs
7272
# This hook does the following: in case of exception write it to
7373
# the "error.log" file, display it to the console, shutdown CEF
7474
# and exit application immediately by ignoring "finally" (_exit()).
7575
errorMsg = "\n".join(traceback.format_exception(excType, excValue,
7676
traceObject))
77-
logFile = GetApplicationPath("error.log")
77+
errorFile = GetApplicationPath("error.log")
7878
try:
7979
appEncoding = cefpython.g_applicationSettings["string_encoding"]
8080
except:
8181
appEncoding = "utf-8"
8282
if type(errorMsg) == bytes:
8383
errorMsg = errorMsg.decode(encoding=appEncoding, errors="replace")
8484
try:
85-
with open(logFile, "a", encoding=appEncoding) as fp:
85+
with codecs.open(errorFile, mode="a", encoding=appEncoding) as fp:
8686
fp.write("\n[%s] %s\n" % (
8787
time.strftime("%Y-%m-%d %H:%M:%S"), errorMsg))
8888
except:
8989
print("cefpython: WARNING: failed writing to error file: %s" % (
90-
error_file))
90+
errorFile))
9191
# Convert error message to ascii before printing, otherwise
9292
# you may get error like this:
9393
# | UnicodeEncodeError: 'charmap' codec can't encode characters
@@ -414,7 +414,7 @@ def TestJavascriptCallback(self, jsCallback):
414414
if bytes == str:
415415
# Python 2.7
416416
jsCallback.Call(1, [2,3], ('tuple', 'tuple'),
417-
unicode('unicode string [ąś]'))
417+
unicode('unicode string [ąś]', encoding="utf-8"))
418418
else:
419419
# Python 3.2 - there is no "unicode()" in python 3
420420
jsCallback.Call(1, [2,3], ('tuple', 'tuple'),
@@ -449,7 +449,7 @@ def ChangeAlertDuringRuntime(self):
449449
def Alert2(self, msg):
450450
print("python.Alert2() called instead of window.alert()")
451451
win32gui.MessageBox(self.browser.GetUserData("outerWindowHandle"),
452-
msg, "python.Alert2()", win32con.MB_ICONWARNING)
452+
msg, "python.Alert2()", win32con.MB_ICONSTOP)
453453

454454
def Find(self, searchText, findNext=False):
455455
self.browser.Find(1, searchText, forward=True, matchCase=False,

cefpython/cef1/windows/binaries/cefsimple.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,26 @@ def GetApplicationPath(file=None):
4545
return str(file)
4646

4747
def ExceptHook(excType, excValue, traceObject):
48-
import traceback, os, time
48+
import traceback, os, time, codecs
4949
# This hook does the following: in case of exception write it to
5050
# the "error.log" file, display it to the console, shutdown CEF
5151
# and exit application immediately by ignoring "finally" (_exit()).
5252
errorMsg = "\n".join(traceback.format_exception(excType, excValue,
5353
traceObject))
54-
logFile = GetApplicationPath("error.log")
54+
errorFile = GetApplicationPath("error.log")
5555
try:
5656
appEncoding = cefpython.g_applicationSettings["string_encoding"]
5757
except:
5858
appEncoding = "utf-8"
5959
if type(errorMsg) == bytes:
6060
errorMsg = errorMsg.decode(encoding=appEncoding, errors="replace")
6161
try:
62-
with open(logFile, "a", encoding=appEncoding) as fp:
62+
with codecs.open(errorFile, mode="a", encoding=appEncoding) as fp:
6363
fp.write("\n[%s] %s\n" % (
6464
time.strftime("%Y-%m-%d %H:%M:%S"), errorMsg))
6565
except:
6666
print("cefpython: WARNING: failed writing to error file: %s" % (
67-
error_file))
67+
errorFile))
6868
# Convert error message to ascii before printing, otherwise
6969
# you may get error like this:
7070
# | UnicodeEncodeError: 'charmap' codec can't encode characters

0 commit comments

Comments
 (0)