clj-python / libpython-clj

Python bindings for Clojure
Eclipse Public License 2.0
1.05k stars 68 forks source link

TypeError using curses #201

Closed rafonsecad closed 2 years ago

rafonsecad commented 2 years ago

Hey, first of all thank you for making this amazing project!

I'm new to clojure and I want to work with pwntools, at the moment to import the module as shown below

(ns pwn-clj.core
  (:require [libpython-clj2.require :refer [require-python]]
            [libpython-clj2.python :as py]
            [tech.v3.datatype :as dtype])
  (:gen-class))

(def pwn (py/import-module "pwn"))

(defn -main [& args]
  (py/call-attr pwn "ciclyc" 10))

I got the following error

$ lein run                                                                                                                             
abr. 14, 2022 6:18:49 P. M. clojure.tools.logging$eval690$fn__693 invoke
INFORMACIÓN: Detecting startup info
abr. 14, 2022 6:18:49 P. M. clojure.tools.logging$eval690$fn__693 invoke
INFORMACIÓN: Startup info {:lib-version "3.8", :java-library-path-addendum "/usr/lib", :exec-prefix "/usr", :executable "/usr/bin/python3", :libnames ("python3.8m" "python3.8"), :prefix "/usr", :base-prefix "/usr", :libname "python3.8m", :base-exec-prefix "/usr", :python-home "/usr", :version [3 8 10], :platform "linux"}
abr. 14, 2022 6:18:49 P. M. clojure.tools.logging$eval690$fn__693 invoke
INFORMACIÓN: Prefixing java library path: /usr/lib
abr. 14, 2022 6:18:50 P. M. clojure.tools.logging$eval690$fn__693 invoke
INFORMACIÓN: Loading python library: python3.8
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by tech.v3.datatype.UnsafeUtil (file:/home/user/.m2/repository/cnuernber/dtype-next/9.009/dtype-next-9.009.jar) to constructor java.nio.DirectByteBuffer(long,int)
WARNING: Please consider reporting this to the maintainers of tech.v3.datatype.UnsafeUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
abr. 14, 2022 6:18:50 P. M. clojure.tools.logging$eval690$fn__693 invoke
INFORMACIÓN: Reference thread starting
Execution error at libpython-clj2.python.ffi/check-error-throw (ffi.clj:703).
Traceback (most recent call last):
  File "/home/user/.local/lib/python3.8/site-packages/pwn/__init__.py", line 4, in <module>
    from pwn.toplevel import *
  File "/home/user/.local/lib/python3.8/site-packages/pwn/toplevel.py", line 22, in <module>
    import pwnlib
  File "/home/user/.local/lib/python3.8/site-packages/pwnlib/__init__.py", line 41, in <module>
    from . import args
  File "/home/user/.local/lib/python3.8/site-packages/pwnlib/args.py", line 60, in <module>
    from pwnlib import term
  File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/__init__.py", line 6, in <module>
    from pwnlib.term import completer
  File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/completer.py", line 7, in <module>
    from pwnlib.term import readline
  File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/readline.py", line 12, in <module>
    from pwnlib.term import text
  File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/text.py", line 127, in <module>
    sys.modules[__name__] = Module()
  File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/text.py", line 44, in __init__
    self._reset = termcap.get('reset')
  File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/unix_termcap.py", line 25, in get
    init()
  File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/unix_termcap.py", line 59, in init
    curses.setupterm()
TypeError: argument must be an int, or have a fileno() method.

Full report at:
/tmp/clojure-15646967815472035495.edn

It seems related to curses library, when I try the code

(def curses (py/import-module "curses"))

(defn -main [& args]
  (py/call-attr curses "setupterm"))

I got the same error

$ lein run
abr. 14, 2022 6:22:54 P. M. clojure.tools.logging$eval690$fn__693 invoke
INFORMACIÓN: Detecting startup info
abr. 14, 2022 6:22:54 P. M. clojure.tools.logging$eval690$fn__693 invoke
INFORMACIÓN: Startup info {:lib-version "3.8", :java-library-path-addendum "/usr/lib", :exec-prefix "/usr", :executable "/usr/bin/python3", :libnames ("python3.8m" "python3.8"), :prefix "/usr", :base-prefix "/usr", :libname "python3.8m", :base-exec-prefix "/usr", :python-home "/usr", :version [3 8 10], :platform "linux"}
abr. 14, 2022 6:22:54 P. M. clojure.tools.logging$eval690$fn__693 invoke
INFORMACIÓN: Prefixing java library path: /usr/lib
abr. 14, 2022 6:22:55 P. M. clojure.tools.logging$eval690$fn__693 invoke
INFORMACIÓN: Loading python library: python3.8
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by tech.v3.datatype.UnsafeUtil (file:/home/user/.m2/repository/cnuernber/dtype-next/9.009/dtype-next-9.009.jar) to constructor java.nio.DirectByteBuffer(long,int)
WARNING: Please consider reporting this to the maintainers of tech.v3.datatype.UnsafeUtil
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
abr. 14, 2022 6:22:55 P. M. clojure.tools.logging$eval690$fn__693 invoke
INFORMACIÓN: Reference thread starting
Syntax error compiling at (/tmp/form-init6754417702995568973.clj:1:73).
TypeError: argument must be an int, or have a fileno() method.

this is the full report

{:clojure.main/message
 "Syntax error compiling at (/tmp/form-init6754417702995568973.clj:1:73).\nTypeError: argument must be an int, or have a fileno() method.\n\n",
 :clojure.main/triage
 {:clojure.error/phase :compile-syntax-check,
  :clojure.error/line 1,
  :clojure.error/column 73,
  :clojure.error/source "form-init6754417702995568973.clj",
  :clojure.error/path "/tmp/form-init6754417702995568973.clj",
  :clojure.error/class java.lang.Exception,
  :clojure.error/cause
  "TypeError: argument must be an int, or have a fileno() method.\n"},
 :clojure.main/trace
 {:via
  [{:type clojure.lang.Compiler$CompilerException,
    :message
    "Syntax error compiling at (/tmp/form-init6754417702995568973.clj:1:73).",
    :data
    {:clojure.error/phase :compile-syntax-check,
     :clojure.error/line 1,
     :clojure.error/column 73,
     :clojure.error/source "/tmp/form-init6754417702995568973.clj"},
    :at [clojure.lang.Compiler load "Compiler.java" 7652]}
   {:type java.lang.Exception,
    :message
    "TypeError: argument must be an int, or have a fileno() method.\n",
    :at
    [libpython_clj2.python.ffi$check_error_throw
     invokeStatic
     "ffi.clj"
     703]}],
  :trace
  [[libpython_clj2.python.ffi$check_error_throw
    invokeStatic
    "ffi.clj"
    703]
   [libpython_clj2.python.ffi$check_error_throw invoke "ffi.clj" 701]
   [libpython_clj2.python.ffi$simplify_or_track
    invokeStatic
    "ffi.clj"
    960]
   [libpython_clj2.python.ffi$simplify_or_track invoke "ffi.clj" 941]
   [libpython_clj2.python.fn$call_py_fn invokeStatic "fn.clj" 197]
   [libpython_clj2.python.fn$call_py_fn invoke "fn.clj" 177]
   [libpython_clj2.python.fn$call_attr_kw$fn__9540 invoke "fn.clj" 228]
   [libpython_clj2.python.fn$call_attr_kw invokeStatic "fn.clj" 226]
   [libpython_clj2.python.fn$call_attr_kw invoke "fn.clj" 220]
   [libpython_clj2.python.fn$call_attr invokeStatic "fn.clj" 241]
   [libpython_clj2.python.fn$call_attr invoke "fn.clj" 238]
   [libpython_clj2.python$call_attr invokeStatic "python.clj" 194]
   [libpython_clj2.python$call_attr doInvoke "python.clj" 191]
   [clojure.lang.RestFn invoke "RestFn.java" 425]
   [pwn_clj.core$_main invokeStatic "core.clj" 22]
   [pwn_clj.core$_main doInvoke "core.clj" 21]
   [clojure.lang.RestFn invoke "RestFn.java" 397]
   [clojure.lang.Var invoke "Var.java" 380]
   [user$eval140 invokeStatic "form-init6754417702995568973.clj" 1]
   [user$eval140 invoke "form-init6754417702995568973.clj" 1]
   [clojure.lang.Compiler eval "Compiler.java" 7181]
   [clojure.lang.Compiler eval "Compiler.java" 7171]
   [clojure.lang.Compiler load "Compiler.java" 7640]
   [clojure.lang.Compiler loadFile "Compiler.java" 7578]
   [clojure.main$load_script invokeStatic "main.clj" 475]
   [clojure.main$init_opt invokeStatic "main.clj" 477]
   [clojure.main$init_opt invoke "main.clj" 477]
   [clojure.main$initialize invokeStatic "main.clj" 508]
   [clojure.main$null_opt invokeStatic "main.clj" 542]
   [clojure.main$null_opt invoke "main.clj" 539]
   [clojure.main$main invokeStatic "main.clj" 664]
   [clojure.main$main doInvoke "main.clj" 616]
   [clojure.lang.RestFn applyTo "RestFn.java" 137]
   [clojure.lang.Var applyTo "Var.java" 705]
   [clojure.main main "main.java" 40]],
  :cause
  "TypeError: argument must be an int, or have a fileno() method.\n",
  :phase :compile-syntax-check}}

Thanks in advance!

jjtolton commented 2 years ago

Does the equivalent python code work on your system?

python3 -c “import pwn; pwn.ciclyc(10)”

On Thu, Apr 14, 2022 at 7:27 PM rafonsecad @.***> wrote:

Hey, first of all thank you for making this amazing project!

I'm new to clojure and I want to work with pwntools https://github.com/Gallopsled/pwntools, at the moment to import the module as shown below

(ns pwn-clj.core

(:require [libpython-clj2.require :refer [require-python]]

        [libpython-clj2.python :as py]

        [tech.v3.datatype :as dtype])

(:gen-class))

(def pwn (py/import-module "pwn"))

(defn -main [& args]

(py/call-attr pwn "ciclyc" 10))

I got the following error

$ lein run

abr. 14, 2022 6:18:49 P. M. clojure.tools.logging$eval690$fn__693 invoke

INFORMACIÓN: Detecting startup info

abr. 14, 2022 6:18:49 P. M. clojure.tools.logging$eval690$fn__693 invoke

INFORMACIÓN: Startup info {:lib-version "3.8", :java-library-path-addendum "/usr/lib", :exec-prefix "/usr", :executable "/usr/bin/python3", :libnames ("python3.8m" "python3.8"), :prefix "/usr", :base-prefix "/usr", :libname "python3.8m", :base-exec-prefix "/usr", :python-home "/usr", :version [3 8 10], :platform "linux"}

abr. 14, 2022 6:18:49 P. M. clojure.tools.logging$eval690$fn__693 invoke

INFORMACIÓN: Prefixing java library path: /usr/lib

abr. 14, 2022 6:18:50 P. M. clojure.tools.logging$eval690$fn__693 invoke

INFORMACIÓN: Loading python library: python3.8

WARNING: An illegal reflective access operation has occurred

WARNING: Illegal reflective access by tech.v3.datatype.UnsafeUtil (file:/home/user/.m2/repository/cnuernber/dtype-next/9.009/dtype-next-9.009.jar) to constructor java.nio.DirectByteBuffer(long,int)

WARNING: Please consider reporting this to the maintainers of tech.v3.datatype.UnsafeUtil

WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations

WARNING: All illegal access operations will be denied in a future release

abr. 14, 2022 6:18:50 P. M. clojure.tools.logging$eval690$fn__693 invoke

INFORMACIÓN: Reference thread starting

Execution error at libpython-clj2.python.ffi/check-error-throw (ffi.clj:703).

Traceback (most recent call last):

File "/home/user/.local/lib/python3.8/site-packages/pwn/init.py", line 4, in

from pwn.toplevel import *

File "/home/user/.local/lib/python3.8/site-packages/pwn/toplevel.py", line 22, in

import pwnlib

File "/home/user/.local/lib/python3.8/site-packages/pwnlib/init.py", line 41, in

from . import args

File "/home/user/.local/lib/python3.8/site-packages/pwnlib/args.py", line 60, in

from pwnlib import term

File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/init.py", line 6, in

from pwnlib.term import completer

File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/completer.py", line 7, in

from pwnlib.term import readline

File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/readline.py", line 12, in

from pwnlib.term import text

File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/text.py", line 127, in

sys.modules[__name__] = Module()

File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/text.py", line 44, in init

self._reset = termcap.get('reset')

File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/unix_termcap.py", line 25, in get

init()

File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/unix_termcap.py", line 59, in init

curses.setupterm()

TypeError: argument must be an int, or have a fileno() method.

Full report at:

/tmp/clojure-15646967815472035495.edn

It seems related to curses library, when I try the code

(def curses (py/import-module "curses"))

(defn -main [& args]

(py/call-attr curses "setupterm"))

I got the same error

$ lein run

abr. 14, 2022 6:22:54 P. M. clojure.tools.logging$eval690$fn__693 invoke

INFORMACIÓN: Detecting startup info

abr. 14, 2022 6:22:54 P. M. clojure.tools.logging$eval690$fn__693 invoke

INFORMACIÓN: Startup info {:lib-version "3.8", :java-library-path-addendum "/usr/lib", :exec-prefix "/usr", :executable "/usr/bin/python3", :libnames ("python3.8m" "python3.8"), :prefix "/usr", :base-prefix "/usr", :libname "python3.8m", :base-exec-prefix "/usr", :python-home "/usr", :version [3 8 10], :platform "linux"}

abr. 14, 2022 6:22:54 P. M. clojure.tools.logging$eval690$fn__693 invoke

INFORMACIÓN: Prefixing java library path: /usr/lib

abr. 14, 2022 6:22:55 P. M. clojure.tools.logging$eval690$fn__693 invoke

INFORMACIÓN: Loading python library: python3.8

WARNING: An illegal reflective access operation has occurred

WARNING: Illegal reflective access by tech.v3.datatype.UnsafeUtil (file:/home/user/.m2/repository/cnuernber/dtype-next/9.009/dtype-next-9.009.jar) to constructor java.nio.DirectByteBuffer(long,int)

WARNING: Please consider reporting this to the maintainers of tech.v3.datatype.UnsafeUtil

WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations

WARNING: All illegal access operations will be denied in a future release

abr. 14, 2022 6:22:55 P. M. clojure.tools.logging$eval690$fn__693 invoke

INFORMACIÓN: Reference thread starting

Syntax error compiling at (/tmp/form-init6754417702995568973.clj:1:73).

TypeError: argument must be an int, or have a fileno() method.

this is the full report

{:clojure.main/message

"Syntax error compiling at (/tmp/form-init6754417702995568973.clj:1:73).\nTypeError: argument must be an int, or have a fileno() method.\n\n",

:clojure.main/triage

{:clojure.error/phase :compile-syntax-check,

:clojure.error/line 1,

:clojure.error/column 73,

:clojure.error/source "form-init6754417702995568973.clj",

:clojure.error/path "/tmp/form-init6754417702995568973.clj",

:clojure.error/class java.lang.Exception,

:clojure.error/cause

"TypeError: argument must be an int, or have a fileno() method.\n"},

:clojure.main/trace

{:via

[{:type clojure.lang.Compiler$CompilerException,

:message

"Syntax error compiling at (/tmp/form-init6754417702995568973.clj:1:73).",

:data

{:clojure.error/phase :compile-syntax-check,

 :clojure.error/line 1,

 :clojure.error/column 73,

 :clojure.error/source "/tmp/form-init6754417702995568973.clj"},

:at [clojure.lang.Compiler load "Compiler.java" 7652]}

{:type java.lang.Exception,

:message

"TypeError: argument must be an int, or have a fileno() method.\n",

:at

[libpython_clj2.python.ffi$check_error_throw

 invokeStatic

 "ffi.clj"

 703]}],

:trace

[[libpython_clj2.python.ffi$check_error_throw

invokeStatic

"ffi.clj"

703]

[libpython_clj2.python.ffi$check_error_throw invoke "ffi.clj" 701]

[libpython_clj2.python.ffi$simplify_or_track

invokeStatic

"ffi.clj"

960]

[libpython_clj2.python.ffi$simplify_or_track invoke "ffi.clj" 941]

[libpython_clj2.python.fn$call_py_fn invokeStatic "fn.clj" 197]

[libpython_clj2.python.fn$call_py_fn invoke "fn.clj" 177]

[libpython_clj2.python.fn$call_attr_kw$fn__9540 invoke "fn.clj" 228]

[libpython_clj2.python.fn$call_attr_kw invokeStatic "fn.clj" 226]

[libpython_clj2.python.fn$call_attr_kw invoke "fn.clj" 220]

[libpython_clj2.python.fn$call_attr invokeStatic "fn.clj" 241]

[libpython_clj2.python.fn$call_attr invoke "fn.clj" 238]

[libpython_clj2.python$call_attr invokeStatic "python.clj" 194]

[libpython_clj2.python$call_attr doInvoke "python.clj" 191]

[clojure.lang.RestFn invoke "RestFn.java" 425]

[pwn_clj.core$_main invokeStatic "core.clj" 22]

[pwn_clj.core$_main doInvoke "core.clj" 21]

[clojure.lang.RestFn invoke "RestFn.java" 397]

[clojure.lang.Var invoke "Var.java" 380]

[user$eval140 invokeStatic "form-init6754417702995568973.clj" 1]

[user$eval140 invoke "form-init6754417702995568973.clj" 1]

[clojure.lang.Compiler eval "Compiler.java" 7181]

[clojure.lang.Compiler eval "Compiler.java" 7171]

[clojure.lang.Compiler load "Compiler.java" 7640]

[clojure.lang.Compiler loadFile "Compiler.java" 7578]

[clojure.main$load_script invokeStatic "main.clj" 475]

[clojure.main$init_opt invokeStatic "main.clj" 477]

[clojure.main$init_opt invoke "main.clj" 477]

[clojure.main$initialize invokeStatic "main.clj" 508]

[clojure.main$null_opt invokeStatic "main.clj" 542]

[clojure.main$null_opt invoke "main.clj" 539]

[clojure.main$main invokeStatic "main.clj" 664]

[clojure.main$main doInvoke "main.clj" 616]

[clojure.lang.RestFn applyTo "RestFn.java" 137]

[clojure.lang.Var applyTo "Var.java" 705]

[clojure.main main "main.java" 40]],

:cause

"TypeError: argument must be an int, or have a fileno() method.\n",

:phase :compile-syntax-check}}

Thanks in advance!

— Reply to this email directly, view it on GitHub https://github.com/clj-python/libpython-clj/issues/201, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACPJX4Y3CRJH53KISX65HVTVFCSWHANCNFSM5TPEPMJQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

rafonsecad commented 2 years ago

hey, yes it does

$ python3 -c "import pwn; print(pwn.cyclic(10))"                                                              
b'aaaabaaaca'
cnuernber commented 2 years ago

What happens if you disable io redirection? When we redirect stdout and stderr they lose their fileno data. See docs for py/initialize! - :no-io-redirect? or something like that.

rafonsecad commented 2 years ago

@cnuernber the problem still continues even if io redirection is disabled

clj꞉pwn-clj.core꞉> (require '[libpython-clj2.python :as py])
nil
clj꞉pwn-clj.core꞉> (py/initialize! :no-io-redirect? false)       
:ok
clj꞉pwn-clj.core꞉> (require '[libpython-clj2.require :refer [require-python]])
nil
clj꞉pwn-clj.core꞉> (def pwn (py/import-module "pwn"))
; Execution error at libpython-clj2.python.ffi/check-error-throw (ffi.clj:703).
; Traceback (most recent call last):
  File "/home/user/.local/lib/python3.8/site-packages/pwn/__init__.py", line 4, in <module>
    from pwn.toplevel import *
  File "/home/user/.local/lib/python3.8/site-packages/pwn/toplevel.py", line 22, in <module>
    import pwnlib
  File "/home/user/.local/lib/python3.8/site-packages/pwnlib/__init__.py", line 41, in <module>
    from . import args
  File "/home/user/.local/lib/python3.8/site-packages/pwnlib/args.py", line 60, in <module>
    from pwnlib import term
  File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/__init__.py", line 6, in <module>
    from pwnlib.term import completer
  File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/completer.py", line 7, in <module>
    from pwnlib.term import readline
  File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/readline.py", line 12, in <module>
    from pwnlib.term import text
  File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/text.py", line 127, in <module>
;     sys.modules[__name__] = Module()
  File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/text.py", line 44, in __init__
    self._reset = termcap.get('reset')
  File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/unix_termcap.py", line 25, in get
    init()
  File "/home/user/.local/lib/python3.8/site-packages/pwnlib/term/unix_termcap.py", line 59, in init
    curses.setupterm()
TypeError: argument must be an int, or have a fileno() method.
clj꞉pwn-clj.core꞉>
cnuernber commented 2 years ago

Set :no-io-redirect to true :-). Sorry for the negative option format .

rafonsecad commented 2 years ago

@cnuernber I thought It wasn't going to work but it did, thanks

clj꞉pwn-clj.core꞉> (require '[libpython-clj2.python :as py])
nil
clj꞉pwn-clj.core꞉> (py/initialize! :no-io-redirect? true)
:ok
clj꞉pwn-clj.core꞉> (require '[libpython-clj2.require :refer [require-python]])
nil
clj꞉pwn-clj.core꞉> (def pwn (py/import-module "pwn"))
#'pwn-clj.core/pwn
clj꞉pwn-clj.core꞉> (py/call-attr pwn "cyclic" 10)
b'aaaabaaaca'
clj꞉pwn-clj.core꞉> (py/call-attr pwn "asm" "nop")
b'\x90'
cnuernber commented 2 years ago

That’s great!