Suppose there is a script A that calls function B, both in Julia.
There are some errors in function B, which cause the script to be stopped at runtime.
Is there a neat way to find out which line is causing the error?
It does not make any sense, to have to put messages like println manually in each line to find out upto which line the code survives, and in which line error happens.
Edit: I am using Linux Red Hat 4.1.2 and Julia version 0.3.6. directly. With no IDE.
Reading the backtrace:
juser#juliabox:~$ cat foo.jl
# line 1 empty comment
foo() = error("This is line 2")
foo() # line 3
juser#juliabox:~$ julia foo.jl
ERROR: This is line 2
in foo at /home/juser/foo.jl:2
in include at ./boot.jl:245
in include_from_node1 at loading.jl:128
in process_options at ./client.jl:285
in _start at ./client.jl:354
while loading /home/juser/foo.jl, in expression starting on line 3
This lines in foo at /home/juser/foo.jl:2 ... while loading /home/juser/foo.jl, in expression starting on line 3 reads as: "there was an error at line 2 in /home/juser/foo.jl file ... while loading /home/juser/foo.jl, in expression starting on line 3"
Looks pretty clear to me!
Edit: /home/juser/foo.jl:2 means; file: /home/juser/foo.jl, line number: 2.
Also you could use #show macro instead of println function for debugging purposes:
julia> println(1 < 5 < 10)
true
julia> #show 1 < 5 < 10
(1<5<10) => true
true
Related
My code:
if $ nice_dec4 = true:
a "Last night, was fantastic, I.."
a "Needed it."
elif $ mean_dec4 = true:
a "Hey.."
a "I was wondering if.."
a "Nevermind."
b "NO!"
b "Could I.. stay over?"
show sloane tired school
a "Why would you lie to me..?"
a "Why do you hit the mouse button like this isn't my life."
a "Why don't you care.. anymore?"
a "Come back soon,"
a "{b}{i}Darling."
The traceback is as follows:
[code]
I'm sorry, but an uncaught exception occurred.
While running game code:
File "game/script.rpy", line 712, in script
if $ nice_dec4 = true:
SyntaxError: invalid syntax (script.rpy, line 712)
-- Full Traceback ------------------------------------------------------------
Full traceback:
File "game/script.rpy", line 712, in script
if $ nice_dec4 = true:
File "/Users/NAME/Desktop/renpy-8.0.1-sdk/renpy/ast.py", line 2115, in execute
if renpy.python.py_eval(condition):
File "/Users/NAME/Desktop/renpy-8.0.1-sdk/renpy/python.py", line 1081, in py_eval
code = py_compile(code, 'eval')
File "/Users/NAME/Desktop/renpy-8.0.1-sdk/renpy/python.py", line 1018, in py_compile
raise e
File "/Users/NAME/Desktop/renpy-8.0.1-sdk/renpy/python.py", line 970, in py_compile
raise orig_e
File "/Users/NAME/Desktop/renpy-8.0.1-sdk/renpy/python.py", line 963, in py_compile
tree = compile(source, filename, py_mode, ast.PyCF_ONLY_AST | flags, 1)
SyntaxError: invalid syntax (script.rpy, line 712)
macOS-10.16-x86_64-i386-64bit x86_64
Ren'Py 8.0.2.22081402
Me And Sloane 1.2
Sun Dec 4 15:33:04 2022
[/code]
I expected the code to change based on a decision I made the player make earlier, all I got was this error.
IGNORE-----------------------------------------------------------------
"It looks like your post is mostly code; please add some more details":
mabklwglabd,jagdwlvgkulygwdvg agwlgd. w gdwilyavjkdfwvakuwdmvawwhmgwa,fywdjgcwfuwgcmhv aw,vw j, dwa dfldf,vdwm kw amvwdjawf wdlva;hwjha.,mww
When you use an if/elif operator in Ren'Py, you don't need to use the $ symbol.
if nice_dec4 = true:
a "Last night, was fantastic, I.."
a "Needed it."
elif mean_dec4 = true:
a "Hey.."
a "I was wondering if.."
$ symbol is used to define variables. For example, $ var1 = True
I am building a shippable app with Julia using the PackageCompiler.jl library.
https://github.com/JuliaLang/PackageCompiler.jl
I have followed the example here and got it to run as expected:
https://github.com/JuliaLang/PackageCompiler.jl
I am now trying to modify the code piece by piece to replace the toy function with my own function. It involves reading a CSV file into a dataframe whose path is taken from the command line. For now, I am just trying to accomplish this task.
I have this code in MyApp.jl:
module MyApp
using Example
using HelloWorldC_jll
using Pkg.Artifacts
using DataFrames, CSV, Statistics
fooifier_path() = joinpath(artifact"fooifier", "bin", "fooifier" * (Sys.iswindows() ? ".exe" : ""))
function julia_main()
try
real_main()
catch
Base.invokelatest(Base.display_error, Base.catch_stack())
return 1
end
return 0
end
function real_main()
# #show ARGS
# #show Base.PROGRAM_FILE
# #show DEPOT_PATH
# #show LOAD_PATH
# #show pwd()
# #show Base.active_project()
# #show Threads.nthreads()
# #show Sys.BINDIR
# display(Base.loaded_modules)
for arg in ARGS
println(arg)
end
println("this part worked!")
df = CSV.read(string(ARGS[1]), DataFrame)[10:end-10,:];
println(df)
return
end
if abspath(PROGRAM_FILE) == #__FILE__
real_main()
end
I compile the code with:
using PackageCompiler;
create_app("MyApp", "MyAppCompiled")
and it compiles just fine.
I run it with:
julia MyAppCompiled/bin/MyApp <absolute path to csv>
and it works up until the dataframe portion, where I get this error:
it worked!
MyApp(66457,0x115997dc0) malloc: *** error for object 0x1117d9510: pointer being realloc'd was not allocated
MyApp(66457,0x115997dc0) malloc: *** set a breakpoint in malloc_error_break to debug
signal (6): Abort trap: 6
in expression starting at none:0
__pthread_kill at /usr/lib/system/libsystem_kernel.dylib (unknown line)
Allocations: 47484171 (Pool: 47468051; Big: 16120); GC: 49
Abort trap: 6
Can anyone help me figure out what I am doing wrong? I know I am putting in the proper path because if I incorrectly type the path I get the " is not a proper file..." message. I've tried calling with relative and absolute paths.
The code i wrote in time series forecasting is as follows.
It keeps getting the error.
for param in pdq:
for param_seasonal in seasonal_pdq:
try:
mod = sm.tsa.statespace.SARIMAX(indexedDataset,order=param,seasonal_order=param_seasonal,enforce_stationarity=False,enforce_invertibility=False)
results = mod.fit()
print('ARIMA{}x{}12 - AIC:{}'.format(param,param_seasonal,results.aic))
except: continue
File "<ipython-input-211-c755485ed830>", line 7
except: continue
^
IndentationError: unexpected unindent
for param in pdq:
for param_seasonal in seasonal_pdq:
try:
mod = sm.tsa.statespace.SARIMAX(indexedDataset,order=param,seasonal_order=param_seasonal,enforce_stationarity=False,enforce_invertibility=False)
results = mod.fit()
print('ARIMA{}x{}12 - AIC:{}'.format(param,param_seasonal,results.aic))
except:
continue
except needs to be at the same level of indent with try.
Please be more careful with indentation when you write python scripts.
In Julia, I want to use addprocs and pmap inside a function that is defined inside a module. Here's a silly example:
module test
using Distributions
export g, f
function g(a, b)
a + rand(Normal(0, b))
end
function f(A, b)
close = false
if length(procs()) == 1 # If there are already extra workers,
addprocs() # use them, otherwise, create your own.
close = true
end
W = pmap(x -> g(x, b), A)
if close == true
rmprocs(workers()) # Remove the workers you created.
end
return W
end
end
test.f(randn(5), 1)
This returns a very long error
WARNING: Module test not defined on process 4
WARNING: Module test not defined on process 3
fatal error on fatal error on WARNING: Module test not defined on process 2
43: : WARNING: Module test not defined on process 5
fatal error on fatal error on 5: 2: ERROR: UndefVarError: test not defined
in deserialize at serialize.jl:504
in handle_deserialize at serialize.jl:477
in deserialize at serialize.jl:696
...
in message_handler_loop at multi.jl:878
in process_tcp_streams at multi.jl:867
in anonymous at task.jl:63
Worker 3 terminated.
Worker 2 terminated.ERROR (unhandled task failure): EOFError: read end of file
WARNING: rmprocs: process 1 not removed
Worker 5 terminated.ERROR (unhandled task failure): EOFError: read end of file
4-element Array{Any,1}:Worker 4 terminated.ERROR (unhandled task failure): EOFError: read end of file
ERROR (unhandled task failure): EOFError: read end of file
ProcessExitedException()
ProcessExitedException()
ProcessExitedException()
ProcessExitedException()
What I'm trying to do is write a package that contains functions that perform operations that can be optionally parallelized at the user's discretion. So a function like f might take an argument par::Bool that does something like I've shown above if the user calls f with par = true and loops otherwise. So from within the definition of f (and within the definition of the module test), I want to create workers and broadcast the Distributions package and the function g to them.
What's wrong with using #everywhere in your function? The following, for example, works fine on my computer.
function f(A, b)
close = false
if length(procs()) == 1 # If there are already extra workers,
addprocs() # use them, otherwise, create your own.
#everywhere begin
using Distributions
function g(a, b)
a + rand(Normal(0, b))
end
end
close = true
end
W = pmap(x -> g(x, b), A)
if close == true
rmprocs(workers()) # Remove the workers you created.
end
return W
end
f(randn(5), 1)
Note: when I first ran this, I needed to recompile the Distributions package since it had been updated since I had last used it. When I first tried the above script right after the recompiling, it failed. But, I then quit Julia and reopened it and it worked fine. Perhaps that was what is causing your error?
This question already has answers here:
Get a list of function names in a shell script [duplicate]
(4 answers)
Closed 6 years ago.
Lets say I have a script that has a bunch of functions that act like test cases. So for example:
#!/bin/bash
...
function testAssertEqualsWithStrings () {
assertEquals "something" "something"
}
testAssertEqualsWithStrings <--- I dont want to do this
function testAssertEqualsWithIntegers () {
assertEquals 10 10
}
testAssertEqualsWithIntegers <--- I dont want to do this
function testAssertEqualsWithIntegers2 () {
assertEquals 5 $((10 - 5))
}
testAssertEqualsWithIntegers2 <--- I dont want to do this
function testAssertEqualsWithDoubles () {
assertEquals 5.5 5.5
}
testAssertEqualsWithDoubles <--- I dont want to do this
...
Is there a way I can call of these functions in order without having to actually use that explicit function call underneath each test case? The idea is that the user shouldnt have to manage calling the functions. Ideally, the test suite library would do it for them. I just need to know if this is even possible.
Edit: The reason why I dont use just the assert methods is so I can have meaningful output. My current setup allows me to have output such as this:
...
LineNo 14: Passed - testAssertEqualsWithStrings
LineNo 19: Passed - testAssertEqualsWithIntegers
LineNo 24: Passed - testAssertEqualsWithIntegers2
LineNo 29: Passed - testAssertEqualsWithDoubles
LineNo 34: Passed - testAssertEqualsWithDoubles2
...
LineNo 103: testAssertEqualsWithStringsFailure: assertEquals() failed. Expected "something", but got "something else".
LineNo 108: testAssertEqualsWithIntegersFailure: assertEquals() failed. Expected "5", but got "10".
LineNo 115: testAssertEqualsWithArraysFailure: assertEquals() failed. Expected "1,2,3", but got "4,5,6".
LineNo 120: testAssertNotSameWithIntegersFailure: assertNotSame() failed. Expected not "5", but got "5".
LineNo 125: testAssertNotSameWithStringsFailure: assertNotSame() failed. Expected not "same", but got "same".
...
EDIT: Solution that worked for me
function runUnitTests () {
testNames=$(grep "^function" $0 | awk '{print $2}')
testNamesArray=($testNames)
beginUnitTests #prints some pretty output
for testCase in "${testNamesArray[#]}"
do
:
eval $testCase
done
endUnitTests #prints some more pretty output
}
Then I call runUnitTests at the bottom of my test suite.
If you just want to run these functions without calling them, why declare them as functions in the first place?
You either need them to be functions because you are using them multiple times and need the calls to be different each time, in which case I don't see an issue.
Otherwise you just want to run each function once without calling them, which is just running the command. Remove all the function declarations and just call each line.
For this example
#!/bin/bash
...
assertEquals "something" "something" #testAssertEqualsWithStrings
assertEquals 10 10 #testAssertEqualsWithIntegers
assertEquals 5 $((10 - 5)) #testAssertEqualsWithIntegers2
assertEquals 5.5 5.5 #testAssertEqualsWithDoubles
...