Skip to content

Commit e75f991

Browse files
author
Paul Davis
committed
Fix integer property lookups.
>>> rt = spidermonkey.Runtime() >>> cx = rt.new_context() >>> cx.add_global("bang", [2, "got me!"]) >>> assert cx.execute("bang[1];") == "got me!" Thanks to spahl for the report and fix. [#16 state:resolved]
1 parent ee6cfb8 commit e75f991

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

THANKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ sk89q
1414

1515
spahl
1616
* Heads up on the signal hack and fix for a compiler warning.
17+
* Bug #16 integer property lookup failure report and fix.
1718

1819
Mike West
1920
* Reported bug in Context.max_time

spidermonkey/pyobject.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ js_get_prop(JSContext* jscx, JSObject* jsobj, jsval key, jsval* val)
101101
pykey = js2py(pycx, key);
102102
if(pykey == NULL) goto done;
103103

104-
utf8 = PyUnicode_AsUTF8String(pykey);
105-
if(utf8 == NULL) goto done;
106-
107104
// Yeah. It's ugly as sin.
108-
if(PyString_Check(utf8))
105+
if(PyString_Check(pykey) || PyUnicode_Check(pykey))
109106
{
107+
utf8 = PyUnicode_AsUTF8String(pykey);
108+
if(utf8 == NULL) goto done;
109+
110110
data = PyString_AsString(utf8);
111111
if(data == NULL) goto done;
112112

tests/test-py-lookup.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2009 Paul J. Davis <paul.joseph.davis@gmail.com>
2+
#
3+
# This file is part of the python-spidermonkey package released
4+
# under the MIT license.
5+
import t
6+
7+
@t.cx()
8+
def test_str_property_as_item(cx):
9+
cx.add_global("zim", {"gir": "tacito!"})
10+
t.eq(cx.execute('zim["gir"]'), "tacito!")
11+
12+
@t.cx()
13+
def test_str_property_as_attr(cx):
14+
cx.add_global("protein", {"rna": "dna"})
15+
t.eq(cx.execute("protein.rna;"), "dna")
16+
17+
@t.cx()
18+
def test_unicode_key(cx):
19+
cx.add_global("unicode", {u"is": "complicated"})
20+
t.eq(cx.execute("unicode.is;"), "complicated")
21+
22+
@t.cx()
23+
def test_int_property(cx):
24+
cx.add_global("foo", [1, 8])
25+
t.eq(cx.execute("foo[1];"), 8)
26+
27+
28+
# JavaScript property looks can only be integers and
29+
# strings. So even though foo[1.1] looks like it should
30+
# work, Spidermonkey is converting it to a string which
31+
# affects access in python land.
32+
33+
@t.cx()
34+
def test_float_prop(cx):
35+
cx.add_global("foo", {1.1: "hidden!"})
36+
t.eq(cx.execute("foo[1.1];"), None)
37+
38+
@t.cx()
39+
def test_float_expected(cx):
40+
cx.add_global("whee", {"3.14": "mmmm food"})
41+
t.eq(cx.execute("whee[3.14];"), "mmmm food")

0 commit comments

Comments
 (0)