Skip to content

Commit

Permalink
Fix integer property lookups.
Browse files Browse the repository at this point in the history
    >>> 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]
  • Loading branch information
Paul Davis committed May 18, 2009
1 parent ee6cfb8 commit e75f991
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 0 deletions THANKS
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ sk89q

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

Mike West
* Reported bug in Context.max_time
Expand Down
8 changes: 4 additions & 4 deletions spidermonkey/pyobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ js_get_prop(JSContext* jscx, JSObject* jsobj, jsval key, jsval* val)
pykey = js2py(pycx, key);
if(pykey == NULL) goto done;

utf8 = PyUnicode_AsUTF8String(pykey);
if(utf8 == NULL) goto done;

// Yeah. It's ugly as sin.
if(PyString_Check(utf8))
if(PyString_Check(pykey) || PyUnicode_Check(pykey))
{
utf8 = PyUnicode_AsUTF8String(pykey);
if(utf8 == NULL) goto done;

data = PyString_AsString(utf8);
if(data == NULL) goto done;

Expand Down
41 changes: 41 additions & 0 deletions tests/test-py-lookup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2009 Paul J. Davis <paul.joseph.davis@gmail.com>
#
# This file is part of the python-spidermonkey package released
# under the MIT license.
import t

@t.cx()
def test_str_property_as_item(cx):
cx.add_global("zim", {"gir": "tacito!"})
t.eq(cx.execute('zim["gir"]'), "tacito!")

@t.cx()
def test_str_property_as_attr(cx):
cx.add_global("protein", {"rna": "dna"})
t.eq(cx.execute("protein.rna;"), "dna")

@t.cx()
def test_unicode_key(cx):
cx.add_global("unicode", {u"is": "complicated"})
t.eq(cx.execute("unicode.is;"), "complicated")

@t.cx()
def test_int_property(cx):
cx.add_global("foo", [1, 8])
t.eq(cx.execute("foo[1];"), 8)


# JavaScript property looks can only be integers and
# strings. So even though foo[1.1] looks like it should
# work, Spidermonkey is converting it to a string which
# affects access in python land.

@t.cx()
def test_float_prop(cx):
cx.add_global("foo", {1.1: "hidden!"})
t.eq(cx.execute("foo[1.1];"), None)

@t.cx()
def test_float_expected(cx):
cx.add_global("whee", {"3.14": "mmmm food"})
t.eq(cx.execute("whee[3.14];"), "mmmm food")

0 comments on commit e75f991

Please sign in to comment.