pygobject / pgi

[Unmaintained: Use PyGObject instead] GTK+ / GObject Introspection Bindings for PyPy.
GNU Lesser General Public License v2.1
74 stars 16 forks source link

Handling pgi.clib.glib.GSListPtr #43

Open jim-easterbrook opened 6 years ago

jim-easterbrook commented 6 years ago

I've been experimenting with using the Gspell spell checking library from Python. The only difficulty I've encountered is with the value returned by Gspell.Checker.get_suggestions. When using PyGObject I get a Python list of strings, as expected. With pgi I get an object of type pgi.clib.glib.GSListPtr.

I've found a workaround as follows:

suggestions = checker.get_suggestions(word, -1)
result = []
for i in range(suggestions.length):
    c_str = ctypes.c_char_p(suggestions.nth_data(i))
    result.append(c_str.value.decode('utf_8'))
return result
stuaxo commented 5 years ago

Can you check if this is an issue for anything that would return a list of strings ?

(ie go, find some other APIs that would normally return lists of strings and see if they return GSListPtr instead) ?

Maybe some code like yours could be added to the right place in PGI if so.

stuaxo commented 5 years ago

@jim-easterbrook can you post a more complete example? I installed the GSpell gir bindings, but I'm not sure how to create the checker instance you have.

jim-easterbrook commented 5 years ago

Here's a short script.

try:
    import pgi
    pgi.install_as_gi()
except ImportError:
    pass
import gi
gi.require_version('Gspell', '1')
from gi.repository import Gspell

check = Gspell.Checker.new(Gspell.Language.lookup('en'))
suggestions = check.get_suggestions('foo', -1)
print(type(suggestions))
print(suggestions)

Without pgi installed I get the following:

(process:11865): gspell-CRITICAL **: gspell_checker_add_word_to_session: assertion 'word != NULL' failed
<class 'list'>
['foo', 'goo', 'fop', 'foot', 'fool', 'food', 'foe', 'for', 'fro', 'too', 'fol', 'loo', 'coo', 'fog', 'moo']

With pgi I get this:

<class 'pgi.clib.glib.GSListPtr'>
<pgi.clib.glib.GSListPtr object at 0x7f554067a9d8>

I'm not familiar with many GObject APIs, I've only used Gspell and GExiv2 so far. I'm not sure how easy it'll be to find another that returns a list of strings.

stuaxo commented 5 years ago

That one is fine, I spent a bit of time trying to understand the pgi code generation the other day but am not really there.

Is plain gobject not an option under the environment you are running under?