bendudson / py4cl

Call python from Common Lisp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error finding package error when looking for for a python exception from imported module

vxe opened this issue · comments

I am importing the below library

https://docs.python.org/3/library/ipaddress.html#custom-exceptions

like this

(py4cl:import-module "ipaddress" :as "ipaddress")

in an asdf project containing this function

(defun mask->cidr (mask)
  (let* ((ipv4-network (ipaddress:ip_network (concatenate 'string "0.0.0.0/" mask))))
    (py4cl:chain ipv4-network prefixlen)))

When loading my project in quicklisp I get the error

Error finding package for symbol "ADDRESSVALUEERROR":
 The name "IPADDRESS" does not designate any package.
   [Condition of type SB-KERNEL:SIMPLE-PACKAGE-ERROR]

where ADDRESSVALUEERROR is an python Exception for the this library apparently

https://docs.python.org/3/library/ipaddress.html#ipaddress.AddressValueError

Curious if

a) anyone can reproduce this
b) if there is a way to import using the from method, I think in python you would get access to AddressValueError like so

from ipaddress import AddressValueError

but not clear if that is possible in py4cl

thanks! great lib btw

With the following code inside the source code file accompanying the corresponding asd file, the asd-system is loaded into SLIME (for this as well as the forked version - with appropriate changes):

(eval-when (:compile-toplevel :load-toplevel :execute)
  (py4cl:import-module "ipaddress" :as "IPADDRESS" :reload t))

(defpackage py4cl-test
  (:use :py4cl :cl))
(in-package :py4cl-test)

(defun mask->cidr (mask)
  (let* ((ipv4-network (ipaddress:ip_network (concatenate 'string
                                                          "0.0.0.0/" mask))))
    (py4cl:chain ipv4-network prefixlen)))

I didn't encounter this issue while packaging this - or so I thought. However, as I tested it now, wrapping it inside (eval-when) is necessary.

Even, defpackage macroexpands to contain a (eval-when). I've added it in the forked version - but this should resolve your issue. Thank you for the feedback!

EDIT: Earlier, I had mentioned (eval-when (:compile-toplevel)) - however, that gives errors when the package is not required to be (re)compiled: since that code is run only during compilation time.

b) if there is a way to import using the from method, I think in python you would get access to AddressValueError like so

from ipaddress import AddressValueError

You can use the :from argument of import-function:

CL-USER> (import-function "AddressValueError" :from "ipaddress")
ADDRESSVALUEERROR

(Did you check (describe 'import-function)? Or did you mean something different?)

With the following code inside the source code file accompanying the corresponding asd file, the asd-system is loaded into SLIME (for this as well as the forked version - with appropriate changes):

(eval-when (:compile-toplevel :load-toplevel :execute)
  (py4cl:import-module "ipaddress" :as "IPADDRESS" :reload t))

(defpackage py4cl-test
  (:use :py4cl :cl))
(in-package :py4cl-test)

(defun mask->cidr (mask)
  (let* ((ipv4-network (ipaddress:ip_network (concatenate 'string
                                                          "0.0.0.0/" mask))))
    (py4cl:chain ipv4-network prefixlen)))

I didn't encounter this issue while packaging this - or so I thought. However, as I tested it now, wrapping it inside (eval-when) is necessary.

Even, defpackage macroexpands to contain a (eval-when). I've added it in the forked version - but this should resolve your issue. Thank you for the feedback!

EDIT: Earlier, I had mentioned (eval-when (:compile-toplevel)) - however, that gives errors when the package is not required to be (re)compiled: since that code is run only during compilation time.

A still better method is to use the :export option of defpackage - that should avoid having to do all the work at every execution time.

As far as I understand, 0821956 should have fixed this.