Re: Can't make inout work.
On Sunday, 17 March 2019 at 20:23:44 UTC, Paul Backus wrote: On Sunday, 17 March 2019 at 10:49:03 UTC, aliak wrote: [...] For some reason, when you call `make("hello")`, the template argument T is being inferred as char[] instead of string. (You can see this by putting `pragma(msg, T)` in the body of make.) It works if you instantiate make explicitly with `make!string("hello")`. This seems like a bug to me. If you remove inout from the code, T is correctly deduced as string. https://issues.dlang.org/show_bug.cgi?id=19749
Re: Can't make inout work.
On 17/03/2019 18:34, Kagamin via Digitalmars-d-learn wrote: On Saturday, 16 March 2019 at 14:57:35 UTC, Paul Backus wrote: This code fails to compile if you change `auto s2` to `const s2`--in other words, it has the same problem as the original example. Maybe there's not much need for qualifiers anyway. This is what I meant as well. diniz
Re: what am I missing here with that working dir?
On Saturday, 16 March 2019 at 07:27:43 UTC, FreeSlave wrote: On Friday, 15 March 2019 at 21:48:50 UTC, DFTW wrote: What am I missing here? Maybe the terminal and your utility you run wkhtmltopdf from have different environment? I guessed so, I've tried set the env as well: enum env = ["LD_LIBRARY_PATH" : "/path/to/wkhtmltox-0.12.4_linux-generic-i386/wkhtmltox/lib"]; import std.process : execute, Config; auto conv = execute([app, html,pdf], env, Config.newEnv, size_t.max, workingDir); but that doesn't work either.
Re: what am I missing here with that working dir?
On Monday, 18 March 2019 at 15:23:46 UTC, DFTW wrote: On Saturday, 16 March 2019 at 07:27:43 UTC, FreeSlave wrote: On Friday, 15 March 2019 at 21:48:50 UTC, DFTW wrote: What am I missing here? Maybe the terminal and your utility you run wkhtmltopdf from have different environment? I guessed so, I've tried set the env as well: enum env = ["LD_LIBRARY_PATH" : "/path/to/wkhtmltox-0.12.4_linux-generic-i386/wkhtmltox/lib"]; import std.process : execute, Config; auto conv = execute([app, html,pdf], env, Config.newEnv, size_t.max, workingDir); but that doesn't work either. My assumption is, the issue is not related to D or the working dir. It is more a linux thing. Maybe you also try function executeShell. Also did you have a look e.ghere https://www.google.com/amp/s/www.cyberciti.biz/faq/debian-ubuntu-linux-wkhtmltopdf-error-while-loading-shared-libraries-libxrender-so-1/amp/ Kind regards Andre
Re: what am I missing here with that working dir?
On Monday, 18 March 2019 at 15:39:39 UTC, Andre Pany wrote: On Monday, 18 March 2019 at 15:23:46 UTC, DFTW wrote: On Saturday, 16 March 2019 at 07:27:43 UTC, FreeSlave wrote: [...] I guessed so, I've tried set the env as well: enum env = ["LD_LIBRARY_PATH" : "/path/to/wkhtmltox-0.12.4_linux-generic-i386/wkhtmltox/lib"]; import std.process : execute, Config; auto conv = execute([app, html,pdf], env, Config.newEnv, size_t.max, workingDir); but that doesn't work either. My assumption is, the issue is not related to D or the working dir. It is more a linux thing. Maybe you also try function executeShell. Also did you have a look e.ghere https://www.google.com/amp/s/www.cyberciti.biz/faq/debian-ubuntu-linux-wkhtmltopdf-error-while-loading-shared-libraries-libxrender-so-1/amp/ Kind regards Andre Also did you try, just this command? auto conv = execute([app, html,pdf]); As you use absolute paths, the working directory is not relevant. Kind regards Andre
is collectException working for every exceptions ?
Hello as the subject say i'm asking this question because with the following code ``` private config_create_answer create_config(string[] args) in { assert(args !is null, "args cannot be null"); assert(args.length == 2, "need 1 arguments"); } out (r) { assert(r.state == "SUCCESS", "create_config should success"); assert(!r.config_key.empty, "config_key should not be empty"); assert(!r.readonly_config_key.empty, "readonly_config_key should not be empty"); } body { string config_name; getopt(args, "name", &config_name); auto cfg = (cast(const char[])(std.file.read("../API_doc/json_recipes/config_create.json"))) .deserialize!config_create; cfg.config_name = config_name.strip("\""); client_.socket.send(cfg.serializeToJson); auto answer = new ubyte[256]; client_.socket.receive(answer); client_.socket.getErrorText.writeln; return (cast(string) answer).deserialize!config_create_answer; } unittest { import std.exception : collectException; auto cli = new CLI("/tmp/raven-os_service_albinos.sock"); assert(cli.create_config(["create_config", "--name=toto"]) .state == "SUCCESS", "should be success"); assert(cli.create_config(["create_config", "--name=\"titi\""]) .state == "SUCCESS", "should be success"); assert(collectException(cli.create_config(["create_config", "--name="]))); //here is my problem } ``` i would like to specify `collectException!GetOptException`, but it's seem's make the program exit with fail status. any idea what i'm doing wrong ? also it's my first d program, so if anything seem's bad let me know
Re: is collectException working for every exceptions ?
On Monday, 18 March 2019 at 18:54:22 UTC, Roman Sztergbaum wrote: Hello as the subject say i'm asking this question because with the following code [...] I'm asking myself if it's usefull to write unittest with contract programming also, but this is another subject i think
Re: what am I missing here with that working dir?
On Monday, 18 March 2019 at 15:39:39 UTC, Andre Pany wrote: On Monday, 18 March 2019 at 15:23:46 UTC, DFTW wrote: On Saturday, 16 March 2019 at 07:27:43 UTC, FreeSlave wrote: On Friday, 15 March 2019 at 21:48:50 UTC, DFTW wrote: What am I missing here? Maybe the terminal and your utility you run wkhtmltopdf from have different environment? I guessed so, I've tried set the env as well: enum env = ["LD_LIBRARY_PATH" : "/path/to/wkhtmltox-0.12.4_linux-generic-i386/wkhtmltox/lib"]; import std.process : execute, Config; auto conv = execute([app, html,pdf], env, Config.newEnv, size_t.max, workingDir); but that doesn't work either. My assumption is, the issue is not related to D or the working dir. It is more a linux thing. Maybe you also try function executeShell. Also did you have a look e.ghere https://www.google.com/amp/s/www.cyberciti.biz/faq/debian-ubuntu-linux-wkhtmltopdf-error-while-loading-shared-libraries-libxrender-so-1/amp/ Kind regards Andre You're right, it's a linux issue. I did get rid of the binaries I've had and compile from the source code on my own, just passing the aditional LD_LIBRARY_PATH set to where the .so files were located, it worked fine!
Re: dub getting stuck
On Sunday, 17 March 2019 at 09:04:37 UTC, Eugene Wissner wrote: On Sunday, 17 March 2019 at 07:20:47 UTC, Joel wrote: macOS 10.13.6 dmd 2.085.0 dub 1.3.0 [snip] dub 1.3.0 is something old. Is it reproducable with a newer version? Can one safely update dub by it's self (Home Brew), when it comes with DMD (Home Brew also)? Otherwise can be related: https://github.com/dlang/dub/issues/1345 https://github.com/dlang/dub/issues/1001
Any easy way to extract files to memory buffer?
Trying to speed up extracting some files that I first have to extract using the command line to files then read those in... Not sure what is taking so long. I imagine windows caches the extraction so maybe it is pointless?
Re: Any easy way to extract files to memory buffer?
On Monday, 18 March 2019 at 21:09:55 UTC, Michelle Long wrote: Trying to speed up extracting some files that I first have to extract using the command line to files then read those in... Not sure what is taking so long. I imagine windows caches the extraction so maybe it is pointless? You can speed up such things using a tmpfs or RAM disk. On Linux you just use mount -t tmpfs ... On Windows it is a little more complicated, there are programs like ImDisk that can create RAM drives (which can then be formatted to whatever). If it's just one file, sometimes you can pipe it from the unpacking program (tar etc.) into the consuming program.
Re: is collectException working for every exceptions ?
On Monday, 18 March 2019 at 18:54:22 UTC, Roman Sztergbaum wrote: Hello as the subject say i'm asking this question because with the following code ``` private config_create_answer create_config(string[] args) in { assert(args !is null, "args cannot be null"); assert(args.length == 2, "need 1 arguments"); } out (r) { assert(r.state == "SUCCESS", "create_config should success"); assert(!r.config_key.empty, "config_key should not be empty"); assert(!r.readonly_config_key.empty, "readonly_config_key should not be empty"); } body { string config_name; getopt(args, "name", &config_name); auto cfg = (cast(const char[])(std.file.read("../API_doc/json_recipes/config_create.json"))) .deserialize!config_create; cfg.config_name = config_name.strip("\""); client_.socket.send(cfg.serializeToJson); auto answer = new ubyte[256]; client_.socket.receive(answer); client_.socket.getErrorText.writeln; return (cast(string) answer).deserialize!config_create_answer; } unittest { import std.exception : collectException; auto cli = new CLI("/tmp/raven-os_service_albinos.sock"); assert(cli.create_config(["create_config", "--name=toto"]) .state == "SUCCESS", "should be success"); assert(cli.create_config(["create_config", "--name=\"titi\""]) .state == "SUCCESS", "should be success"); assert(collectException(cli.create_config(["create_config", "--name="]))); //here is my problem } ``` i would like to specify `collectException!GetOptException`, but it's seem's make the program exit with fail status. any idea what i'm doing wrong ? also it's my first d program, so if anything seem's bad let me know I haven't spot the exact position of the problem yet, but I think the usage of assert is not correct at same places of your coding. - Assertions are for logic errors, not for issues related to resources (network, files, input from users,...) You cannot recover from a logic error, there assert terminates the application. - for resource issues you should throw Exceptions. You can recover from resource issues. - assertions throwing "Errors" which are not catched by collectException. Kind regards Andre
SEGFAULT when converting C string to string
I'm writing a shared library in C to be used from a C program, so I went to my small tests. That one failed give a SEGFAULT on the std.conv.to call. This is the pice of D code/the library: extern(C) int foo(const char *name, int age) { import core.stdc.stdio : printf; import core.stdc.stdlib; import std.conv : to; printf("printf() got called! age = %d\n", age); printf("name = [%s]\n", name); //auto nm = to!string(name); // <-- everything works until I uncomment this line. return age * 2; } if matters, compiled with dub: "targetType" : "dynamicLibrary" on 64-bit machine. This is the piece of C code, foo.c: #include #include #include int main(void) { printf("+main();\n"); const char* libpath = "mylib.so"; void *lh = dlopen(libpath, RTLD_LAZY); if(!lh) { fprintf(stderr, "dlopen error: %s\n", dlerror()); return EXIT_FAILURE; } int (*fn)(const char*, int) = dlsym(lh, "foo"); char *err = dlerror(); if(err) { fprintf(stderr, "dlsym error: %s\n", err); return EXIT_FAILURE; } const char *name = "jhon"; int age = 18; int result = (*fn)(name, age); printf("unloading lib"); dlclose(lh); printf("result = %d\n", result); printf("-main()\n"); return 0; } compile as: gcc foo.c -ldl My OS is Ubuntu version 18 I'm assuming const char* both in C and D are raw pointers.
PyD - accessing D class fields in Python
Howdy; I'm trying to extend my Python program with D, but I'm having trouble accessing a D class's field/attribute/property/something. My D file looks like this: ``` module simpletest; import pyd.pyd; import std.stdio; class ExampleTest{ int _a = 3; int a(){ return _a; } void a(int a){ this._a = a; } int b = 4; } extern(C) void PydMain() { module_init(); wrap_class!( ExampleTest, Property!(ExampleTest.a), Property!(ExampleTest.b) // Member!("b") ); } ``` and my Python file looks like this: ``` #!/usr/bin/env python3.7 # -*- coding: utf-8 -*- """Run with `pipenv run pytest -s tests/test_simple_test.py`.""" import simpletest def test_class(): test = simpletest.ExampleTest() assert None is print(test.a) assert test.a == 3 test.a = 2 assert test.a == 2 assert None is print(test.b) ``` The first field, `a`, I can read, but `attribute 'b' of 'simpletest.ExampleTest' objects is not readable`. `a` has a getter and setter function; do I have to make getters and setters for all fields I want to share with Python? That sounds like a lot of wasted space if so. The only examples at the Github (https://github.com/ariovistus/pyd/tree/master/examples) only seem to show it with getters and setters.
Re: SEGFAULT when converting C string to string
On Mon, Mar 18, 2019 at 10:20:35PM +, DFTW via Digitalmars-d-learn wrote: > I'm writing a shared library in C to be used from a C program, so I > went to my small tests. That one failed give a SEGFAULT on the > std.conv.to call. > > This is the pice of D code/the library: > > extern(C) int foo(const char *name, int age) > { > import core.stdc.stdio : printf; > import core.stdc.stdlib; > import std.conv : to; > printf("printf() got called! age = %d\n", age); > printf("name = [%s]\n", name); > //auto nm = to!string(name); // <-- everything works until I uncomment > this line. > return age * 2; > } [...] Most likely explanation: you failed to call rt_init() before using a language feature that requires druntime to be initialized. In this case, a GC allocation by std.conv.to!string. Call rt_init() immediately after loading the library, and calling rt_term() after you're done with it, should fix this problem. T -- Talk is cheap. Whining is actually free. -- Lars Wirzenius
Re: Any easy way to extract files to memory buffer?
On Monday, 18 March 2019 at 21:14:05 UTC, Vladimir Panteleev wrote: On Monday, 18 March 2019 at 21:09:55 UTC, Michelle Long wrote: Trying to speed up extracting some files that I first have to extract using the command line to files then read those in... Not sure what is taking so long. I imagine windows caches the extraction so maybe it is pointless? You can speed up such things using a tmpfs or RAM disk. On Linux you just use mount -t tmpfs ... On Windows it is a little more complicated, there are programs like ImDisk that can create RAM drives (which can then be formatted to whatever). If it's just one file, sometimes you can pipe it from the unpacking program (tar etc.) into the consuming program. Yeah, but it seems like a lot of work simply to extract the files to memory.
Re: SEGFAULT when converting C string to string
On Monday, 18 March 2019 at 22:33:21 UTC, H. S. Teoh wrote: On Mon, Mar 18, 2019 at 10:20:35PM +, DFTW via Digitalmars-d-learn wrote: [...] [...] Most likely explanation: you failed to call rt_init() before using a language feature that requires druntime to be initialized. In this case, a GC allocation by std.conv.to!string. Call rt_init() immediately after loading the library, and calling rt_term() after you're done with it, should fix this problem. T That's right. From the small code samples I've seen, they had no rt_init() usage, so I wasn't aware I've had to call it. Thanks!
Emulating DLL
Is it possible to create a D module that has functions in it, and then use those functions dynamically at run time emulating DLL like functionality? e.g., module MyDL; import Context; export void foo(Context) { ... } and module UseDL; import DL; import Context; void main() { DL.Load("MyDL.dl"); DL.foo(new Context); DL.Unload(); // or DL.Loadable("MyDL.dl"); // monitors changes and reloads the function safely when a change is detected. This might require some work to make safe. DL.foo(new Context); DL.Unload(); } (pseudo) The idea here is to simply be able to call external code that can be hotswapped and such. It requires dealing with relocatable code, GC, etc. It would be portable and non-windows specific and just work. dmd and ldc of course do not output anything that is reasonable as they use the traditional non-portable formats. This leads me to think that without modifying them, I'd have to extract the code from the DLL/SO and deal with all the problems and then generate the .dl file. The idea is to create simple way to "script" D programs and use the D compiler to compile the functionality. It won't be as fast as an interpreter but it will have full speed. I'm just not sure what would be the best way to generate the code and do the fixing up. I'd be willing to hack on the dmd compiler to try and get it to generate a more sane file for this process if someone could point me to some details.
Re: Emulating DLL
On Monday, 18 March 2019 at 22:50:57 UTC, Craig wrote: Is it possible to create a D module that has functions in it, and then use those functions dynamically at run time emulating DLL like functionality? On Monday, 18 March 2019 at 22:50:57 UTC, Craig wrote: Is it possible to create a D module that has functions in it, and then use those functions dynamically at run time emulating DLL like functionality? I've talked extensively on this topic at DConf over the last few years. http://dconf.org/2016/talks/watson.html http://dconf.org/2017/talks/watson.html http://dconf.org/2018/talks/watson.html https://github.com/GooberMan/binderoo It's not a simple thing by any means, there's plenty of little implementation details that you'll need to be aware of. But yes, it's quite possible.
Re: Any easy way to extract files to memory buffer?
On Mon, Mar 18, 2019 at 10:38:17PM +, Michelle Long via Digitalmars-d-learn wrote: > On Monday, 18 March 2019 at 21:14:05 UTC, Vladimir Panteleev wrote: > > On Monday, 18 March 2019 at 21:09:55 UTC, Michelle Long wrote: > > > Trying to speed up extracting some files that I first have to > > > extract using the command line to files then read those in... > > > > > > Not sure what is taking so long. I imagine windows caches the > > > extraction so maybe it is pointless? [...] Why not just use std.mmfile to memory-map the file into memory directly? Let the OS take care of actually paging in the file data. T -- Give me some fresh salted fish, please.
Re: Any easy way to extract files to memory buffer?
On Monday, 18 March 2019 at 23:01:27 UTC, H. S. Teoh wrote: On Mon, Mar 18, 2019 at 10:38:17PM +, Michelle Long via Digitalmars-d-learn wrote: On Monday, 18 March 2019 at 21:14:05 UTC, Vladimir Panteleev wrote: > On Monday, 18 March 2019 at 21:09:55 UTC, Michelle Long > wrote: > > Trying to speed up extracting some files that I first have > > to extract using the command line to files then read those > > in... > > > > Not sure what is taking so long. I imagine windows caches > > the extraction so maybe it is pointless? [...] Why not just use std.mmfile to memory-map the file into memory directly? Let the OS take care of actually paging in the file data. T The files are on disk and there is an external program that read them and converts them and then writes the converted files to disk then my program reads. Ideally the conversion program would take memory instead of disk files but it doesn't.
Re: is collectException working for every exceptions ?
On 03/18/2019 11:54 AM, Roman Sztergbaum wrote: > Hello as the subject say i'm asking this question because with the > following code Andre Pany has already explained. Otherwise, I was going to say "collectException can collect Exceptions, not exceptions." ;) There have been many discussions on the General forum on when to call assert() vs. enforce() (essentially, 'throw new Exception') and what should go inside contracts. Unrelated, with expression based contracts and the 'body' keyword now being an optional (context-dependent?) keyword, your function can be written more cleanly: private config_create_answer create_config(string[] args) in (args !is null, "args cannot be null") in (args.length == 2, "need 1 arguments") out (r; r.state == "SUCCESS", "create_config should success") out (r; !r.config_key.empty, "config_key should not be empty") out (r; !r.readonly_config_key.empty, "readonly_config_key should not be empty"); { // ... } Ali
gtkD: How to paint to screen for animation
I've added a function to addOnDraw for a DrawingArea and it paints using the code I have when I resize. I added a queueDraw in threadsAddIdle and it seems to draws the screen immediately but it does not seem to be called again. If I put queueDraw inside the addOnDraw routine then the animation works but it is quite slow, about 1 fps and cpu usage is 100% without it, it is 0%.