Skip to content

Commit

Permalink
Julia formatter (#528)
Browse files Browse the repository at this point in the history
* add .JuliaFormatter.toml

* format everything

---------

Co-authored-by: Christopher Doris <github.com/cjdoris>
  • Loading branch information
cjdoris authored Jul 20, 2024
1 parent a1b72f7 commit 430fe4f
Show file tree
Hide file tree
Showing 64 changed files with 2,737 additions and 1,734 deletions.
8 changes: 8 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ignore = [
"examples",
".git",
".CondaPkg",
".ipynb_checkpoints",
".pytest_cache",
".venv",
]
2 changes: 1 addition & 1 deletion bump.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function bump(file, oldpat, newpat)
oldtext = read(file, String)
newtext = replace(oldtext, oldpat => newpat)
@assert newtext != oldtext
write(file, newtext)
write(file, newtext)
end

function bumpver(file, pattern, oldver, newver)
Expand Down
16 changes: 7 additions & 9 deletions docs/customdocs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ function runner(::Type{CustomDocExpander}, node, page, doc)
Dict{Symbol,Any}( # NOTE: Not sure about what to put here.
:module => Main, # This is supposed to be tracking python code.
:path => "",
:linenumber => 0
)
:linenumber => 0,
),
)::Docs.DocStr

# NOTE: This was modified because the original Documenter.create_docsnode was generating unreachable links
Expand All @@ -80,12 +80,10 @@ function _parse_docs(code::AbstractString)
m = match(r"^(.+?)\s*-\s*(.+?)\s*(\n[\s\S]*)$", strip(code))

if isnothing(m)
error(
"""
Invalid docstring:
$(code)
"""
)
error("""
Invalid docstring:
$(code)
""")
end

name = Symbol(m[1])
Expand Down Expand Up @@ -134,4 +132,4 @@ function _create_docsnode(docstring, result, object, page, doc)
return MarkdownAST.Node(docsnode)
end

end # module CustomDocs
end # module CustomDocs
23 changes: 7 additions & 16 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@ include("customdocs.jl")
makedocs(
sitename = "PythonCall & JuliaCall",
modules = [PythonCall],
format = Documenter.HTML(
assets = ["assets/favicon.ico"],
),
format = Documenter.HTML(assets = ["assets/favicon.ico"]),
warnonly = [:missing_docs], # avoid raising error when docs are missing
pages = [
"Home" => "index.md",
"The Julia module PythonCall" => [
"Guide" => "pythoncall.md",
"Reference" => "pythoncall-reference.md",
],
"The Python module JuliaCall" => [
"Guide" => "juliacall.md",
"Reference" => "juliacall-reference.md",
],
"The Julia module PythonCall" =>
["Guide" => "pythoncall.md", "Reference" => "pythoncall-reference.md"],
"The Python module JuliaCall" =>
["Guide" => "juliacall.md", "Reference" => "juliacall-reference.md"],
"Conversion" => [
"Julia to Python" => "conversion-to-python.md",
"Python to Julia" => "conversion-to-julia.md",
Expand All @@ -27,10 +21,7 @@ makedocs(
"faq.md",
"pycall.md",
"releasenotes.md",
]
],
)

deploydocs(
repo = raw"github.com/JuliaPy/PythonCall.jl.git",
push_preview = true
)
deploydocs(repo = raw"github.com/JuliaPy/PythonCall.jl.git", push_preview = true)
3 changes: 2 additions & 1 deletion src/C/C.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ using UnsafePointers: UnsafePtr
using CondaPkg: CondaPkg
using Pkg: Pkg
using Requires: @require
using Libdl: dlpath, dlopen, dlopen_e, dlclose, dlsym, dlsym_e, RTLD_LAZY, RTLD_DEEPBIND, RTLD_GLOBAL
using Libdl:
dlpath, dlopen, dlopen_e, dlclose, dlsym, dlsym_e, RTLD_LAZY, RTLD_DEEPBIND, RTLD_GLOBAL

include("consts.jl")
include("pointers.jl")
Expand Down
44 changes: 24 additions & 20 deletions src/C/context.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
A handle to a loaded instance of libpython, its interpreter, function pointers, etc.
"""
@kwdef mutable struct Context
is_embedded :: Bool = false
is_initialized :: Bool = false
is_preinitialized :: Bool = false
lib_ptr :: Ptr{Cvoid} = C_NULL
exe_path :: Union{String, Missing} = missing
lib_path :: Union{String, Missing} = missing
dlopen_flags :: UInt32 = RTLD_LAZY | RTLD_DEEPBIND | RTLD_GLOBAL
pyprogname :: Union{String, Missing} = missing
pyprogname_w :: Any = missing
pyhome :: Union{String, Missing} = missing
pyhome_w :: Any = missing
which :: Symbol = :unknown # :CondaPkg, :PyCall, :embedded or :unknown
version :: Union{VersionNumber, Missing} = missing
matches_pycall :: Union{Bool, Missing} = missing
is_embedded::Bool = false
is_initialized::Bool = false
is_preinitialized::Bool = false
lib_ptr::Ptr{Cvoid} = C_NULL
exe_path::Union{String,Missing} = missing
lib_path::Union{String,Missing} = missing
dlopen_flags::UInt32 = RTLD_LAZY | RTLD_DEEPBIND | RTLD_GLOBAL
pyprogname::Union{String,Missing} = missing
pyprogname_w::Any = missing
pyhome::Union{String,Missing} = missing
pyhome_w::Any = missing
which::Symbol = :unknown # :CondaPkg, :PyCall, :embedded or :unknown
version::Union{VersionNumber,Missing} = missing
matches_pycall::Union{Bool,Missing} = missing
end

const CTX = Context()
Expand Down Expand Up @@ -60,7 +60,9 @@ function init_context()
exe_path::String
else
# By default, we use Python installed by CondaPkg.
exe_path = Sys.iswindows() ? joinpath(CondaPkg.envdir(), "python.exe") : joinpath(CondaPkg.envdir(), "bin", "python")
exe_path =
Sys.iswindows() ? joinpath(CondaPkg.envdir(), "python.exe") :
joinpath(CondaPkg.envdir(), "bin", "python")
# It's not sufficient to only activate the env while Python is initialising,
# it must also be active when loading extension modules (e.g. numpy). So we
# activate the environment globally.
Expand All @@ -83,7 +85,7 @@ function init_context()

# Ensure Python is runnable
try
run(pipeline(`$exe_path --version`, stdout=devnull, stderr=devnull))
run(pipeline(`$exe_path --version`, stdout = devnull, stderr = devnull))
catch
error("Python executable $(repr(exe_path)) is not executable.")
end
Expand All @@ -99,9 +101,9 @@ function init_context()

# Find and open Python library
lib_path = something(
CTX.lib_path===missing ? nothing : CTX.lib_path,
CTX.lib_path === missing ? nothing : CTX.lib_path,
get(ENV, "JULIA_PYTHONCALL_LIB", nothing),
Some(nothing)
Some(nothing),
)
if lib_path !== nothing
lib_ptr = dlopen_e(lib_path, CTX.dlopen_flags)
Expand All @@ -112,7 +114,9 @@ function init_context()
CTX.lib_ptr = lib_ptr
end
else
for lib_path in readlines(python_cmd([joinpath(@__DIR__, "find_libpython.py"), "--list-all"]))
for lib_path in readlines(
python_cmd([joinpath(@__DIR__, "find_libpython.py"), "--list-all"]),
)
lib_ptr = dlopen_e(lib_path, CTX.dlopen_flags)
if lib_ptr == C_NULL
@warn "Python library $(repr(lib_path)) could not be opened."
Expand All @@ -138,7 +142,7 @@ function init_context()
init_pointers()

# Compare libpath with PyCall
@require PyCall="438e738f-606a-5dbb-bf0a-cddfbfd45ab0" init_pycall(PyCall)
@require PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0" init_pycall(PyCall)

# Initialize the interpreter
with_gil() do
Expand Down
5 changes: 3 additions & 2 deletions src/C/extras.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
Py_Type(x::PyPtr) = PyPtr(UnsafePtr(x).type[!])

PyObject_Type(x::PyPtr) = (t=Py_Type(x); Py_IncRef(t); t)
PyObject_Type(x::PyPtr) = (t = Py_Type(x); Py_IncRef(t); t)

Py_TypeCheck(o::PyPtr, t::PyPtr) = PyType_IsSubtype(Py_Type(o), t)
Py_TypeCheckFast(o::PyPtr, f::Integer) = PyType_IsSubtypeFast(Py_Type(o), f)

PyType_IsSubtypeFast(t::PyPtr, f::Integer) = Cint(!iszero(UnsafePtr{PyTypeObject}(t).flags[] & f))
PyType_IsSubtypeFast(t::PyPtr, f::Integer) =
Cint(!iszero(UnsafePtr{PyTypeObject}(t).flags[] & f))

PyMemoryView_GET_BUFFER(m::PyPtr) = Ptr{Py_buffer}(UnsafePtr{PyMemoryViewObject}(m).view)

Expand Down
24 changes: 14 additions & 10 deletions src/C/pointers.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const CAPI_FUNC_SIGS = Dict{Symbol, Pair{Tuple, Type}}(
const CAPI_FUNC_SIGS = Dict{Symbol,Pair{Tuple,Type}}(
# INITIALIZE
:Py_Initialize => () => Cvoid,
:Py_InitializeEx => (Cint,) => Cvoid,
Expand Down Expand Up @@ -270,29 +270,33 @@ const CAPI_OBJECTS = Set([
])

@eval @kwdef mutable struct CAPIPointers
$([:($name :: Ptr{Cvoid} = C_NULL) for name in CAPI_FUNCS]...)
$([:($name :: PyPtr = C_NULL) for name in CAPI_EXCEPTIONS]...)
$([:($name :: PyPtr = C_NULL) for name in CAPI_OBJECTS]...)
PyOS_InputHookPtr :: Ptr{Ptr{Cvoid}} = C_NULL
$([:($name::Ptr{Cvoid} = C_NULL) for name in CAPI_FUNCS]...)
$([:($name::PyPtr = C_NULL) for name in CAPI_EXCEPTIONS]...)
$([:($name::PyPtr = C_NULL) for name in CAPI_OBJECTS]...)
PyOS_InputHookPtr::Ptr{Ptr{Cvoid}} = C_NULL
end

const POINTERS = CAPIPointers()

@eval init_pointers(p::CAPIPointers=POINTERS, lib::Ptr=CTX.lib_ptr) = begin
@eval init_pointers(p::CAPIPointers = POINTERS, lib::Ptr = CTX.lib_ptr) = begin
$([
if name == :Py_FinalizeEx
:(p.$name = dlsym_e(lib, $(QuoteNode(name))))
else
:(p.$name = dlsym(lib, $(QuoteNode(name))))
end
for name in CAPI_FUNCS
end for name in CAPI_FUNCS
]...)
$([:(p.$name = Base.unsafe_load(Ptr{PyPtr}(dlsym(lib, $(QuoteNode(name)))::Ptr))) for name in CAPI_EXCEPTIONS]...)
$(
[
:(p.$name =
Base.unsafe_load(Ptr{PyPtr}(dlsym(lib, $(QuoteNode(name)))::Ptr))) for name in CAPI_EXCEPTIONS
]...
)
$([:(p.$name = dlsym(lib, $(QuoteNode(name)))) for name in CAPI_OBJECTS]...)
p.PyOS_InputHookPtr = dlsym(CTX.lib_ptr, :PyOS_InputHook)
end

for (name, (argtypes, rettype)) in CAPI_FUNC_SIGS
args = [Symbol("x", i) for (i,_) in enumerate(argtypes)]
args = [Symbol("x", i) for (i, _) in enumerate(argtypes)]
@eval $name($(args...)) = ccall(POINTERS.$name, $rettype, ($(argtypes...),), $(args...))
end
55 changes: 34 additions & 21 deletions src/Compat/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,41 @@
Misc bits and bobs for compatibility.
"""
module Compat
using ..PythonCall: PythonCall # needed for docstring cross-refs
using ..Core
using ..Core: Core, C, Utils, pynew, incref, getptr, pycopy!, pymodulehooks, pyisnull, pybytes_asvector, pysysmodule, pyosmodule, pystr_fromUTF8
using ..Convert: pyconvert, @pyconvert
using ..Wrap: PyArray, PyPandasDataFrame
using Serialization: Serialization, AbstractSerializer, serialize, deserialize
using Tables: Tables
using Requires: @require
using ..PythonCall: PythonCall # needed for docstring cross-refs
using ..Core
using ..Core:
Core,
C,
Utils,
pynew,
incref,
getptr,
pycopy!,
pymodulehooks,
pyisnull,
pybytes_asvector,
pysysmodule,
pyosmodule,
pystr_fromUTF8
using ..Convert: pyconvert, @pyconvert
using ..Wrap: PyArray, PyPandasDataFrame
using Serialization: Serialization, AbstractSerializer, serialize, deserialize
using Tables: Tables
using Requires: @require

include("gui.jl")
include("ipython.jl")
include("multimedia.jl")
include("serialization.jl")
include("tables.jl")
include("pycall.jl")
include("gui.jl")
include("ipython.jl")
include("multimedia.jl")
include("serialization.jl")
include("tables.jl")
include("pycall.jl")

function __init__()
C.with_gil() do
init_gui()
init_pyshow()
init_tables()
end
@require PyCall="438e738f-606a-5dbb-bf0a-cddfbfd45ab0" init_pycall(PyCall)
function __init__()
C.with_gil() do
init_gui()
init_pyshow()
init_tables()
end
@require PyCall = "438e738f-606a-5dbb-bf0a-cddfbfd45ab0" init_pycall(PyCall)
end
end
Loading

0 comments on commit 430fe4f

Please sign in to comment.