I'm played with GeanyPy plugin and faced with a problem.
Here is the source code I created to get current document from sidebar:
def press(self, widget, event, data=None): if event.button != 2: return mod, it = widget.get_selection().get_selected() doc = mod.get(it, 2)[0]
According to sidebar.c, mod.get(it, 2) should return pointer to Document.
But Python doesn't know that this is document and doc variable in this example has type GPointer. How can I access to this document from python plugin?
P.S. I tried to use ctypes.cast, no success. Currently I'm addressing document with a file path but it is a hack and isn't working for symlinks.
-- Best regards, Pavel Roschin aka RPG
On 14-04-24 09:44 PM, Pavel Roschin wrote:
I'm played with GeanyPy plugin and faced with a problem.
Here is the source code I created to get current document from sidebar:
def press(self, widget, event, data=None): if event.button != 2: return mod, it = widget.get_selection().get_selected() doc = mod.get(it, 2)[0]
According to sidebar.c, mod.get(it, 2) should return pointer to Document.
This isn't supported/recommended to do in either Geany plugin API or GeanyPy, but see below.
But Python doesn't know that this is document and doc variable in this example has type GPointer. How can I access to this document from python plugin?
P.S. I tried to use ctypes.cast, no success. Currently I'm addressing document with a file path but it is a hack and isn't working for symlinks.
If you really want to, maybe try to call the C function `Document_create_new_from_geany_document()` (in src/geanypy-document.[ch]) which is used by GeanyPy internally, but should be callable somehow from Python.
Cheers, Matthew Brush
On 14-04-24 11:35 PM, Matthew Brush wrote:
On 14-04-24 09:44 PM, Pavel Roschin wrote:
I'm played with GeanyPy plugin and faced with a problem.
Here is the source code I created to get current document from sidebar:
def press(self, widget, event, data=None): if event.button != 2: return mod, it = widget.get_selection().get_selected() doc = mod.get(it, 2)[0]
According to sidebar.c, mod.get(it, 2) should return pointer to Document.
This isn't supported/recommended to do in either Geany plugin API or GeanyPy, but see below.
But Python doesn't know that this is document and doc variable in this example has type GPointer. How can I access to this document from python plugin?
P.S. I tried to use ctypes.cast, no success. Currently I'm addressing document with a file path but it is a hack and isn't working for symlinks.
If you really want to, maybe try to call the C function `Document_create_new_from_geany_document()` (in src/geanypy-document.[ch]) which is used by GeanyPy internally, but should be callable somehow from Python.
... Unless all you want to do is get the current/active document, in which case just call `geany.document.get_current()` from Python.
Cheers, Matthew Brush
On 14-04-24 09:44 PM, Pavel Roschin wrote:
I'm played with GeanyPy plugin and faced with a problem.
Here is the source code I created to get current document from sidebar:
def press(self, widget, event, data=None): if event.button != 2: return mod, it = widget.get_selection().get_selected() doc = mod.get(it, 2)[0]
According to sidebar.c, mod.get(it, 2) should return pointer to Document.
This isn't supported/recommended to do in either Geany plugin API or GeanyPy, but see below.
But how can I do this in native way? I just want to implement simplest feature I proposed in PR #172 which seems will take forever to accept.
If you really want to, maybe try to call the C function `Document_create_new_from_geany_document()` (in src/geanypy-document.[ch]) which is used by GeanyPy internally, but should be callable somehow from Python.
How can I call this function? I could try to use ctypes to load library but I didn't find native way to get full path to geanypy library.
Currently I close file by path but it doesn't work for symlinks:
https://github.com/scriptum/geany-plugins-python/blob/master/mmc.py
-- Best regards, Pavel Roschin aka RPG
On 14-04-25 10:18 AM, Pavel Roschin wrote:
On 14-04-24 09:44 PM, Pavel Roschin wrote:
I'm played with GeanyPy plugin and faced with a problem.
Here is the source code I created to get current document from sidebar:
def press(self, widget, event, data=None): if event.button != 2: return mod, it = widget.get_selection().get_selected() doc = mod.get(it, 2)[0]
According to sidebar.c, mod.get(it, 2) should return pointer to Document.
This isn't supported/recommended to do in either Geany plugin API or GeanyPy, but see below.
But how can I do this in native way? I just want to implement simplest feature I proposed in PR #172 which seems will take forever to accept.
I just meant it's not recommended for plugins to rely on the tree view implementation stuff like which tree model columns have various things packed in them and such just because it's undocumented and could change anytime. Anyway, you are of course free to do anything you want, and if that's the only way, what else can you do? :)
If you really want to, maybe try to call the C function `Document_create_new_from_geany_document()` (in src/geanypy-document.[ch]) which is used by GeanyPy internally, but should be callable somehow from Python.
How can I call this function? I could try to use ctypes to load library but I didn't find native way to get full path to geanypy library.
It should be in the "current" library (not sure correct term), like if it was C, you would call `dlopen(NULL, ...)`. I'm not familiar enough with ctypes to say exactly but if it had a function like `load_library()` I'd expect you would pass `None` where it expects a filename. Alternatively, you might try `geany.document.__file__` to pickup the C module's filename where the symbol lives, if that works.
Cheers, Matthew Brush
It should be in the "current" library (not sure correct term), like if it was C, you would call `dlopen(NULL, ...)`. I'm not familiar enough with ctypes to say exactly but if it had a function like `load_library()` I'd expect you would pass `None` where it expects a filename. Alternatively, you might try `geany.document.__file__` to pickup the C module's filename where the symbol lives, if that works.
Indeed a *very* quick (ie I didn't try anything useful, just "load" the library) try in the REPL shows cdll.LoadLibrary(None) seems to find something presumed to be the main program.
Cheers Lex
Cheers, Matthew Brush
Devel mailing list Devel@lists.geany.org https://lists.geany.org/cgi-bin/mailman/listinfo/devel
It should be in the "current" library (not sure correct term), like if it was C, you would call `dlopen(NULL, ...)`. I'm not familiar enough with ctypes to say exactly but if it had a function like `load_library()` I'd expect you would pass `None` where it expects a filename. Alternatively, you might try `geany.document.__file__` to pickup the C module's filename where the symbol lives, if that works.
It works great, now I can access to all internal geany binary functions. But I can't access to geanypy.so functions :(
e.g.
lib.utils_remove_ext_from_filename
<_FuncPtr object at 0x27dc120>
lib.Document_create_new_from_geany_document
Traceback (most recent call last): File "<input>", line 1, in <module> File "/usr/lib64/python2.7/ctypes/__init__.py", line 378, in __getattr__ func = self.__getitem__(name) File "/usr/lib64/python2.7/ctypes/__init__.py", line 383, in __getitem__ func = self._FuncPtr((name_or_ordinal, self)) AttributeError: ./src/geany: undefined symbol: Document_create_new_from_geany_document
I tried another way: using document_close from geany binary:
def doc_close(self, doc): ptrValue = int(str(doc)[13:-1], 16) lib.document_close(ctypes.cast(ptrValue, ctypes.c_void_p).value)
Now I have segfault :)
-- Best regards, Pavel Roschin aka RPG
On 26 April 2014 17:50, Pavel Roschin roshin@scriptumplus.ru wrote:
It should be in the "current" library (not sure correct term), like if it was C, you would call `dlopen(NULL, ...)`. I'm not familiar enough with ctypes to say exactly but if it had a function like `load_library()` I'd expect you would pass `None` where it expects a filename. Alternatively, you might try `geany.document.__file__` to pickup the C module's filename where the symbol lives, if that works.
It works great, now I can access to all internal geany binary functions. But I can't access to geanypy.so functions :(
e.g.
lib.utils_remove_ext_from_filename
<_FuncPtr object at 0x27dc120>
lib.Document_create_new_from_geany_document
Traceback (most recent call last): File "<input>", line 1, in <module> File "/usr/lib64/python2.7/ctypes/__init__.py", line 378, in __getattr__ func = self.__getitem__(name) File "/usr/lib64/python2.7/ctypes/__init__.py", line 383, in __getitem__ func = self._FuncPtr((name_or_ordinal, self)) AttributeError: ./src/geany: undefined symbol: Document_create_new_from_geany_document
I tried another way: using document_close from geany binary:
def doc_close(self, doc): ptrValue = int(str(doc)[13:-1], 16)
***VERY*** evil >:-D
Is int the same size as pointer on your system? On windows in particular int can be 32 bit and pointer 64 bit.
Cheers Lex
lib.document_close(ctypes.cast(ptrValue, ctypes.c_void_p).value)
Now I have segfault :)
-- Best regards, Pavel Roschin aka RPG _______________________________________________ Devel mailing list Devel@lists.geany.org https://lists.geany.org/cgi-bin/mailman/listinfo/devel
***VERY*** evil >:-D
It is :-D
Is int the same size as pointer on your system? On windows in particular int can be 32 bit and pointer 64 bit.
With both ways (int, long) I can't achieve result. Seems that this is not actual doc pointer or something. Without native binding can't imagine how to do that.
-- Best regards, Pavel Roschin aka RPG
On 26 April 2014 21:45, Pavel Roschin roshin@scriptumplus.ru wrote:
***VERY*** evil >:-D
It is :-D
Is int the same size as pointer on your system? On windows in particular int can be 32 bit and pointer 64 bit.
With both ways (int, long) I can't achieve result. Seems that this is not actual doc pointer or something. Without native binding can't imagine how to do that.
A python int is the underlying C long see https://docs.python.org/2/library/stdtypes.html#numeric-types-int-float-long..., but unfortunately on windows 64 thats still 32 bits, see http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751%28v=vs.85%2....
We have had problems in Geany in the past where people tried to pack pointers into a long, worked on Linux, failed on Win64.
(I'm thinking I remember you are using Windows, if I'm wrong please disregard)
Unfortunately I can't actually think of how you could do it in Geanypy, I think you would be better off doing it in C.
Cheers Lex
PS but at least you have restarted the discussion on the PR ;)
-- Best regards, Pavel Roschin aka RPG _______________________________________________ Devel mailing list Devel@lists.geany.org https://lists.geany.org/cgi-bin/mailman/listinfo/devel
On 14-04-26 12:50 AM, Pavel Roschin wrote:
It should be in the "current" library (not sure correct term), like if it was C, you would call `dlopen(NULL, ...)`. I'm not familiar enough with ctypes to say exactly but if it had a function like `load_library()` I'd expect you would pass `None` where it expects a filename. Alternatively, you might try `geany.document.__file__` to pickup the C module's filename where the symbol lives, if that works.
It works great, now I can access to all internal geany binary functions. But I can't access to geanypy.so functions :(
e.g.
lib.utils_remove_ext_from_filename
<_FuncPtr object at 0x27dc120>
lib.Document_create_new_from_geany_document
Traceback (most recent call last): File "<input>", line 1, in <module> File "/usr/lib64/python2.7/ctypes/__init__.py", line 378, in __getattr__ func = self.__getitem__(name) File "/usr/lib64/python2.7/ctypes/__init__.py", line 383, in __getitem__ func = self._FuncPtr((name_or_ordinal, self)) AttributeError: ./src/geany: undefined symbol: Document_create_new_from_geany_document
I tried another way: using document_close from geany binary:
def doc_close(self, doc): ptrValue = int(str(doc)[13:-1], 16) lib.document_close(ctypes.cast(ptrValue, ctypes.c_void_p).value)
Now I have segfault :)
Is it possible that `GPointer` is a `PyCapsule` (or `PyCObject`) and not an actual `void*` pointing directly to a `GeanyDocument*`?
Cheers, Matthew Brush
Is it possible that `GPointer` is a `PyCapsule` (or `PyCObject`) and not an actual `void*` pointing directly to a `GeanyDocument*`?
I'm not advanced pyhton user so I assume that it is (e.g. pygkt creates this "wrap"). But I took an example from here:
http://olivergerlich.wordpress.com/2009/06/28/pythongtk-getting-the-gerror-m...
and it isn't working.
-- Best regards, Pavel Roschin aka RPG
On 14-04-26 12:50 AM, Pavel Roschin wrote:
It should be in the "current" library (not sure correct term), like if it was C, you would call `dlopen(NULL, ...)`. I'm not familiar enough with ctypes to say exactly but if it had a function like `load_library()` I'd expect you would pass `None` where it expects a filename. Alternatively, you might try `geany.document.__file__` to pickup the C module's filename where the symbol lives, if that works.
It works great, now I can access to all internal geany binary functions. But I can't access to geanypy.so functions :(
Another (untested) hack that might work:
def geanypy_path(): import geany, os path = os.path.dirname( os.path.dirname(os.path.dirname(geany.__file__))) return os.path.join(path, "geanypy.so")
Cheers, Matthew Brush
Another (untested) hack that might work:
def geanypy_path(): import geany, os path = os.path.dirname( os.path.dirname(os.path.dirname(geany.__file__))) return os.path.join(path, "geanypy.so")
Cheers, Matthew Brush
It works good too, but as I mentioned above, casting doc to correct pointer isn't simple in Python (and I have no ideas how to).
-- Best regards, Pavel Roschin aka RPG
On 27 April 2014 03:43, Pavel Roschin roshin@scriptumplus.ru wrote:
Another (untested) hack that might work:
def geanypy_path(): import geany, os path = os.path.dirname( os.path.dirname(os.path.dirname(geany.__file__))) return os.path.join(path, "geanypy.so")
Cheers, Matthew Brush
It works good too, but as I mentioned above, casting doc to correct pointer isn't simple in Python (and I have no ideas how to).
You have to define a structure type that matches what the pointer points to https://docs.python.org/2/library/ctypes.html#structures-and-unions and then cast the doc pointer to that https://docs.python.org/2/library/ctypes.html#type-conversions and then you should be able to access the fields.
Cheers Lex
-- Best regards, Pavel Roschin aka RPG _______________________________________________ Devel mailing list Devel@lists.geany.org https://lists.geany.org/cgi-bin/mailman/listinfo/devel
Le 26/04/2014 22:48, Lex Trotman a écrit :
On 27 April 2014 03:43, Pavel Roschin roshin@scriptumplus.ru wrote:
Another (untested) hack that might work:
def geanypy_path(): import geany, os path = os.path.dirname( os.path.dirname(os.path.dirname(geany.__file__))) return os.path.join(path, "geanypy.so")
Cheers, Matthew Brush
It works good too, but as I mentioned above, casting doc to correct pointer isn't simple in Python (and I have no ideas how to).
You have to define a structure type that matches what the pointer points to https://docs.python.org/2/library/ctypes.html#structures-and-unions and then cast the doc pointer to that https://docs.python.org/2/library/ctypes.html#type-conversions and then you should be able to access the fields.
…or just write C for 5 minutes instead of fighting Python for 5 days :)